Most cloud providers offer APIs to fetch pricing information, like the charges for virtual machine usage or cloud storage. In this article, we will take a look at the Azure’s Resource RateCard API and Google Cloud Platform (GCP)’s Cloud Billing Catalog API.

Azure’s Resource RateCard API

Azure offer the Resource RatecCard API as a part of the Billing APIs. In the documentation of this API, Azure explains very details on the request/response’s fields and values. However, they do not mention much about the initial setups before we can start using this API, they just say we need an OAuth bearer token obtained from the Azure Active Directory. This is the reason why we might spend hours just to have the appropriate setup.

So, I bet you don’t want to waste your time doing this sh*t. I wrote this to help you find a way, walking through step by step how to get the estimated pricing information of available resources in an Azure subscription with the Azure Resource RateCard API.

First, we will go through all the steps to access the RateCard API, let’s assume that we are on a fresh Azure account.

Inital Setup

Step 1: Install the Azure CLI

First, follow the instruction in this link to install the Azure CLI. We can use the Azure portal as well but I prefer using the CLI. Then, we need to obtain the admin credentials:

Step 2: Register resource provider

From the documentation, we can see that the RateCard API is provided from the “Microsoft.Commerce” resource provider. (For more information, see: Azure resource providers and types)

We will register the “Microsoft.Commerce” with the following command:

Note: You may find some articles say that you need to register other providers like: Microsoft.Compute, Microsoft.Resources, Microsoft.ContainerService,… I strongly state that we only need to register the “Microsoft.Commerce” provider.

Step 3: Create a custom role definition

In this step, we will create a role with the necessary permissions. (See Azure custom role for more information)

Create a JSON file which contains our role definition, name it ratecard-role.json (Replace {SUBSCRIPTION_ID} with your account’s Subscription ID):

Next, register this role definition:

Note: As well as step 2, you also may find some articles say that the custom role need to have many different kind of permissions. And also like in step 2, I strongly state that we only need the “Microsoft.Commerce/RateCard/read” permission, we do not want to give more permission than it needs, right?

Step 4: Register application

Next, we will register an application and associate it with the role we created in the previous step:

The credentials are stored in the my_credentials.json file, which we will use later:

Making API call

In order to make an API call to the RateCard API, we will follow the steps showed in the following diagram:

Step 1: Get access token from Azure Active Directory (Step 1 & 2 in the diagram)

Since Azure does not offer an SDK for the RateCard API, we will need to use a REST client to make API call. For quick demonstration, we will use cURL.

According to the diagram above, first, we will make a client_credential call to the Azure Active Directory to get the access token.

We can get {TENANT_ID}, {CLIENT_ID}, {CLIENT_SECRET” from the my_credentials.json file we generated in previous section.

Step 2: Call the RateCard API (Step 3 & 4 in the diagram)

In the previous step, we have acquired the access token. Now we will use the access token to call the RateCard API:

Note that in the above command, we set the value of OfferDurableId as “MS-AZR-0003P”, which is the Offer ID a pay-as-you-go Azure subscription (See Microsoft Azure Offer Details to get Offer ID match to your Azure subscription). The RateCard API is not supported if you are using an Enterprise subscription and we can see in the documentation that Enterprise account does not have Offer ID either.

It will take several seconds to complete since the response is about 16MB. Although the API has been around for several years, we still only have the option to search for any kind of information that we need in this enormous file. Hope in the future will provide a query mechanism like the AWS’s Price List API. And we have result in the output.json file:

As we can see, the result contains an array of Meter, each Meters is a JSON object. And basically, Azure just give us this enormous 16MB JSON object. In order to get the price group or a specific resource we need to filter inside this object. Therefor, I suggest the following fields of the Meter object that in my opion they are helpful for filtering:

  • MeterRegion: The region in which the Azure service is available.
  • MeterCategory: The category of the meter (“Virtual Machine”, “Virtual Network”,…)
  • MeterSubCategory: The subcategory of the meter (“Ev4 Series”, “IP Addresses”,…)
  • MeterName: The name of the meter, within the given meter category.

On the other hand, the “MeterId” (The unique identifier of the resource) will lead us to the exact product we want. Either way will be just fine based on our needs.

Finally, let explore a bit on the result. For example, we have a Meter object:

Take a look at the Meter Rates and Unit fields, we have the estimated price of the General Block Blob v2 Hierarchical Namespace in the US North Central as follow:

  • First 51200Gb: $0.021/month
  • 52100 – 512000Gb: $0.02/month
  • > 512000Gb: $0.0191/month

GCP’s Cloud Billing Catalog API

The Cloud Billing Catalog API is offer as a part of the Cloud Billing Service of GCP. GCP’s documentation on this API is much clearer and the process is also much simpler. They offer us 3 ways to call this API:

  • Via SDK
  • Via RESTAPI
  • Via RPC API

Well, like most developer will do, we will jump right into using the SDK. If you want use the REST API, I think the documentation is clear enough. And in case you want to use RPC, you can check my article on how to use GCP’s RPC APIs.

Step 1: Authentication

First, we need to authenticate our request in order to use GCP’s APIs (For more information, see: Google Cloud Authentication Overview). In this section, we will use API key to authenticate.

We can create a service account on Google Cloud Console > Credentials page:

Step 2: Get List Services

In this step, we will get a list of GCP’s public services (Compute Engine, Cloud Storage,…), with {API_KEY} is the API key we generated from the previous step:

And we have the result:

Where:

  • name: the resource name for the service.
  • serviceId: the identifier for the service
  • displayName: the human readable display name for the service

We will need the name for the next step.

Note that each call will return only a batch of services. We need to pass the nextPageToken to our code and make a call again and again until the nextPageToken is empty to get the full list of services.

Step 3: Get List SKUs

Now we can get the estimated price of GCP’s service, with {API_KEY} from step 1 and {SERVICE_NAME} is the name from step 2:

Replace {SERVICE_NAME} with the name we have previous step. For example, we replace the {SERVICE_NAME} as “services/6F81-5844-456A”, name of the Compute Engine service, and we have the result:

Pretty similar to the RateCard API, the result contains a skus array (just like the Meters array), each sku is a JSON object. Just like previous step, to get a full list we need make calls until the nextPageToken is empty.

Although GCP have group the skus by service, which make the response much smaller, we still need filtering. Hence, I also have the following fields of the sku object that I think are helpful:

  • serviceRegions: region in which the SKU is offer.
  • description: human readable description of the SKU
  • resourceGroup: a group classification for related SKUs (“RAM, “GPU”, ” InterregionEgress “,…)

And just like “MeterID”, the “skuId” (The unique identifier of the SKU) will lead us to the exact product we want.

Now let explore the result. For example, we have a SKU object:

Look at the usageUnit, units, nanos and aggregationInterval fields we have the estimated price of Network Inter Region Egress from Americas to Sao Paulo as follow:

  • First 1GB: $0/month
  • >1Gb: 80000000/10^9 = $0.08/month

Conclusion

Both RateCard API and Cloud Billing Catalog API return to us a humongous JSON object which needs further processing, which I have suggested some fields in the JSON object that I think is useful for filtering. After walking thourgh this article, I hope you can use the RateCard API and Cloud Billing Catalog API with ease and save a bit of your time exploring this API. Thank for your time!

Le Minh Man – Solution & Technology Unit, FPT Software

Related posts: