Why would you download the Azure Retail Prices REST API?
Because currently the Azure Cost Management only shows you your expenses. Before buying Reserved Instance you want to have an idea about how much you are going to save. Buying Reserved Instances or Savings Plans is still a bit of a jump in the void.
Wow, I cannot believe Microsoft doesn’t provide an overview of how much you are going to save before you buy Reserved Instances. That would actually help them sell Reserved Instances!
Indeed.
So how we can do that?
My first attempt was to use Power BI and a direct query to the Azure Retail Prices REST API. That failed miserably and I cannot tell why: the API uses pagination with a NextPageLink
token but even catching that token doesn’t bring to a result; Power BI keeps lading data forever and than fails.
The second approach was to use Python to export the who API to a CSV and that was rather successful. This is the code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
import requests import pandas as pd url = "https://prices.azure.com/api/retail/prices" all_data = [] while True: print(url) data = requests.get(url).json() all_data.extend(data["Items"]) if data["NextPageLink"]: url = data["NextPageLink"] else: break df = pd.DataFrame(all_data) print(df.head().to_markdown(index=False)) df.to_csv("data.csv", index=False) |
OK, now we have it on a CSV, what to do next?
My approach was to import the CSV file to an Azure SQL Database.
Then you can compare what you actually pay ( Azure Cost Management connector) to what you could pay if you were using Reserved Instances.
Be careful because the API has two major problems:
- doesn’t have a unique identifier for resources
- it lacks consistency
I don’t understand, explain me that, please.
If you want to query the API for a particular resource you have to use different queries.
Let me give you some examples:
- Azure App Services: uses
[serviceName],' - ',skuName,' - ',[location]
- Azure SQL Database: uses
productName,' - ',meterName,' - ',[location]
- Virtual Machines: sometimes uses
serviceName,' - ',skuName,' - ',[location]
, sometimes uses[serviceName],' - ',meterName,' - ',[location]
I created a T-SQL query that pivots the costs and creates a unique row for each resource.
You can download it here: Azure Retail Prices REST API
I haven’t investigated all resource type but there might be more lack of consistency.
I contacted the Azure Support and they confirmed that there is no unique identifier and no consistency between resources.
Good luck.