Using Retail Rates Prices API to get retail prices for all Azure virtual machines

As an Azure Solutions Architect, I frequently have to generate BOM (Bill of Materials) for Azure resources and large details can result in many hours of using the Azure Calculator to create estimates. This is why I’ve constantly created tools that could automate tedious tasks that require a lot of time and prone to errors. One of the tools I’ve used most over the past while is my Python script that automatically creates an estimate for me on the Azure Calculator as described in one of my previous posts:

Automating the creation of Azure Calculator estimates with Selenium and Python

The above has made estimates with 500+ virtual machines a much better experience.

Another common activity I’ve had to perform is to compare pricing for different virtual machines so I can work with clients to understand the impacts of decided on the SKU. To help with this activity, I have a python script that allows me to use the Retail Rates Prices API (https://learn.microsoft.com/en-us/rest/api/cost-management/retail-prices/azure-retail-prices) to quickly retrieve the cost of different Azure Virtual Machines so they can be compared. The following are 3 scripts I’d like to share:

Get-Azure-VM-SKUs.py
https://github.com/terenceluk/Azure/blob/main/Azure%20Price/Get-Azure-VM-SKUs.py

This script will have you interactively sign on into Azure with a subscription specified in the script. Then proceed to retrieve all the VM SKUs available then export to a CSV file named: Azure-VM-SKU-List.csv

Retrieve-Azure-VM-Cost-With-Input-Output-JSON-And-CSV.py
https://github.com/terenceluk/Azure/blob/main/Azure%20Price/Retrieve-Azure-VM-Cost-With-Input-Output-JSON-And-CSV.py

This python script prompt for the following values in the console:

  1. currencyCode
  2. armRegionName
  3. armSkuName

Sample of these are:

  1. CAD
  2. canadaeast
  3. Standard_D2_v4 or Standard_E2s_v5

To retrieve the various prices for reservations, consumption, low priority, spot.

Then output it to the console in JSON and export to CSV named Azure-Single-VM-Price.csv adding a column for the monthly cost based on 730 hours.

Retrieve-Azure-VM-Cost-With-CSV-Reference.py

This python script will read an existing CSV file named Azure-VM-Input.csv with the columns:

  1. currencyCode
  2. armRegionName
  3. armSkuName

Sample of these are:

  1. CAD
  2. canadaeast
  3. Standard_D2_v4 or Standard_E2s_v5

To retrieve the various prices for reservations, consumption, low priority, spot.

Then output it to the console in JSON and export to CSV named Azure-VM-Price-Output.csv adding a column for the monthly cost based on 730 hours.

Hope these scripts help anyone who might be looking for reducing some of the time required for these activities. Feel free to modify them as required.