How to start or stop automatically an azure VM to optimize cost
Context
We use Virtual Machines
very often on Azure, for development, testing, building, etc.
Though these kind of VM might not be used all the time, especially very late in the night or during the weekend.
So one easy cost optimization could be to turn off these VM during those periods to save Azure Compute, and money :)
There are many ways to achieve that, but one of my favourite is to use Azure DevOps
, and especially the Azure Pipelines
part to do it, mainly because it gives me :
- A very easy and simple integration with Azure
- Traceability on the Start and Stop executions
- Fine tuned scheduling (which days, what time, many times per day, etc.)
If you are already familiar with Azure CLI
, you could configure an auto Start/Stop workflow quite easily, but as I released a free extension for this purpose, let’s see how to use it!
Install Azure Virtual Machine Manager Task extension
You can go directly to the extension page Azure Virtual Machine Manager Task or look for it on the Marketplace.
From the extension page, click on the Get it free
button, and you should be redirected to your Azure DevOps organization
selection page.
From there, you have many options:
- If you have the permissions to manage extensions, you should be able to select your organization and start the installation immediatly.
- If you don’t have the permissions, you can request the extension to be installed (Your organization administrators should then receive a notification email)
- If you are
On-Premise
(Azure DevOps Server 2019), you have to download and install it
You can then create a new Release definition
on the Azure Pipelines
and start configuring it.
Configure the Release scheduling
For this part, you can leverage the scheduling functionalities already existing in the Release triggers:
See Scheduled release triggers
Use the Azure Virtual Machine Manager task in an Azure Pipeline Release
In your environment, you can add a new task and select the Azure Virtual Machine Manager
task from the Utility
tab:
Start or Stop Azure VM with Azure Virtual Machine Manager task
The configuration should be pretty straightforward:
- Action: Choose to
Start
orStop
the VM. - Azure subscription: The azure subscription where the VM is located .
- Resource group: The resource group where the VM is located.
- Virtual machine name: The name of the virtual machine.
Once everything is configured, you can start a Release manually to check that everything is working correctly!
Congratulation :)
Feel free to give me any feedback (problem you could encounter or feature suggestion) in the comments or on Twitter!
May the code be with you!