Share via


Shut Down Azure VM’s Automatically

Shut Em Down

I’m focusing more on Azure Technologies in order to complement my SharePoint workloads. To this end I’ve been leveraging my MSDN Azure benefit which gives me $150 of Azure credit every month. Some folks I work with don’t think this is enough but I’ve been able to consistently make this last for several months without hitting my limit.

There are some considerations to make this happen.

  • You can’t just spin up any old VM. Choose the smallest size you can get away with. I’m partial to A0 size for DC’s, and A3’s for most other workloads.
  • Make sure that you choose the “Basic” tier of any of those machine sizes. Basic removes the load-balancing and auto-scaling features. You usually don’t need that for test workloads. See the Azure VM Pricing page for more details.

Finally, like Sticky and Fredro up top, you need to Shut ‘Em Down. Shut down the VM’s as soon as you’re done using them. This will stop using up your credits. Don’t leave a 5 server farm running overnight and expect to be able to pick up in the morning. I know dev’s that have forgotten and then try to open support tickets to get at their data after their allowance has expired, but remember that the MSDN tier only offers subscription support, not technical support. In any case, you’ll need more credits in your account in order to spin those machines back up to get the data.

Solution? Azure Automation. It’s a feature that allows you to use automate common administrative tasks in your Azure account. There is counterpart called Service Management Automation (SMA) that is part of the Windows Server 2012 R2, but I’m including that just for reference. Some of the documentation and blog posts out there will reference SMA but you can apply a lot of the same code since it’s all PowerShell.

 Azure Automation allows you to author a PowerShell workflow and publish it as a Runbook, which then can be scheduled.

A fellow MSoftie published a runbook to the gallery that shuts down all your machines every night. You can look at the Runbook here.

In order to get this to work, you have to create an Azure AD account that can be used by Azure Automation to log into your account. You don’t want to use your Microsoft (Hotmail, Live, Outlook.com) account since they won’t work with Azure Automation. The overview of the this process is to

  • Create an Azure account in your subscription,
  • Grant it co-administrator rights to manage your subscription resources
  • Create an Automation Credential asset that uses that account

John Levy has posted detailed instructions on the Azure Blog. Follow those steps first.

The next step is to actually create the Runbook. We have a template on the Azure Portal. Select New > App Services > Automation > Runbook

image

Now select “From Gallery”. You’ll get a dialog listing all the Runbook templates we offer. Select VM Lifecycle Management > Azure Automation Workflow to schedule stopping of all Azure Virtual Machines.

image

Give your Runbook a name and associate it with the Automation account you created above.

image

The Runbook will be imported into your account. Once it is finished, click “Edit Runbook” to see the code. The comments will tell you the final steps. You’ll need to add your Automation credential to the script and reference your Azure Subscription.

image

You can test the script by clicking on “test”. Be aware that you may get an error regarding the credential. In the script above you’re asked to enter it in the format <“account@MySubscription.OnMicrosoft.com>” but I got it to work by simply using the “Account” portion and excluding everything to from and to the right of the “@”, inclusive. So that line would look like

$Cred = Get-AutomationPSCredential –Name “Account”

Now that you have this completed, publish the Runbook and create a schedule for it to run every night. Choose midnight or some other time that is appropriate for your work habits. This will save you credits and you’ll never have to worry about forgetting to shut down your machines again. Be aware that the machines will forcefully shut down so if you need the machines to stay up then disable the schedule. Also remember to save your work since it’s tantamount to somebody just switching off a physical server.

Comments

  • Anonymous
    January 01, 2003
    Thanks Mario. Came across this while researching the topic. I ended up with a runbook that uses tags to define a schedule. Here's the link for those interested:https://automys.com/library/asset/scheduled-virtual-machine-shutdown-startup-microsoft-azure
  • Anonymous
    August 24, 2015
    What do I have to put into the SubscriptionName? I have the MSDN subscription...
  • Anonymous
    August 24, 2015
    What do I have to put into the SubscriptionName? I have the MSDN subscription...
  • Anonymous
    August 25, 2015
    Roman,

    Log into the portal (old one for this example), click on your name in the upper-right hand side, click on "View My Bill" and you'll see the full name of your susbscription. Something like "Windows Azure MSDN - Visua Studio"
  • Anonymous
    August 28, 2015
    The comment has been removed
  • Anonymous
    August 28, 2015
    Please let me begin by simply saying that will do-it-yourself lawyering features its boundaries. Surely, you could draft contracts yourself, you could survive ugly negotiations using your business clientele, you could settle a new marital question among her but when the call to come for you to court comes up.
    http://www.thetexaslawyer.org
  • Anonymous
    August 28, 2015
    The comment has been removed
  • Anonymous
    August 28, 2015
    Most of the people hate legal guidelines since legal guidelines always reduce the liberty of individuals. In truth laws are generally like individuals who has to be your friend, opposing forces, guide along with servant. Read this information to know basic principles of legislations.
    http://www.gclaw.org
  • Anonymous
    August 28, 2015
    Does one or a number of one you already know have Autism? Have you ever found answers for a questions? United Pertaining to Autism is often a Non-Profit Corporation providing replies and be an aid to families experiencing Autism. There exists help, you'll find answers, along with autism can be treatable. To acquire more information about is keep reading.
    http://www.autismlaw.org
  • Anonymous
    October 05, 2015
    Thanks for the article.
    2 question though, is there a way of shutting down a Azure VM after is hasn't been used for let say 1 hour?
    Also we are running software on the servers that have licenses based on machineid. When you shutdown the VM the machineid is changed.

    Hope someone can answer these questions.

  • Anonymous
    January 26, 2016
    If you're a part of an organization that is looking to systematize and monitor such cost-saving automation in their Azure environments, check out two blogs that show a white-glove approach to how Azure VMs can be shutdown or scaled down on a schedule.
    Shut-down Azure VMs on a schedule: http://cloudmonix.com/blog/how-to-automate-schedule-shutdowns-of-azure-vms/
    Scale-down Azure VMs on a schedule: http://cloudmonix.com/blog/how-to-automate-scaling-of-azure-vms/
    HTH
  • Anonymous
    January 27, 2016
    For those who want a more white-glove approach to doing this, without PowerShell and with failure notifications and other goodies, check out these blogs:
    Shut-down Azure VMs on a schedule: http://cloudmonix.com/blog/how-to-automate-schedule-shutdowns-of-azure-vms/
    Scale-down Azure VMs on a schedule: http://cloudmonix.com/blog/how-to-automate-scaling-of-azure-vms/