4

In Azure portal ,if one subscription is selected ,the cost analysis can be viewed like the following screenshot

Azure Cost Analysis

I want to programmatically fetch the information the like the one displayed above may be using using some python SDK API/REST API. If anybody has any experience/idea on this ,please help.

After going through replies ,I have gone through the Azure Billing Rest API and I am now able to call the Usage Aggrgate and RateCard related Rest APIs.

Following are the results of those REST Calls. .Azure Billing Usage Aggregate Response

Azure Billing Ratecard Response

But honestly speaking ,I still have not figured out how these to would give me detailed view like the cost analysis does where for each resource how much cost associated can be displayed.Actually I am very new to Azure probably that is why I am missing the link some where .

Can somebody give some hint here ?

Soumen
  • 121
  • 1
  • 3
  • 14

3 Answers3

1

There are the offical documents below for retrieving the billing data using Python SDK or REST API in Python.

  1. For using Python SDK, please see http://azure-sdk-for-python.readthedocs.io/en/latest/resourcemanagementcommerce.html.
  2. For using Billing REST API, please see https://learn.microsoft.com/en-us/rest/api/billing/, and you can try to use Python package requests to get these data.

However, I think you may have know these above, the key is that you need to follow the tutorial Manage access to billing information for Azure using role-based access control to get the role permission via your account admin.

And then you may also need to register a client app to get the client id for Resource Management Authentication if you want to use Service Principal/ADAL (not AD User/Password) in Python SDK or use REST API to do the same authentication as Azure REST API Reference page said. For the authentication topic, you can refer to the content map of Manage Apps to know more if you have been getting some trouble.

Hope it helps. Any concern, please feel free to let me know.

Peter Pan
  • 23,476
  • 4
  • 25
  • 43
  • 1
    Billing as a SDK now, no need for requests :) https://pypi.python.org/pypi/azure-mgmt-billing. FYI too https://pypi.python.org/pypi/azure-mgmt-consumption. Note finally that this is plugged inside the CLI. If you get what you want with CLI cmdlet, this means that you can get the same in Python SDK. – Laurent Mazuel Jun 12 '17 at 19:54
  • Edited my initial question after I made some progress with Azure Billing REST APIs on Usage Aggregate and Ratecard.But still have doubts .Updated the initial question accordingly – Soumen Jun 15 '17 at 17:44
  • Any input on how usage aggregate and ratecard API would give detailed view like the cost analysis does where for each resource how much cost associated can be displayed ? Please let me know . – Soumen Jun 21 '17 at 04:38
1

If you already have the usage and the ratecard data, then you must combine them. Take the meterId of the usage data and get the related ratecard data. The ratecard data contains the MeterRates and the IncludedQuantity which you must take. There are probably multiple meter rates and the included quantity because there are probably different costs per usage (e.g. first 10 calls for free, 3 GB for free, ...). The consumption starts/is reseted at the 14th of the month. That's the reason why you have to read the data from the whole billing period (begins with 14th of each month), because that's the only way how you get the correct consumption.

So, if you are using e.g. Azure Functions and you have a usage of 100.000 units per day and you want the costs from 20th - 30th, then the calculation works as follows: read data from 14th - 30th. These are 17 days and therefore it used 1.700.000 units. The first 400.000 are for free = IncludedQuantity (so in this sample the first 4 days). From the 400.001 unit on, you have to take the meter rate (0,0000134928 €) and calculate the costs. 1.300.000 * 0,0000134928 = ~17,54€. Fortunately, the azure functions have only one rate. If the rate changes e.g. after 5.000.000 units, then you also have to take this into account. If you have the whole costs, then you can filter on your date which is 20.-30. and you will get the result.

That's the short explanation of the calculation. I implemented this calculation in C# and published it as a NuGet package. The source code is on github - probably it helps. It also contains a sample console which you could use to export the data.

armin
  • 66
  • 4
  • Hey, That is good info. Could you tell me where did you get the information about how to do this calculation for different meter rates? I searched in the documentation but I could not find it. It would be great if you could share your source. Thanks. – shwetaOnStack Jan 31 '18 at 12:01
  • 1
    I guess there is no documentation. When I wrote the code, I found it out by myself. I checked the API and the data that I get from the API. The API overview contains a short information, that you need to combine the ratecard and usage api data, so I thought about what would make sense for me and implemented it that way. I doublechecked the result with my bill and it matched. Only link I can provide is: https://msdn.microsoft.com/en-us/library/azure/mt218998.aspx – armin Feb 01 '18 at 12:55
1

I have the same issue. But unluckily, python SDK is too hard to use.

Moreover, you cannot find an availabale sample or example on Google.

So, I choose using restapi rather than python SDK.

With python code, you can do this firstly,

import requests
from azure.common.credentials import ServicePrincipalCredentials

and set headers pyload and url.

 headers = {
     "Content-Type": "application/json",
     "Authorization": <token> }

you can get token through credentials, which generated by client_id,secret,tenant. credentials.token() will return the token you can use in headers.

you can find restapi in https://learn.microsoft.com/en-us/rest/api/ or use F12 in Chrome when you access the Azure dashboard.

Cheng
  • 11
  • 1
  • 2
    This helps explain how to authenticate with the Azure APIs, but it doesn't answer the question (how to programmatically obtain cost/billing data). – Paul J Apr 03 '20 at 14:57
  • @PaulJ You could find the cost/biliing data on the web, and see the cost URL by F12 in Chorme. And then, find the URL in API Exploer of Azure, and using the Python requests to obtain cost/billing data – Cheng Apr 13 '20 at 02:59
  • Once we have credential, we could call any api in Azure by Python requests. – Cheng Apr 13 '20 at 03:01
  • The Python SDK basically gets information via REST requests anyway. Using the Python SDK it is just easier to login with `az login` and then get the client via `get_client_from_cli_profile(ResourceManagementClient)` – Dirk R Jul 20 '20 at 09:14