One of the most common Azure administration tasks is scheduling an auto stop & start of your Azure VM’s to save you a few $$$.
Here’s a quick post showing how you can do this.
There’s a number of ways you can do this however I’ve chosen to reuse a PowerShell script from the Azure script gallery. Why reinvent the wheel, right?
1. First create an Automation Account within Azure – it will execute your start / stop script. I’ve called my account “AutomationAccount”, creative huh 🙂
2. Open the Automation Account you’ve just created and navigate to Runbooks > Browse From Gallery.
3. Find and import script “Scheduled Virtual Machine Shutdown/Startup”. The script should now appear in the Runbooks window.
Script documentation can be found here – https://automys.com/library/asset/scheduled-virtual-machine-shutdown-startup-microsoft-azure.
4. Edit the Runbook and then click Publish
5. This script will shut down any Azure resources with a Tag containing the key “AutoShutdownSchedule” and a value specifying the shut down schedule. Add the tag name to any resources you want to automatically startup / shutdown. Refer to the above documentation link for more information.
In my case, I’ve added the following Tag to the Resource Group containing my VM. I want my VM to be shut down on weekends
- Key: AutoShutdownSchedule
- Value: Saturday, Sunday
6. Next, create a new user in Active Directory. This user will execute your stop / start script. At the time of writing, this could only be managed through the old Azure portal.
7. Navigate to Settings > Administrators and Add register the user you just created as an Administrator of your Azure subscription.
8. Login with the new user you’ve just created. You should be prompted to enter a new password at first login.
9. Next you’ll need to instruct the Azure Automation service to use the correct user account when executing your script. Log back into your main Azure account and go back to your Automation Account. Open Settings > Assets > Credentials. Add a new Credential. The User Name and Password will be as per the Active Directory User Account previously created.
10. Everything should be in place now so lets test the script out. Open your Automation Account and Edit the Runbook you previously created, then open the Test Pane. This will allow you to run the script and troubleshoot any issues.
11. Assuming it’s running ok, schedule the Runbook to run as frequently as possible i.e. run every hour on every day of the week.
Remember: this schedule is NOT the schedule for stopping/ starting your VM’s. When the Runbook launches, it will look for any resources with the appropriate Tag and then lookup the stop / start schedule from the Tag value. This setup provides you with more flexibility i.e. if you have complex scheduling requirements.
To check whether it’s running properly, you can view the logs from the Jobs panel within the Automation Account settings blade.
Well that’s it. It’s quick and easy to setup.
As I mentioned before, there’s a variety of ways to do this however this is my preferred way.