Automatically shutdown virtual machines in Azure

There are already a few blog posts available about how to shutdown virtual machines in Azure. I listed some of them at the bottom of this post, but anyhow – I wanted to try it on my own and wrote a short documentation about it.

There are different ways how to automatically shutdown a virtual machine in Azure:

  • Automation account (preferred way for existing VMs)
  • DevTest-Labs (preferred for development and test environments)
  • WebJobs
  • Install a script on your servers

Update December 1, 2016: there is a new feature available that allows to configure the auto shutdown directly as a virtual machine configuration – see: Auto shutdown Azure virtual machines. If you want to shutdown multiple virtual machines and/or control it from a central point – continue reading this article.

Automation account

Using an automation account is the easiest way to shutdown the machines. The basic automation account is available for free. It supports 500 minutes of execution time which is more than enough to automatically shutdown or start virtual machines.
To configure it, you just need to execute the following steps and everythings up and running:

  1. Create an automation account:
    20160721_01_addAutomationAccount
  2. Configure name, resource group, etc.: I chose the resource group from one of my virtual machines so that I can delete or export my whole configuration (including the VM, network interfaces, storage account, automation account etc.). I selected to create a “Run as” account because I don’t have one yet.
    20160721_02_addAutomationAccount
  3. Open the runbooks and select browse gallery.20160721_03_configureRunbook
  4. Now you can see that there are already a lot of runbooks available to shutdown virtual machines. The most popular one is “Scheduled Virtual Machine Shutdown/Startup”. This script uses the tags of a virtual machine for shutting it down. You can find a description about it here: https://automys.com/library/asset/scheduled-virtual-machine-shutdown-startup-microsoft-azure
  5. I used the graphical runbook from the automation team: “Stop Azure V2 VMs”.20160721_06_addRunbook
  6. Click on “Import” and ok.20160721_07_configureRunbook
  7. Select your newly created runbook. It’s currently not possible to configure a schedule for it, because we need to publish it at first.
  8. Click on edit and next on “Test pane”. You can immediately start the runbook. Probably it will take some minutes till it really starts., but you can see the current status (e.g. “Queued”, “Running…”, “Completed”) in black window on the right side . When it’s done, then you will see the output – e.g. “my-vm is already stopped”.20160721_08_editRunbook
  9. Looks good, so let’s click on publish and confirm the popup.
  10. Now we are allowed to configure a schedule. Let’s click on schedule – add schedule – Link schedule to your runbook – Create new schedule:20160721_10_createSchedule
    I’ll create one that runs at the end of each day. It’s possible to enter e.g. “20:00” as time and it will automatically change to: 8:00:00 PM
  11. If you want, you can also configure the parameters “RESOURCEGROUPNAME”, “VMNAME” and “AZURECONNECTIONASSETNAME”. If you leave them empty, then all VMs will be shutdown. The parameters allow you to shutdown only VMs from a specific resource group or only shutdown a specific VM. If you want to shutdown only one VM, then you have to set the RESOURCEGROUPNAME and the VMNAME. The schedules allows to create multiple schedules for different virtual machines. So if there are 3 specific virtual machines, we can configure a different schedule for each of the virtual machines and set the VM by setting the mentioned parameters (more details: https://gallery.technet.microsoft.com/scriptcenter/Stop-Azure-ARM-VMs-1ba96d5b)
  12. Click OK and your schedule is up and running!
  13. Alternative to the schedule is to create a webhook. Every time you make a POST to the http URL, the runbook will be started. This can be very useful if you want to create a little app that allows you to start and stop your virtual machine via smartphone.

That’s the easiest way how to shutdown all or some virtual machines. If you need to configure the schedule per VM or per resource group, then you can add multiple runbooks of the template “Stop Azure V2 VMs” or you use the “Scheduled Virtual Machine Shutdown/Startup”. The second one allows you to set the schedule as a tag for each resource group or virtual machine. This runbook should be configured that it runs every hour. Every hour it checks all VMs and if they should run or not (by checking the tag). If they should not run, then it simply shuts them down. (see: https://automys.com/library/asset/scheduled-virtual-machine-shutdown-startup-microsoft-azure)

DevTest-Labs

Another option is to use DevTest-Labs. When you create them, then you can already select a schedule when to shutdown your virtual machines. That is the best way how to do it if you have development or test environments. You can create multiple DevTest-Labs with different schedules. Unfortunately it is currently not possible to move existing VMs to a DevTest-Lab. The uservoice site suggests that this feature is planned, but it is not yet started.

20160721_09_createDevTestLab

When you create a new DevTest-Lab, Azure automatically creates a resource group, a virtual network, two storage accounts (one for the vhd, the other one for diagnostic purposes) and the DevTest-Lab itself. Each newly created virtual machine will have it’s own resource group, but it is part of the DevTest-Lab. If you want to get more information about DevTest-Labs – check the links below in additional information.

20160721_11_devTestLab

Additional Information

Automation Service Pricing: https://azure.microsoft.com/en-us/pricing/details/automation/
Pricing calculator: https://azure.microsoft.com/en-us/pricing/calculator/?service=automation
Use Azure automation to run VMs during office hours: https://blogs.endjin.com/2016/03/using-azure-automation-to-run-vms-during-office-hours-only-using-graphical-runbooks/
Azure-Automation – Schedule the shutdown of Azure VMs with PowerShell: http://www.techdiction.com/2015/03/25/microsoft-azure-automation-schedule-virtual-machines-to-shutdown-using-powershell/
Azure Automation scenario – starting and stopping virtual machines: https://azure.microsoft.com/en-us/documentation/articles/automation-solution-startstopvm-graphical/
Azure Dev Test Labs: http://www.clemensreijnen.nl/post/2016/06/23/Azure-DevTest-Labs
DevTest-Labs overview: https://azure.microsoft.com/en-us/documentation/articles/devtest-lab-overview/

Categories:

No responses yet

Leave a Reply

Your email address will not be published. Required fields are marked *

About
about armin

Armin Reiter
Blockchain/Web3, IT-Security & Azure
Vienna, Austria

Reiter ITS Logo

Cryptix Logo

Legal information