Continuing the Azure instance reservation topic – when to reserve Azure instances – let us take one further step, how to calculate the reservation ratio and how to control over-reservation which are advocates for exchange or cancellation.
Well supported by Azure portal, check the utilization column of the reservation list. If any of them is below 100%, it is over-reserved. It doesn’t mean that you automatically waste money, in case of dynamic scaling it is possible that over-reservation financially beneficial, need to calculate the saving-waste based on the past and forecasted utilization pattern.
Over-reservations can be exchanged to another instance type or can be cancelled with a fee.
Reserving all vms
The simplest management if all VMs must be reserved, so expected reservation ratio is 100%.
Let’s go to the cost advisor and reserve what is recommended as to buy virtual machine reserved instances.
If you would like to go deeper, review cost analysis which shows currently only pay as you go prices and not the reservation prices. If cost view is filtered for service name “virtual machines” and grouped by “Meter subcategory”, it shows the unreserved compute costs, candidates for reservations. Be careful not reserving spiky capacity usage!
Precisely calculate reservation ratio
There is a way to calculate the exact reservation ratio via Azure CLI and data analysis, how many hours are running with pay as you go and with reservation.
Set some environment variable
SUBSCRIPTION=”my Azure subscription name” DATE=$(date -d "yesterday" +"%Y-%m-%d")
Let’s get usage data – vmId, location, costModel if it is RI or PAYGO, usageQuantity as how many hours was running on the date – for the virtual machines of the defined subscription via Azure CLI and store it in usage.csv
az consumption usage list --start-date $DATE --end-date $DATE \ --include-meter-details --subscription "$SUBSCRIPTION" \ --query "[?meterDetails.meterCategory=='Virtual Machines'].[instanceId,instanceLocation,product,usageQuantity]" --output tsv | sed -e "s|\t|;|g" -e "1ivmId;instanceLocation;costModel;usageQuantity" \ -e "s|;Virtual Machines[^;]*;|;PAYGO;|" -e "s|;VM RI - Compute;|;RI;|">usage.csv
Unfortunately, usage data contains the vm size only for pay as you go rows and not for RIs, also missing OS type. Need to get vm size and OS type from vm list. It is possible only if VMs are not decommissioned in the meantime. You can read it from your own asset management tool. Precisely, vmId, vmSize and osType are got and stored it in vms.csv.
az vm list --subscription "$SUBSCRIPTION" \ --query ".[id, hardwareProfile.vmSize, storageProfile.osDisk.osType]" \ --output tsv | sed -e "s|\t|;|g" -e "1ivmId;vmSize;osType" >vms.csv
Adding vmSize and osType to usage table and pivoting, some lines of python code.
import pandas as pd import numpy as np usage=pd.read_csv("usage.csv", sep=";") vms=pd.read_csv("vms.csv", sep=";") vms.vmSize = vms.vmSize.str.lower() # unfortunately vmSize case not consequent usageWithVmSize=pd.merge(usage, vms, on=["vmId"], how="left") pivot=pd.pivot_table(usageWithVmSize, index=['osType','instanceLocation','vmSize'], columns='costModel', values='usageQuantity', aggfunc=np.sum, fill_value=0) pivot["RIRatio"]=pivot.RI / (pivot.RI + pivot.PAYGO) pivot.sort_values(by='RIRatio').to_csv("azureRIRatio.csv",sep=";")
Voilà! azureRIRatio.csv tell us how many hours was running as pay as you go and as reservation per location – OS type – vm size and calculated the percentage of reservations. Not a big deal creating a dashboard if you are a UI fun, e.g.
To really comparing the cost beneficial and waste, weighting more expensive servers with the appropriate focus, recommended to add the vm cost and calculating the cost ratio.
Some additional considerations.
- Think about extending the usage pattern for longer period, calculating the waste because of not reserved in the past and forecasting the future usage and come up with RI ratio recommendation.
- Windows and Linux are calculated separately here for completeness. They are the same price and reservation in case of enterprise license agreement available, so Azure hybrid benefit used. More simple, more fun!
- Aggregating by vm series considering ratio if instance size flexibility is used to get super simple J dashboard just for series!
Hoping Microsoft is going to announce new features, such as
- Similar dashboard should have been provided by Azure per default.
- VmSize, and Cost information must be added to the az usage output for RI rows, similarly as it exists for pay as you go rows.
- OSType must be part of the product information in az usage output.
- Reservation type (1 or 3 yrs reservation) must be available in the meterSubCategory field of the az usage output.
- VmSize case not consequent, unfortunately depends on how the vm created L, hoping will be fixed soon.