Scheduled Azure VM Vertical Scale Up

ScheduledAzureVMVerticalScaleUp

Scheduled Azure VM Vertical Scaling

At times we would like to increase and decrease the size of our Azure VM based on schedule to efficiently plan out our Azure credit consumption. If you are looking on how to change the size of the VM based on schedule, you are looking at the right place. However, if you are looking on how to optimize the size of VM based on performance metric, you can find It here: https://docs.microsoft.com/en-us/azure/virtual-machine-scale-sets/virtual-machine-scale-sets-vertical-scale-reprovision

Overall, the process to perform scheduled vertical scaling of azure VM is as follows:

  • Create and automation account
  • Define Variable and Create Runbook
  • Test the runbook
  • Create schedule to the runbook

 

Step 1: Login to portal.azure.com and search for Automation. Give it a name,choose a subscription and Resource Group

As you are creating an automation account the Run As account will be created as well. Depending on your deployment (Classic or ARM), you should use these account to their respective environment

<

Step 2: Define the variable that will be used in the runbook script. For this particular script, we will define Certificate, CertificateName, SubscriptionID and SubscriptionName.

Certificate and CertificateName can be found under the Certificates of Automation account

SubscriptionID and SubscriptionName can be found under Billing of Azure.

 

Step 3: Now we will create a new Runbook under Runbook. Click on RunBook > Add a Runbook

Give the runbook a name, type and description. Since our script is based on powershell, we will put it as powershell

Step 3: Once the runbook is created it will direct you to an empty console. Enter the scripts in the empty space

$subscriptionName = Get-AutomationVariable -Name “SubscriptionName”
$subscriptionID = Get-AutomationVariable -Name “SubscriptionID”
$certificateName = Get-AutomationVariable -Name “CertificateName”
$certificate = Get-AutomationCertificate -Name $certificateName
Set-AzureSubscription -SubscriptionName $subscriptionName -SubscriptionId $subscriptionID -Certificate $certificate
Select-AzureSubscription $subscriptionName
Get-AzureService | where {$_.ServiceName -eq “<Cloudservicename>”}
Get-AzureVM -ServiceName <Cloudservicename> -Name <VMname> | Set-AzureVMSize -InstanceSize <Size>  | Update-AzureVM

*If you are using Azure ARM VMs, you may need to tweak the codes a little as this is for Classic/ASM.

In this example code below, I am scaling down for VM from DS4 to DS2 during off-peak hours

I have also created another runbook to scale up VM back from DS2 to DS4 during working hours

To test the code, click on the Test Pane to get a verbose log of the script

To execute the runbook manually, you can always go back to the runbook list, select the runbook you want, click Start and monitor the process

Step 4: Once we confirmed the runbook is working well, we can set the schedule for the runbook to run whenever it meets a certain time. Go to the Runbook and select the runbook you want to schedule, and click Schedules.

Click Add a Schedule > Link a schedule to your runbook > Create a new schedule

Select the schedule you want to run this runbook. Apply different schedule as accordingly to the scale up and scale down runbooks.

Once you have done the steps, your environment is now ready to perform scale up and scale down of VM using schedule provided.

Want to stay in the know?

Sign up to our newsletter to receive the latest news, events, webinars, and thought leadership.
Subscription Form (#6)