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.