This document describes in headlines how to setup automated start and shutdown of Azure VMs. To go through the steps, you are required to have access to the management portal at https://portal.azure.com and administration rights.
By shutting down the servers when not used there is a noticeable amount to be saved.
Prerequisites
Automation Account
To run the automation scripts, you need an Automation Account. Notice, that the account must be created under the same subscription as the servers that should be managed by the account.
The fastest way to locate the accounts is to search for the Automation Accounts service:
To create a new account, click the Add button:
Give the account a name, specify a resource group (or create a new) and a location:
When created (it takes a moment), select the new account:
Shut down servers
Runbooks
All activity is kept in runbooks. The account comes with 4 default runbooks which we for this scope leaves as is.
We are going to create our runbooks based on a template that gives us the functionality we need out of the box. To do this click Browse gallery:
The list of available templates is long. The first one we are going to use is the “Stop Azure V2 VMs”.
When we click on it we can see a graphical presentation of the flow. This is not relevant to go through in this document. Click the Import button to use the template:
Give the new runbook a name and a description:
After the runbook has been created, click Edit:
From here we can change the runbook including default values for the runbook. We can also test the runbook with different values.
In this case we are not going to change anything so we publish it right away by clicking the Publish button:
Scheduled runs
Next step is to schedule the runbook to run every afternoon. To do this click the Schedule button:
To do this we need to things: A schedule and the parameters to run the runbook with. First the schedule:
In this scenario we want the VMs to be stopped every day at 18:00:
To run the runbook we need to specify the resource group name and the name of the VM.
This information is found on the information page of the VM in the Azure portal.
Add it to the parameters:
Now the scheduler will shut down the server matching the parameters every day at 18:00.
If you leave the VM name blank all servers in the resource group will be affected.
Starting VMs
To create a runbook to start the VMs go to the gallery and select the Start Azure v2 VMs template:
Give it a name and description:
Click edit to access the runbook just created:
Publish the runbook. Again we do not want to change anything:
Create a schedule to start the VM every working day at 7:00:00. In this case we don’t start them up in the weekends; but if somebody starts a VM it is automatically shut down by the previously created runbook.
You just saved a lot of money on the VM…
Create scheduled runs for every VM you can shut down at night.
Many Thanks.
Glad I could help and thanks for reading my blog. 🙂
Finally, a method that works! 🙂
Hello SØREN RASMUSSEN,
Thanks for sharing, it’s really useful and configure in very simple method. how can I exclude during the weekends (start VM’s) using above solution. Please let us know if any such option available.
Thanks in advance.
Regards,
Surendra Aderu
Hi,
Thanks for taking the time to read and comment. I’m glad that you can use the information available.
To skip certain week days you should choose weekly instead of daily. This allows you to specify which weekdays to execute and which not to.
Hello, what would happen, if I schedule a shut down for 18 :00, but there is still a user connected to the application service that resides on VM? Thank you.
Hi Artur,
Thanks for reading. Any connections to the server – RDP or others – will be disconnected.
Will the status that is displayed by LifeCycle Services will be synched correctly? I am under the impression that the status is not synched correctly to LCS.
Hi,
Thanks for reading my blog and taking the time to comment.
Lifecycle Services is more or less correctly updated. They do end up with status stopped in LCS and deallocated in Azure; but in the end the cost is controlled from Azure and not LCS.
Great article and thank you. Can we add multiple VM to the schedule.
Thanks for reading my blog. If you have your VMs in the same resource group you should be able to control them all in one by only specifying the group in the filter.