Azure : Billing Automation / Integration via the REST

Introduction
If you are an internal service provider needing to do chargebacks, or get an insight into your spending. Or if you are a cloud service provider in need to bill your customers for their Azure usage… You’ll find your self in need to get the raw usage data of your subscription(s). So today we’ll delve into getting your usage data from Azure via the REST api.

kvaes-azure-billing-consumption-usage-pricing-data-information

 

Environment Setup
For today’s post, we’ll be using “postman“. A very nice tool suggested by a co-worker of mine (Robin) and it’s really user-friendly to work with! What does the tool do? Basically it’ll let you craft REST calls without the need for custom scripts / coding effort. Why do we need it? Because we’re going to need to do authentication with each call, and let’s say… It isn’t that straightforward if you aren’t accustomed with it (like me at this time). šŸ™‚

In order to set up your environment, I would suggest that you go through the following guide. So basically setup your environment …
2016-09-01 13_14_56-Postman
and your header preset ;

2016-09-01 13_15_18-Postman

 

Getting usage data

So now that we got the annoying / hard part out-of-the-way, let’s fire up some calls… But to which APIs? It depends on which contract is underneath the system and what you want to achieve ;

For this post, I’ll be using the “Enterprise Agreement” one, as this is most commonly used. The URL we’ll be using in the following ;

https://management.azure.com/subscriptions/{{subscriptionID}}/providers/Microsoft.Commerce/UsageAggregates?api-version=2015-06-01-preview&reportedStartTime=2016-07-01T00%3a00%3a00%2b00%3a00&reportedEndTime=2016-08-01T00%3a00%3a00%2b00%3a00&aggregationGranularity=Daily&showDetails=false

So whatĀ does that look like?

2016-09-01 13_20_46-

We selected the header preset & environment as we configured earlier on. We setup the variable for the subscriptionID in the url and changed the dates… After having pressed “Send”, we notice that it took some 20s to return the result. A nice JSON file with all the usage information!

 

Getting pricing data

Now we know how much we have consumed, though what are we paying per unit? By combining the consumption with the pricing data, we will be able to construct our billing.

For this we’ll be using the RateCard API, which will provide us with the price per “metric” ;

https://management.azure.com/subscriptions/{{subscriptionID}}/providers/Microsoft.Commerce/RateCard?api-version=2015-06-01-preview&$filter=OfferDurableId eq 'MS-AZR-0121p' and Currency eq 'EUR' and Locale eq 'en-US' and RegionInfo eq 'BE'

So in this URL we changed the subscription id, but in the filter we’ve also setup several parameters to reflect the conditions I want to receive my RateCard in. How does that look?

2016-09-01 13_50_06-Postman

And yet again we get a nice JSON file which we can use!

 

Combining to usage & pricing data to billing information

The next step will be to parse the both files and create usable data for billing purposes. As a bit of a disappointment probably, I’ll be postponing this part to a future post… Most importantly because parsing these JSON files isn’t something that goes smoothly. My first crack at it with PowerBI wasn’t something I can call a success! šŸ˜‰

 

TL;DR

  • Postman is a good tool to quickly help you during the development process.
  • UsageĀ & Ratecard data can be obtained via a REST api.
  • Authentication is a bit tougher than what you are probably accustomed to.
  • Combining the UsageĀ & RateCardĀ data via the JSON output is more challenging that I would have expected.

 

Advertisements

One thought on “Azure : Billing Automation / Integration via the REST

  1. This tutorial is really awesome, using post man for the tutorial makes it each to be integrate with any platform that can call http. but like the said the final product is the billing most especially am pretty much interested in tracking my azure balance before the end of the month. I know this is pretty much prerequisite to archiving that. i need and head up approach to how i can archive that with is two api. Thanks will be dropping my mail below.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s