Azure Virtual Machine and Tags
Andrew Posted on 1:24 pm

Managing Azure Resources – A Primer – Controlling Costs with Automation

Last time we looked at some different ways of getting started with managing your Azure resources. Today I’d like to introduce you a little bit to Azure Automation – this time for saving money in shutting down unused resources – and also starting them on a schedule. Last article we created a demo resource group called rgDemoControl1, to catch up check it out here. Today we will build on this and learn how to shut off virtual machines & resources automatically.

So, as usual let’s make sure we are logged into our testing subscription at https://portal.azure.com and then double-check you’re in the testing environment. It’s important to not test these things in a production environment – imagine we accidentally shut down production systems? Exactly….let’s not do that today!

Excellent! Let’s go the Virtual Machines blade. You’ll see something similar to this if you filter on the resource group rgDemoControl1.

Note the tag “testing:demo”. This will help us in reporting so that we can understand the costs better.

Now, we’ve talked about automation on the AzureTracks blog before – to learn more about it check out the articles here and then come back & join us for the rest of this little introduction to applying automation in our testing environment.

First, let’s get the VMs turned on each morning before we start working. Let’s be optimistic and say we’ll use a 7 day schedule for a special project we’re testing software with a group of selected users in our organization. The first step is to jump to the Azure Automation Account blade here. Next select the account to use, or reference the previous setup articles here for more guidance.

Once you have selected your Azure Automation account, choose Runbooks.

Side-Trip for Azure Update Management

Say – remember last article when we did some work with Update Management? Well, just as a side-trip here to go ahead and take a look in there (the link just above Runbooks!). You may notice your VMs have updates to apply! One thing I left off the intro was scheduling the updates to push out.

Go ahead and click on Update Management > Schedule Update Deployment and schedule the updates to get installed on your VMs. It is actually just that easy!! There have been a lot of ways over the years to get updates pushed out and this is just tucked away as part of your included features in Azure – so get in there and test it out then go use it folks!

Also notice that you can add additional VMs either in Azure or in other locations…such as on-premises, private cloud, other public clouds, where-ever you have VMs. Amazing!

Back to Azure Automation Runbooks

Alright, back on track here, let’s jump back to the Azure Automation account and then click on Runbooks. Now let’s select a bit down the list is the option Start/Stop VM.

In the new blade select the option Learn More About and Enable the Solution.

Create the solution Start/Stop VMs during off-hours. You can also just search for this from the Azure Portal home search box.

This could not be easier!

If you leave the Target Resource Group as * it will apply to ALL groups in your subscription!

Now simply select your Log Analytics workspace and select Parameters. We’ll enter the name rgDemoControl1 in our Target ResourceGroup Names box as we only want to control our own VMs in this testing group.

Next, set your schedule options, email address notifications and of course if you select Yes to Notifications enter an email address to use. Select OK when ready > then Create.

Once the solution is deployed, jump to the resource group you used and note that there will be a few Runbooks created by the solution. Give it a few minutes, then let’s go take a look at this!

Above, I selected my runbook called ScheduledStartStop_Base_Classic that was created automatically, then I chose Edit. You can see from the snap of the code in the runbook that it is well written as a Function that accepts 3 parameters passed in when this function is called by the scheduling agent.

Excellent. Now our VMs inside our rgDemoControl1 resource group will be automatically started and stopped based on the schedule that we set.

As a final note, you should receive an email notification on the address you entered once the alert is created:

Thanks for hanging in through this really fun walk-through for some Azure Automation that allows us to save our money. If we architect and plan carefully, we will have funding and time left to try new features throughout the year! Remember to clean up any resources you’re done testing with!

See you all next time!