Azure Virtual Machines allocate resources and charge you based on those machines running. If you’re like me, I have a set of Virtual Machines I use for testing, but do not need to be turned on all the time. I wanted a method to reduce the cost of running those Virtual Machines…without having to go in and turn on every Virtual Machine every time I wanted to use them. Remember, if you just shut down your Virtual Machines; you will still be charge as those resources are still allocated to you. The key is to stop/deallocate those Virtual Machines to ensure charges are no longer accrued to your account. This script is a set it and forget it script. Also, since it shuts down Virtual Machines based on tags, you’ll never need to update the script when you add more virtual machines. With all that being said, let’s get started.
You’ll need a few things:
- An Azure Automation account
- A user account will admin privileges
- A set of Virtual Machines you can use to test this script
- The PowerShell script to run the automation (http://pastebin.com/U8pPqz4K)
- A cup of strong coffee (optional, but recommended)
So to start, let’s navigate to http://portal.azure.com and sign in with our azure account. Once logged in, we will create the Azure Automation Account. To create that account, click on the new button and search for Automation. In the search blade, click on Automation. That will expand the Create Azure Automation Account blade. On the bottom, click on Create.
Fill out the Name, Subscription, Resource Group, and Location. I leave the Create Azure Run As account set to Yes. Once everything has been filled out, click create.
Next, we will create the Runbook. The runbook is what will have the script, as well as a schedule to know when to run. Start by navigating to http://pastebin.com/raw/U8pPqz4K and copy the script. Next go back to the portal and open up the “Automation Accounts” tile. Next click on the Automation Account you just made. Then, you’ll want to open up the “Settings” blade. If you scroll down, you’ll find a tile called “Runbooks”.
In the runbooks blade, click on the “Add a runbook” button on the top of the blade. Click on “Quick Create”.
Give the runbook any name you like, and for runbook type, choose “PowerShell”. Once you have all of that, click on create.
Once the runbook has created, you will be in the editor. Paste the script in. Next you’ll want to edit the $AzureCredentialName and $AzureSubscriptionName paramaters to match your environment.
After you have changed those lines out…save the runbook and click the publish button. Once the Runbook is published, we will need to create a schedule. We will do that last though.
The next step, is to create the tags on each Virtual Machine. These tags will denote which Virtual Machines the script will shut down, and also the times in which they will run. To start, click on the Virtual Machines tile, and then choose a Virtual Machine you would like to have scheduled to shut down. Then click on the tag icon.
In the Tags blade, type in the Key “AutoShutdownSchedule” and value “6PM -> 8AM”. The value denotes the times the Virtual Machine will be shut down and deallocated. You can choose any value that you would like that fits your needs. After you’re done, click the save button. You may now go to each Virtual Machine you would like and add that tag.
Finally, we will create a schedule for the runbook, so that it will run. Without a schedule, the runbook will not run automatically. To create a schedule, go back to the Runbooks blade, and click on the runbook we just created.
On the top bar of the runbook blade, you will see a schedule button. Click on the schedule button, select “Link a schedule to your runbook” then click “Create a new schedule”. In the New Schedule blade, give it a name, description, and start date. Under recurrence, click on recurring. This will open up additional options. I would suggest running this every hour. This is more of a discretion call that you will want to set. After all the options have been configured, click okay. Once the schedule has been linked to the runbook, you’re all set.
After setting the tags on the Virtual Machines, you can open up the runbook, and run a test to ensure everything works properly. I also like using different times for different labs that I have created. For instance, I have a few Virtual Machines that I want to run while I’m at home during off hours that are for personal use, but I do not need while I’m at work…so I have another value set for personal Virtual Machines of “3AM -> 6PM”. Also, you can take this concept of grabbing all Virtual Machines by a tag key and use it for other cool ideas. I personally have a runbook that clears my temp folder on all Virtual Machines. I have the key set to “ClearTempDrive” and a value of “C:\Temp”. I like this method because if I have other folders I would like cleared, I can add additional tag values with folder paths. So take this concept, play with it, and find other interesting ways to use this script’s methodology. I’d love to hear anything you all come up with.