How to get cost per resource using Azure REST API?

Aditya Thebe 36 Reputation points
2023-05-04T06:31:52.77+00:00

I want to get a very granular report on the cost for each resource. Example: IF there are 20 virtual machines, I need to know cost incurred by each of those 20 individually

The response would need to have this information at least

For subscription xxx-xxxx, the virtual machine of resource type microsoft.compute-virtualmachines with name tailscale cost is $5/week.

I have checked cost management API, Consumption and BIlling API but nothing provided me this fine grained information.

Azure Cost Management
Azure Cost Management
A Microsoft offering that enables tracking of cloud usage and expenditures for Azure and other cloud providers.
2,036 questions
{count} votes

2 answers

Sort by: Most helpful
  1. SadiqhAhmed-MSFT 37,686 Reputation points Microsoft Employee
    2023-08-18T14:29:04.2233333+00:00

    @Chandra Reddy, Pathapati Venkata Umesh (Cognizant) Query API should be able to solve your needs: Query - REST API (Azure Cost Management) | Microsoft Learn

    You can groupby and /or filter on specific services and resources.

    However, there is a strict rate limit on the Query API. Functionally speaking, the Query API definitely can do that, but doing multiple calls on the same subscription and filter on different values like Tags / Service Name, etc. And by doing that, you hit a lot of entity and tenant rate limit. 

    For your scenario, you may do something like a loop through all the service names and call the Query API to filter the cost by the service name and get the desired data you are looking for. 

    In that way, I doubt you will accept to add 30 seconds wait time between each call since that will cause the script to run much longer time.

    Recommended for your scenario:

    Can you evaluate CostDetails API - Generate Cost Details Report - Create Operation - REST API (Azure Cost Management) | Microsoft Learn ?

    You can get resource costs for all resources under the subscription and can do any client-side filtering.

    For the information on the fields:

    Understand usage details fields - Microsoft Cost Management | Microsoft Learn

    Hope this helps. Let us know if you need any further assistance.


    If the response helped, do "Accept Answer" and up-vote it

    1 person found this answer helpful.
    0 comments No comments

  2. SadiqhAhmed-MSFT 37,686 Reputation points Microsoft Employee
    2023-05-04T12:52:53.0933333+00:00

    Hello @Aditya Thebe Thank you for reaching out to us on Microsoft Q&A platform. Happy to help!

    To get the cost per resource using Azure REST API, you can use the Azure Cost Management and Billing REST API. This API allows you to programmatically retrieve cost and usage data for your Azure resources.

    Here's an example of how to use the Azure Cost Management and Billing REST API to retrieve the cost per resource:

    First, you need to get an API key for the Azure Cost Management and Billing REST API. You can get the API key by following the instructions in the Azure Cost Management and Billing REST API documentation.

    1. Once you have the API key, you can use it to make a REST API call to the Azure Cost Management and Billing REST API. Here's an example of the API call to retrieve the cost per resource:
    GET https://management.azure.com/subscriptions/{subscriptionId}/providers/Microsoft.CostManagement/query?api-version=2019-11-01&$filter=properties/UsageStart ge '2022-01-01' and properties/UsageEnd le '2022-01-31' and properties/ResourceGroup eq 'myResourceGroup'&$select=properties/UsageStart,properties/UsageEnd,properties/ResourceGroup,properties/ResourceName,properties/ResourceLocation,properties/ConsumedQuantity,properties/UnitPrice,properties/Cost
    

    In this example, the API call retrieves the cost per resource for the month of January 2022 for all resources in the "myResourceGroup" resource group. The API call also specifies that the cost should be returned in USD.

    1. Once you make the API call, you will receive a JSON response that contains the cost per resource for the specified time period. Here's an example of the JSON response:
    {
        "id": "/subscriptions/{subscriptionId}/providers/Microsoft.CostManagement/query",
        "name": "query",
        "type": "Microsoft.CostManagement/query",
        "properties": {
            "columns": [
                {
                    "name": "properties/UsageStart",
                    "type": "DateTime"
                },
                {
                    "name": "properties/UsageEnd",
                    "type": "DateTime"
                },
                {
                    "name": "properties/ResourceGroup",
                    "type": "String"
                },
                {
                    "name": "properties/ResourceName",
                    "type": "String"
                },
    

    Hope this helps. Feel free to ping with questions if you need furhter assitance!


    If the response helped, do "Accept Answer" and up-vote it