Recently I made a couple of demos how you could benefit from Azure Automation acting on certain alerts. One demo was, if an OMS or Azure Monitor alert is triggered because of high CPU on your Azure VM, a webhook will be called and scale up the Azure VM to a predefined size.
In Azure Monitor you are able to create an Alert and set the threshold to e.g. 80%…
…and then call a webhook…
Note: Azure Monitor has already built-in alert action to start / stop a virtual machine or even scale up / down. These actions install and call pre-configured Azure Automation runbooks from Microsoft. But this post should show you in a transparent way how you can achieve it, doing it yourself.
As you probably know in OMS you are also able to provide a webhook in your alert settings…
Which ever way you choose Azure Monitor or OMS, it does not matter. I just want to provide you the PowerShell script I used in Azure Automation. Although it is far from complete and production level, but as demo it serves it’s purpose .
# Get automation credential for authenticating and resizing the VM
$Credential = Get-AutomationPSCredential -Name 'AzureScaleUser'
# Size of the VM like Standard_D1_v2, Standard_D2_v2
$HWProfile = Get-AutomationVariable -Name 'ScaleUpSize'
# Resource group where the VM is living in
$ResourceGroup = Get-AutomationVariable -Name 'ResourceGroup'
# Subscription which hosts the VM / account etc.
$SubscriptionId = Get-AutomationVariable -Name 'SubscriptionID'
# VM name you want to up / down scale
$VMName = Get-AutomationVariable -Name 'VMName'
# Login to azure
Add-AzureRMAccount -Credential $Credential -SubscriptionId $SubscriptionId
# Get the VM
$VM = Get-AzureRmVM -Name $VMName -ResourceGroupName $ResourceGroup
If ($VM.HardwareProfile.VmSize -eq $HWProfile)
Write-Output "HW size already set to $($VM.HardwareProfile.VmSize)"
Write-Warning "Scaling up to $HWProfile"
# Set new VM size
$VM.HardwareProfile.VmSize = $HWProfile
# Update VM
Update-AzureRmVM -VM $VM -ResourceGroupName $ResourceGroup
Write-Output "HW scaling up to $($VM.HardwareProfile.VmSize)"
My Azure Automation variables look like this…
I hope this helps you for your next demo / PoC or what ever project you need it for.