Azure Automation is a way to automate frequently repeated processes that are commonly executed in the cloud. It saves you time since you don't need to execute things manually and of course, it saves your money!
- Create an Automation Account in Azure
- Create a runbook
- Create a Scheduled task to start your VM
- Create a Scheduled task to stop your VM
- Schedule Auto Shutdown
1. Create an Automation Account in Azure
In the request form specify name, subscription, resource group, location and set Create Azure Run as Account to yes, which allows us to manage Resource Manager resources by using runbooks. Then, click Create! See more information about how to create an automation account here. Here is how my settings look:
2. Create a runbook
After creating the automation account, it is time to create the runbook which contains the logic to automate processes. In the automation account you've just created, go to Runbooks > Add a runbook > Create a new runbook. It opens the request form where you enter the runbook's name and the runbook's type. At the time of this writing there are four runbook types:
Powershell: Allows you to create complex logic using Windows PowerShell commands. You can edit your code either directly in the Azure Portal or offline.
Graphical: Supports a graphical interface to edit the runbook logic. Unfortunately, you can't edit graphical logic outside of the Azure Portal.
Powershell Workflow: Similar to the powershell type. It is based on Windows Powershell workflow.
Graphical Workflow: Similar to the graphical type. It is based on Windows Powershell workflow.
I chose PowerShell as the runbook type since I'm more familiarized with it. Here is what my settings look like:
Now you've created the runbook, you can start adding code to it. Click the edit button which opens the integrated edit area. The logic I created can be used to either start or stop a virtual machine based on its resource group and name. I also added a logic to limit the code execution during the business days which in my case are Monday to Friday. I didn't take federal holidays into consideration. I shared my code through GitHub. I also added a couple of comments to make the code more understandable.
After adding the code to start and stop Azure VM's, you can try it using the Test pane which is an integrated test area in the Azure Portal. Testing the logic is very easy since you must only enter the required parameters and click Start.
Since your code is running as aspected, you can now publish your runbook. Without publishing the runbook you wont be able to create a scheduled task!
3. Create a scheduled task to start your VM
Now that you've published your runbook, you can create the first schedule task which will start your VM at a desired time. To create the scheduled task, go inside of the runbook to Schedules > Add a schedule. Configuring the scheduled task is very simple since you just have to create a schedule and enter the runbook's parameters. I want my VM to be started every business day at 6:00 a.m. Here is how my settings look:
4. Create a Scheduled task to stop your VM
Repeat the step before to create a scheduled task which turns the VM off. In my case, I want the VM to be stopped every business day at 10 p.m. Here is how both of my schedule tasks look:
5. Schedule auto shutdown
If your VM is not a classic VM, you can set the auto shutdown option which is an alternative to step 4. You can find the Auto-shutdown in your VM options.
The auto shutdown runs all week days. If you want to have more control over it, consider using Azure Automation.
Azure Automation simplifies your life as an administrator since you can automate tedious processes. It saves you time, money and it increases the reliability of executed tasks. If you have been using PowerShell, it will be very easy for you to get started with the Azure Automation :)
- Azure Automation Overview
- Azure Automation Runbook Types
- Getting Started with Azure Automation
- My first PowerShell runbook