Managing bulk Azure Reserved Instances

The Azure logo, with a drawing of Bit the Raccoon looking up at it.

Azure is great for workloads where you want to pay for what you use, as you can save money by switching services off or down-sizing them when you can.

Over time, as you become a more mature public cloud consumer and more workloads move to production, there may be workloads that need to run constantly. How should you manage these costs?


Enter Reserved Instances

Azure has a feature called Reserved Instances (or RI). RI allow you to commit to a usage pattern over a certain time period, a year or 3 years for example. In this article we will cover Virtual Machine RIs, but RIs can be purchased for many different Azure services.

So how would you go about creating a Reserved Instance?


Azure Portal Reservations

The Azure Portal has support for RIs. From here, you can browse, create and modify RIs.

A screenshot of Azure Reservations, taken from the Azure Portal



This is a great way to get started, as the visual aspects of the Azure Portal will help you understand the basic concepts, as well as walk you through the purchase process using a shopping cart approach.

But what if you need to create a lot of RIs? Or create RIs across a number of different subscriptions? The chance of user error clicking through the Portal steps multiple times may be too high.

For these scenarios, I created the Reserved Instance Helper.


Reserved Instance Helper

Within an enterprise, the person responsible for the RIs is probably part of a financial operations team, or FinOps. They may not have programming skills, and unfortunately for them, the only way to access RIs right now is through the Portal, PowerShell, Azure CLI or REST APIs.

The FinOps team are most likely more comfortable working with Excel sheets than the command line, which is why the Reserved Instance Helper is designed for this kind of team. The idea is to use an Excel spreadsheet as the source, making it easy to capture the details of each Virtual Machine RI in a worksheet row.

I created a PowerShell script to read each row and make the necessary calls to create the RI. To create an RI there are two calls: one to calculate the RI, which creates an Order ID, then purchase the RI referring to the Order ID from the previous step.

A screenshot of the Reserved Instance Helper spreadsheet and PowerShell execution.



The small print

There are a few implementation details to be aware of when working with RIs. Firstly, the name of the RI is free text – there is nothing to prevent you from creating two RIs with the same name. Azure knows they are different because they have unique Reservation IDs, but we poor humans may find it harder to work with multiple RIs with the same name.

My spreadsheet has an Excel formula to create a name for each RI based on the row in the worksheet. There are also lookups for each of the RI attributes that is based on static values.

Because the RI name isn’t unique, it would be all too easy to run the spreadsheet multiple times and thereby create multiple copies of the same RI. To prevent this, I added a Purchased copy and set this to Yes after the RI has been created from the row.


Wait, there’s more!

There is something to be aware of when using the command line tools to create RIs, and it’s that the command to purchase the RI actually triggers a workflow. As well as creating the RI, the workflow assigns access rights so that the creator can access the RI. Creating this access right is an asynchronous task, sometimes it completes before the purchase command returns and you don’t notice. Other times it takes longer to complete and hasn’t completed yet when the purchase command returns. In this situation, an error message is returned.

This might be unsettling as a user, but fear not – when you go to the Azure Portal you will see that the RI has been purchased. In this situation, I considered waiting for the asynchronous access rights assignment to complete by getting the newly purchased RI, but in testing this sometimes took as long as 30 minutes. I figured that a user creating 50 RIs wouldn’t want to wait 25 hours for the script to complete, so right now just accept the errors in the feedback and check the output in the Azure Portal when you are done.

If you want to see the utility in action, I made a short video:

I hope this is useful – let me know if it has helped you and enjoy the benefits of Reserved Instances! You can view the source for the Reserved Instance Helper over on GitHub.