azurecost-management

I would like to know the Azure cost used last month by calling the Azure Cost Management API


I would like to know the Azure cost used last month by calling the Azure Cost Management API.

The following APs were called.

https://management.azure.com/subscriptions/00000-00000-00000-0000-00000/providers/Microsoft.CostManagement/query?api-version=2023-11-01

The header information will be omitted here. The values in the header information are correct because this month's rate information is well imported. Here is the body information in JSON format required for POST calls, which is the problem.

{
    "type":"ActualCost",
    "dataSet":{
    "granularity":"None",
    "aggregation":{
    "totalCost":{
    "name":"Cost",
    "function":"Sum"
    }
    },
    "grouping":[
    {
    "type":"Dimension",
    "name":"SubscriptionId"
    }
    ]
    },
    "timeframe":"TheLastMonth".   <===== This is a problem.
}
    

Calling in the above format results in the following error.

"Invalid query definition, timeframe TheLastMonth is currently not supported.

If you modify the time to Custom format, it comes out well, but I don't want to do that. Is there any way?


Solution

  • It seems like there is a bug with "timeframe": "TheLastMonth".
    I also reported it (again) here: https://github.com/Azure/azure-rest-api-specs/issues/27650

    Have you checked your billing cycle on your subscription? If it is the first day and the last day of the month, then the value returned by TheLastMonth and TheLastBillingMonth will be equal, as we are looking at the same timeframe.

    Tried both with Postman and PowerShell, same result.

    $aggregation = @{
      totalCost = @{
        name     = "Cost"
        function = "SUM"
      }
    }
    
    $grouping = @{
        name      = "SubscriptionId"
        type      = "Dimension"
      }
    
    Invoke-AzCostManagementQuery -Type ActualCost -Scope "subscriptions/xxxxx" -DatasetGranularity 'None' -Timeframe MonthToDate -DatasetAggregation $aggregation -DatasetGrouping $group