Azure Cost Management JSON Body Examples

Cost management isn’t very fun, but is vital to any cloud deployment. Without it, your costs can quickly spiral out of control. In this post I’ll provide Cost Management JSON Body examples, because I find the examples in the official docs overly complex and inadequate. If you’ve followed my blog or seen me present you know I like to start with simple examples first and then build on them.

To query Cost Management, we can of course use PowerShell, however in this instance we’ll not be using the PowerShell module. As there are instances where you’ll want or need to query Cost Management outside of PowerShell.

Query Scope

First, you’ll need to know the query string and how to set its scope.

POST https://management.azure.com/{scope}/providers/Microsoft.CostManagement/query?api-version=2019-11-01

In the brackets is where you can set the scope of what you want to query from Azure Cost Management. For example to query just your subscription you could put /subscriptions/{subscriptionID}/

like so

POST https://management.azure.com/subscriptions/{Subscription:id}/providers/Microsoft.CostManagement/query?api-version=2019-11-01

You can also set other scopes like resource groups and billing account scope. To see the full list of scopes you can check out the docs here. Query – Usage (Azure Cost Management) | Microsoft Docs

JSON Body Examples

All these examples will be using just Subscription scope from above.

To get back just current month to date billing for your entire subscription.

{
    "type": "Usage",
    "timeframe": "MonthToDate",
    "dataset": {
      "granularity": "None",
      "aggregation": {
       "totalCost": {
          "name": "PreTaxCost",
          "function": "Sum"
        }
      },
     "grouping": [
        {
         "type": "Dimension",
          "name": "SubscriptionId"
        }
      ]
    }
  }

This will return back 3 rows, pre tax cost, currency by subscriptionId you provided. Even with this simple JSON body we have several options to customize what we return. For instance for timeframe we can use: BillingMonthToDate, Custom, MonthToDate, TheLastBillingMonth, TheLastMonth, WeekToDate.

For dimension we can ask Cost Management to give us back the total cost by SubscriptionId, which is what is in the example above. We can also return cost by resource group or even resource type.

{
    "type": "Usage",
    "timeframe": "MonthToDate",
    "dataset": {
      "granularity": "None",
      "aggregation": {
       "totalCost": {
          "name": "PreTaxCost",
          "function": "Sum"
        }
      },
     "grouping": [
        {
         "type": "Dimension",
          "name": "resourceGroup"
        }
      ]
    }
  }

By changing the dimension to resourceGroup, this will return back the pre tax cost and currency for every resource group specified in the scope.
We can also change the dimension to resourceType and get back a cost broken down by each resource type.

I suspect these two scenarios cover a decent amount of needs. However, we can definitly get more advanced as there is a filter function.

{
    "type": "Usage",
    "timeframe": "MonthToDate",
    "dataset": {
      "granularity": "None",
      "filter": {
                  "dimensions" : {
                      "name" : "resourceType",
                      "operator" : "In",
                      "values" : [
                         "microsoft.sql/servers"
                      ]
                  }
      },
      "aggregation": {
       "totalCost": {
          "name": "PreTaxCost",
          "function": "Sum"
        }
      },
     "grouping": [
        {
         "type": "Dimension",
          "name": "SubscriptionId"
        }
      ]
    }
  }

Adding filter underneath granularity allows us to filter by dimensions, tags and more. In the above example I added a filter that will return the cost for all SQL Servers by subscription.

{
    "type": "Usage",
    "timeframe": "MonthToDate",
    "dataset": {
      "granularity": "None",
      "filter": {
                  "tags" : {
                      "name" : "Environment",
                      "operator" : "In",
                      "values" : [
                         "Dev",
"Prod"
                      ]
                  }
      },
      "aggregation": {
       "totalCost": {
          "name": "PreTaxCost",
          "function": "Sum"
        }
      },
     "grouping": [
        {
         "type": "Dimension",
          "name": "SubscriptionId"
        }
      ]
    }
  }

These are some basic Cost Management JSON body examples to get started with. There are much more complicated examples at the docs here Query – Usage (Azure Cost Management) | Microsoft Docs

The next thing we can do is maybe query cost management from Azure Workbooks.