Posted in:

I recently needed to investigate an unexpected increase in the Azure bill for a subscription. The Azure Portal does actually offer some quite good breakdowns of cost, but in this case I wasn't able to access the cost view in the portal, so I turned to the excellent Azure CLI for help.

Retrieving consumption usage metrics

The command we need is az consumption usage list. You need to pass your subscription id or name, and the start and end date you want to analyse.

az consumption usage list --subscription "My Subscription" `
          --start-date "2019-11-01" --end-date "2019-11-28"

This returns a large amount of JSON with a breakdown per day of your usage of all the various billing metrics.

Usage information

Each entry in the JSON contains a whole host of useful information about your consumption. Here's some of the key fields:

  • consumedService - (e.g. 'Microsoft.Storage', or 'Microsoft.Web')
  • instanceId - the identifier of the Azure resource it relates to
  • instanceName - the name of the Azure resource
  • instanceLocation - (e.g. 'EU West')
  • meterId - this is a GUID referring to what is being measured. Unfortunately, I don't what the best place to look up the meaning of each meter (although I did find this spreadsheet). There is also an az consumption pricesheet command but it wouldn't return any data for my subscription.
  • product - not always filled in unfortunately, but where available, seems to essentially be the meter name (e.g. "Bandwidth - Data Transfer Out - Zone 1", or "Virtual Machines Dv2/DSv2 Series Windows - D3 v2/DS3 v2 - EU West")
  • usageStart and usageEnd - shows you what day the usage relates to
  • usageQuantity - how much you've used. The unit of measure depends on the meterId. For example, it might be a number of GB for data transfer.
  • pretaxCost - not shown for all subscription types, but where available tells you the cost for that item. I presume it shows using your preferred billing currency, which for me is GBP.
  • tags - if you're tagging your resources (which is a recommended practice), then they are shown here as well, allowing you to easily filter for all related resources.

Analysing usage

I wanted to be able to sort and filter the data in Excel, so I used the following PowerShell to create a CSV file including column headings for easy sorting and filtering:

(az consumption usage list --subscription "My Subscription" `
    --start-date "2019-11-01" --end-date "2019-11-28" `
    | ConvertFrom-Json) | ConvertTo-Csv -NoTypeInformation `
    | Set-Content "MyUsage.csv"

This helped me to easily track down exactly which resources had jumped in price, and when the price change happened.

Having this command available via the Azure CLI also makes it very easy for you to perform your own ad-hoc automated queries on Azure usage, to track trends or detect anomalies.

Want to learn more about the Azure CLI? Be sure to check out my Pluralsight course Azure CLI: Getting Started.