Power Community

Power Community

Instant details of your Azure Subscription costs via CLI

Some days ago after finishing an Azure training someone of the attendees asked me to give more details about a tool I used to retrieve the costs of my different Azure subscriptions. As promised, in this quick post I share some more details about that.

Before explaining the tool, I need to say that the Azure Portal offers a great Cost Analysis tool for analyzing the costs of your subscription:

With the standard tool in Azure Portal you can visualize cost usage by applying filters over a specified time and you can determine the maximum cost incurred by a resource group (or) resource for any customized date range (useful for checking for anomalies on costs).

You can also create Management Groups (containers that help you manage access, policy, and compliance across multiple subscriptions) and checking costs for groups of subscriptions:

The tool I used in that demo is not an official Microsoft tool but it’s a nice command line tool called Azure Cost CLI. I use this tool quite often on different subscriptions, mainly because this tool have some nice advantages:

  • It can be triggered on demand via Azure CLI.
  • It can provide output in different formats, included JSON, Text and Markdown.
  • It can also be used on a Github action

You can quickly install this tool on your Azure CLI with the follwoing command:

dotnet tool install --global azure-cost-cli 

and when installed you can retrieve the cost detail in the following way:

azure-cost accumulatedCost -t Custom --others-cutoff 15 -s YOURSUBSCRIPTIONID

With this command I’m asking to have the details of my Azure Subscription accumulated costs in a custom period starting from the first of the previous month to the actual date and providing details by resource group for 15 resources (than the other will be grouoed in a generic Others group).

The output is as follows:

You can also have cost details by resource. When doing that the output can be very long and the CLI is not so useful. This is the main reason why I love to use this command:

azure-cost costByResource -o text --timeframe Custom > CostByResource.txt

Here I’m redirecting the detail of the costs by resource in a file (I’ve choosed the Text output format in this case). A file will be generated containing the output of the command. Then you can download it from the Azure CLI to your local machine:

And now you can inspect the detailed output:

You can also use the output of this tool in a Github workflow to get the cost of your subscription and store the results in markdown as a Job Summary:

name: Azure Cost CLI Workflow
on:
  workflow_dispatch:
    inputs:
      az-subscription-id:
        description: 'Azure Subscription ID'
        required: true
jobs:
  run-azure-cost-cli:
    runs-on: ubuntu-latest
    steps:
      - name: Azure Login
        uses: azure/login@v1
        with:
          creds: ${{ secrets.AZURE_CREDENTIALS }}
      - name: Install Azure Cost CLI
        run: dotnet tool install -g azure-cost-cli
      - name: Run Azure Cost CLI
        run: azure-cost accumulatedCost -o markdown --subscription ${{ github.event.inputs.az-subscription-id }} >> $GITHUB_STEP_SUMMARY

As said during the training, for me this tool is useful and if you need a quick tool for Azure costs monitoring, this is something that you should test.

This post was originally published on this site

- Advertisement -spot_img

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisement - Advertisement

Latest News

Preferred Solution | New feature | Microsoft Dataverse

Click on the below image to navigate to my YouTube Channel. Please like and share your valuable feedback on this...

More Articles Like This

- Advertisement -spot_img