1

I'm looking at the documentation of the rest API and I'm looking to get the costs of my resources.

Looking at both the REST API and SDK for .NET, there's a Consumption endpoint that does this, but on further testing, this only seems to provide the latest available billing cycle (24 hour window).

Example Code:

            var filterString = $"properties/instanceName  eq \'{vm.VirtualMachineName}\'";

            var vmConsumptionData = await consumptionClient.Item1.UsageDetails.ListAsync(
                    scope: $"/subscriptions/{consumptionClient.subscriptionGuid}",
                    filter: filterString 
                );

consumptionClient is an instance of a consumptionClient returned as part of a Tuple elsewhere.

Result is yesterdays last billed amount.

Are there any other endpoints that would provide a more up to date cost overview of my resources in Azure? I.E. Per minute or hour

The only other way I believe this can be done is to retrieve the 'Usage quantity' of a resource and work this out based on the resources cost per hour.

Dandy
  • 1,466
  • 16
  • 31
  • Can you please post an example API call you made? Linked Consumption API seems to be odata based with support for billing period filter which shows data going back upto 1st May 2014. – Abhay Saraf Feb 01 '18 at 10:11
  • @AbhaySaraf Added an example of my code to the question. As an example, I'm grabbing literally everything about a virtual machine from the beginning of its existence. This provide everything up to the last billing date (yesterday). – Dandy Feb 01 '18 at 12:23
  • There is a API to Get price and metadata information for resources used in an Azure subscription:https://msdn.microsoft.com/en-us/library/mt219004.aspx – Wayne Yang Feb 02 '18 at 03:15
  • @WayneYang-MSFT I believe RateCard are only available per resource type, not a breakdown of each specific resources (I.E. This shows all Virtual Machines, as opposed to, each virtual machines meter cost). Whereas I'm trying to detail a better breakdown. Looking at the azure dashboard though, even the billing here only gives a 24hr Window. – Dandy Feb 02 '18 at 03:51
  • Yep, As I known, there is no such API to one specific resource – Wayne Yang Feb 02 '18 at 04:15

1 Answers1

1

As of Mar-06-2020 documentation on Microsoft.CostManagement/Query worked:

POST https://management.azure.com/subscriptions/{subscriptionId}/resourcegroups/{resourceGroupName}/providers/Microsoft.CostManagement/query?api-version=2019-11-01&$top=10000
Content-Type: application/json
{
    "type": "ActualCost",
    "dataSet": {
        "granularity": "None",
        "aggregation": {
            "totalCost": {
                "name": "PreTaxCost",
                "function": "Sum"
            },
            "totalCostUSD": {
                "name": "PreTaxCostUSD",
                "function": "Sum"
            }
        },
        "filter": {
            "Dimensions": {
                "Name": "ResourceId",
                "Operator": "In",
                "Values": [
                    "{resourceId}"
                ]
            }
        }
    },
    "timeframe": "Custom",
    "timePeriod": {
        "from": "2020-02-29T00:00:00+00:00",
        "to": "2020-03-06T23:59:59+00:00"
    }
}

with response:

{
    "id": "subscriptions/{subscriptionId}/resourcegroups/{resourceGroupName}/providers/Microsoft.CostManagement/query/XXXXXXXX-9ab4-XXXX-83fe-XXXXXXXXXXXX",
    "name": "XXXXXXXX-9ab4-XXXX-83fe-XXXXXXXXXXXX",
    "type": "Microsoft.CostManagement/query",
    "location": null,
    "sku": null,
    "eTag": null,
    "properties": {
        "nextLink": null,
        "columns": [
            {
                "name": "PreTaxCost",
                "type": "Number"
            },
            {
                "name": "Currency",
                "type": "String"
            }
        ],
        "rows": [
            [
                1234.5678910,
                "CAD"
            ]
        ]
    }
}

I found this returns accurate value for my specific resource.

NOTE: Only concern is that this API endpoint is flagged as -legacy in the documentation. Not sure if it will be supported long term but works for now.

Rohit Mistry
  • 113
  • 3
  • 11