How to automate shutdowns of Azure VMs

Scheduling starts and shutdowns of Azure Virtual Machines without PowerShell

In this tutorial, we’re going to learn how to Shutdown and Start cloud-based Microsoft Azure Virtual Machines on a schedule.  The process takes about 1-2 minutes and is easily replicable to any and all VMs in your Azure subscription in just a few minutes more.  It works for Windows and Linux VMs and does not rely on somewhat complex and hard to maintain PowerShell scripts or Azure Automation runbooks.  Also, if any errors occur during either start or shutdown of VMs, you will get an immediate notification.

If you have production Azure VMs that need to remain up but are just utilized less, please learn more about how to scale down Azure VMs on a schedule.

Scheduling the start and shutdown of VMs allows you to save money on Azure costs.  A VM that has been shutdown and deallocated does not incur any Azure charges (except for the space it occupies in blob storage).  Shutting down VMs is usually beneficial for DEV/QA and other non-production environments, CI/build servers, or for production environments where access to VMs is required only during specific times.

Step 1 – Get a Netreo Account

You will need a Netreo account.  If you have not yet registered, go here.  Netreo is a pretty sophisticated monitoring and automation software for Azure.  By using Netreo, you’ll have access to a ton of useful performance metrics, logs, alerts, integrations, and other automation features.  While during the 2-week trial period everything in Netreo is free, after the trial expires, automated Startups and Shutdowns will only available for users of Netreo’s Ultimate plan.  But many companies easily justify the relatively inexpensive cost of Netreo by configuring it to power-down their unused VMs.

However, if you want to shutdown and start Azure VMs on a true budget, you will want to learn about Azure Automation and Azure PowerShell.  There are a few dozen blogs on this particular subject and we’ll link to them at the bottom of this article. Be aware, that you will still need to find ways to get alerted when Azure Automation Runbooks fail to run properly, otherwise you may end up with unpleasant surprises every once in a while.

Technical considerations to note: when a VM is shutdown its IP is deallocated.  When it is brought back up, it will get a new IP unless it is assigned an Azure Reserved IP.

Step 2 – Connect Netreo to your Azure Subscription

Ensure that your Azure Virtual Machines have been added to Netreo account.  Simplest way to do so is to run thru the Netreo Setup Wizard.  It takes a few minutes and it is very intuitive and straightforward.

Netreo Setup Wizard
Netreo Setup Wizard

Step 3 – Define Stop and Shutdown actions to execute at specific times

After Wizard completes, edit the monitoring definition of a particular Azure VM and on the Actions tab and define two actions; one to stop and one to start a VM at specific times.  In detail:

  • Bring up an Edit dialog for a specific Azure VM from Netreo dashboard
  • On the Actions tab, add a new action.  This will be the action to Stop the VM at a specific time
  • Specify Name and Severity of the action
  • Ensure that Action execution is based on a Schedule instead of an Expression
  • Define a Start time of the action by creating a new Schedule entry of type Action Execution (keep in mind that the start time is defined in UTC). Schedule setup is quiet flexible and has many more scheduling options than Azure’s native Scheduler.
    Defining the start time for action to execute on
  • Execution target should remain as “Self”
  • Execution Command should be “AzureVmInstanceShutdownDeallocate”.  This will deallocate the VM and stop charges from Azure
  • Keep in mind that Netreo will also send out alerts in case of either success or failure of action execution
  • Save and Done.
  • Now rinse and repeat this process to create a similar Start action.  Use “AzureVmInstanceStart” command and a different time schedule instead.
  • Since the Schedule setup is quiet flexible, you are able to create any number of Start and Stop actions under a single resource to accommodate different schedules for weekdays and weekends, specific weeks of the month, etc.
    Netreo action definition to shutdown Azure VM on a schedule

Step 4 (Optional) – Automate other Virtual Machines quickly

Configuring other VMs to shutdown and start on the same schedule is a fairly trivial task in Netreo.

  • Save the configuration of configured VM as a “Template”
  • Navigate to Resource Templates screen under Account Settings and open the previously saved Template
  • “Push” the StartVM or StopVM actions any other VMs in your monitored setup
    Copying Start/Stop actions to other VMs

Automating Azure Virtual Machines without Netreo

There are a number of excellent blogs that provide detailed instructions on how to use Azure Automation Runbooks or Scheduling Manager with Azure PowerShell to automate startup and shutdown of VMs.  Here are a links to a few of them

Find Out how Netreo can help you with all your Azure monitoring needs. Request a Demo Today!

Ready to get started? Get in touch or schedule a demo.