This article shows how to automate scaling of Azure resources. I’m using this myself to save money by scaling down test- and development environments on nights and weekends, when they are not used anyway.
I’m using Azure Automation and Powershell Runbooks for these tasks.
1. Create an Automation account
Start by creating an Azure Automation account.
Select “Create Azure Run As account”. That will create a service principal user in your Azure Active Directory and assigns the Contributor role to this user.
In plain English that means that a user that has access to your resources will be created. The Powershell script will login with that user when scaling the resources.
2. Create Runbooks
In the Automation account I have created several Powershell “Runbooks”. A Runbook is basically a snippet of code that can be triggered by e.g. a timer, or another Runbook.
The first Runbook just logs in to my Azure subscription. I’ve actually just copied the first part of the “AzureAutomationTutorialScript” Runbook that was created when I created the Automation account.
The second Runbook is doing the actual scaling of a web app and a SQL database. It first calls the LoginToAzure Runbook, and after that the resources can be scaled.
If you try and run this Runbook you probably get an error saying that the Set-AzureRmAppServicePlan cmdlet is missing.
Not all Powershell commands are available in a Runbook by default. Fortunately it’s very easy to add the Module that contains the cmdlet you are missing. Just follow the instructions here: https://docs.microsoft.com/en-us/azure/automation/automation-runbook-gallery#modules-in-powershell-gallery
3. Schedule your Runbook
Add schedules in your Automation account and associate the Runbook with one or more schedules. This is an example of a schedule that runs at 8 in the morning every weekday.
That’s it 🙂