How can you start and stop your virtual machines using Azure Automation? And why should you? As you may know, Azure virtual machines are billed per second. If no 24/7 access is needed, virtual machines can be shut down for example during night time or outside office hours, and then spawned up again in the morning. Doing that can hugely reduce costs since you are only paying for the time the server is actually running.
Probably the simplest way to achieve automatic shutdown is to enable Auto-shutdown. This can be done under the virtual machine blade (Operations -> Auto-shutdown). It can also be specified on the ‘Management’ tab when creating a virtual machine.
Although it is a really simple and easy solution and can be really useful, for example, if you have demo or development environments that you tend to forget to switch off (like myself), it lacks the possibility to start the virtual machine again at a certain time.
At this point Azure Automation comes along. By creating an Azure Automation Account, importing runbooks from the Runbook gallery, and setting up a schedule we can achieve scheduled stops and starts. This is also a fairly inexpensive method since you can run your jobs for 500 minutes per month free of charge after which you’ll pay 0,002€ per minute. Let’s find out how this can be set up!
Creating the automation
The first task is to create an Automation Account (Create a resource -> Automation -> Create).
Automation account needs of course to be named, put in subscription, resource group and location of your choice. We’ll also agree to create ‘Azure Run As account’. What this means is that the creation of an Automation account also creates a Service Principal (Azure AD Application) that it can utilize to connect the resources within the subscription. By default this Service Principal will have Contributor rights for the whole subscription. If you need to utilize automation schedule only for certain Resource Group(s) you may want to grant access for only that/those Resource Group(s).
After a few minutes the Automation Account is created and we can navigate to it. Let’s select ‘Runbook gallery’ under ‘Process Automation’ and search for ‘Azure V2 VMs’. Now we should see ‘Start Azure V2 VMs’ and ‘Stop Azure V2 VMs’ runbooks.
The next step is to import these runbooks to our Automation Account by clicking the runbook in question and clicking ‘Import’. During import it is possible to edit runbook name and description.
After importing runbooks they can be found in ‘Runbooks’ under ‘Process Automation’. Note that newly imported runbooks are marked as new and before they can be used they need to be published.
Publishing runbooks can be done by selecting the runbook, clicking ‘Edit’ and then ‘Publish’. Now both runbooks should have their ‘Authoring status’ set as ‘Published’.
The next step is to create schedules for those runbooks. Again, this can be done in the left pane (Shared Resources -> Schedules).
Automation can be set to run once or in a recurring manner. In case it is set to be recurring there are a few options to choose from.
- Hour: runs every x hour (e.g. if set to run every 1 hour and start is set to 10:00 AM the schedule will run on 11:00 AM, 12:00 AM, 1:00 PM, etc.)
- Day: runs every day at the same time
- Week: runs at the same time on selected days (can be useful for example if you want your VMs to run on weekdays but to be shut down during weekends)
- Month: runs at the same time on selected days of the month (e.g. your script can run on every Monday every month)
It is also possible to set an expiration date for schedules.
When schedules for start and stop are created it’s time to add those schedules to their respective runbooks. Navigate to Runbooks -> StartAzureV2Vm -> Schedules -> Add a schedule. Here we can link the newly created schedule to a runbook.
After that it’s time to enter parameters and run settings.
Here you can specify three parameters for the schedule.
- RESOURCEGROUPNAME: As it states this is the Resource Group that holds the VMs that should be started or stopped. If you specify only this parameter all the VMs inside that Resource Group will be affected when the schedule is run.
- VMNAME: Name of the VM that should be started or stopped. If you specify this parameter, only the VM in question will be affected.
- AZURECONNECTIONASSETNAME: This is the connection asset that has information for connection using service principal. Can be left blank to use the default parameter.
Schedules can now be seen under ‘Schedules’ (surprise, surprise!). Here you can also see when the next run will occur, the time zone in use and if the schedule is enabled or not.
Now everything is set up and it’s time to wait until the magic happens! After the schedule has started the execution of the runbook you can see it on the ‘Jobs’ blade.