Hello friends, it’s me again 🙂
As you all might know that Microsoft Azure provide the ability to Automate your VMs to stop during off-work hours, this is a very handy solution that you can always use to reduce your consumption cost and it introduces multiple methods you can use like schedule Auto Start/Stop on time basis (for example, start every day at 8.00 am and shutdown at 6.00 pm) or based on VM tags or based CPU utilization like we’re gonna see on this article.
Microsoft always have a decent documentation specially in Azure, but I feel that the article for Auto Start/Stop based on CPU utilization (see link below) is a little bit vague and not straight forward specially for anyone who is not experienced in Azure Automation, So I’ve decided to clear the fog in this step by step guide.
https://docs.microsoft.com/en-us/azure/automation/automation-solution-vm-management
We’re going to follow the above article to create the Start/Stop VMs during off-hours solution.
So during the creation of the Start/Stop solution there’s no option to use the CPU based utilization, it will always direct you to use the time based solution and here it comes the foggy part, How can we change?
1- If you go to your Your Automation Account > Schedules you would see the below
We need to disable the Scheduled-StopVM so the machine won’t stop based on time as we choose during the creation of the solution, but you still need the Scheduled-StartVM so your VM would start on specified time.
2- Go to YourAutomation Account > Variables
We need to change the highlighted variables to meet our needs,
External_AutoStop_Condition: This is the conditional operator required for configuring the condition before triggering an alert. Possible values are [GreaterThan, GreaterThanOrEqual, LessThan, LessThanOrEqual]
External_AutoStop_Threshold: Threshold for the Azure Alert rule. Possible percentage values ranging from 1 to 100
External_AutoStop_TimeAggregationOperator: The time aggregation operator which will be applied to the selected window size to evaluate the condition. Possible values are [Average, Minimum, Maximum, Total, Last]
External_AutoStop_TimeWindow: The window size over which Azure will analyze selected metric for triggering an alert. This parameter accepts input in timespan format. Possible values are from 5 mins to 6 hours.
(the above descriptions are from the Azure Portal)
I have changed mine as below for the sake of testing
My customization for testing is meant to shutdown the VM automatically if the CPU percentage reached anywhere above 40% in time window of 10 mins.
3- Run the AutoStop_CreateAlert_Parent runbook and specify your VM name. If many VMs are wanted sperate each name with a comma.
4- Back to Your Automation Account > Schedules, enable and modify the schedule for Schedule_AutoStop_CreateAlert_Parent.
When triggered, the AutoStop_CreateAlert_Parent will call the AutoStop_CreateAlert_Child and if this runbook monitors that your condition is true it will call AutoStop_VM_Child that will automatically shutdown your VM.
If you run AutoStop_CreateAlert_Parent manually it will not call the child runbook and therefore the automation will fail, it has to be scheduled.
And here we go, having an automatic stop for our VM based on CPU utilization.
Please hit me up if you have any question or points to discuss, your feedback is always appreciated 🙂
Hi Bassel,
Thank you for the interesting explanation. I need to set up CPU utilization based shutdown for a group of VMs at my workplace.
Is it possible to configure email alerts for Auto-stop VM based on CPU Utilization, by defining threshold for CPU Utilization. I need to be really sure of the VMs that are shut-down, specifically during initial test phase.
For example, the admin defines a list of VMs and maximum-minimum threshold for CPU utilization to be monitored. When the Azure Automation monitors CPU utilization for the list of VMs (defined by the admin of Azure Automation) and, if the CPU utilization is less than x% (for e.g., 25%), the Azure Automation stop the VMs (as defined by the admin), and then the admin receives the email notification defining the list of VMs which are stopped, runbook that caused the shutdown.
It will be of more help if you share screenshots/instructions step-by-step for doing the same.
At the Azure website from Microsoft, a similar email notification is provided for scheduled start and stop scenario, but not for CPU utilization based auto-stop.
Thank You in advance!
Hi Basil, Thank you for the wonderful explanation.
Do you know if we can imlement his functionality with a little bit more info /features like,
Before a VM is being shut down based on the Metric specified, I would like to recieve an email alert atleast 15 mins prior to the job execution.
Also it will be really helpful if that email contains a link that would trigger a postpone of the shutdown by lets say 1 hour.
If this link to postpone the job is not clicked by a user in x minutes, then the VM will shutdown in 15 mins from the time of email trigger.
Is this something that can be achieved? Let me know your thoughts
Hey Basil!
Thanks for taking the time to go through this detailed solution. I’m still surprised it is so unclear on how to incorporate utilization into the start/stop automation solution in Azure runbooks.
There’s also third party solutions, like VMPower.com that make it easier to do this at scale with just a few clicks.