In part two of this series, I showed you how to autoscale virtual machines (VMs) using Virtual Machine Scale Sets (VMSS) and Managed Disks behind a load balancer. In part three, I’ll look at how to configure vertical scaling (up and down) as an alternative to the default horizontal scaling (in and out). For more information on VMSS, Managed Disks, and vertical versus horizontal scaling, read part ‎ one and two of the series.

Azure Autoscale uses horizontal scaling by default. This is the best solution to avoid interruptions in service because it does not require rebooting VMs. But vertical scaling can be achieved with the help of metrics, VMSS alerts, and Azure Automation runbooks. As you have probably guessed, it is more complicated to set up, test, and maintain. Nevertheless, I will show you how to configure a runbook to scale down VMs in the VMSS if CPU utilization drops below five percent. The same method can be used to scale up VMs.

Create an Azure Automation Run as Account

Before you can use a runbook for vertical scaling, you will need an Azure Automation account with a Run as Account. This allows the runbook to authenticate with Azure Resource Manager (ARM) resources.

Log in to the Azure management portal here.

Scroll to the bottom of the list of services on the far left and click More services .

. In the Filter field, type automation and click Automation Accounts in the results.

field, type and click in the results. At the top of the Automation Accounts pane, click + Add .

pane, click . In the Add Automation Account pane, give the new account a name in the Name field.

pane, give the new account a name in the field. Select the Azure subscription in which you want to create the new account using the Subscription drop-down menu.

drop-down menu. Check Use existing above the Resource group field.

above the field. Using the Resource group drop-down menu, select the resource group created in part 2 of this series.

drop-down menu, select the resource group created in part 2 of this series. Change the location using the Location drop-down menu if required.

drop-down menu if required. Make sure that Yes is selected under Create Azure Run As Account.

is selected under Click Create at the bottom of the pane.

The new automation account will appear in the Automation Accounts pane when deployment has completed. You might need to click Refresh to see if the pane is still open.

Import and Publish a PowerShell Runbook

Microsoft has created four runbooks for vertical scaling VMs in a VMSS. You can add them to your Azure Automation account by downloading them from the gallery.

On the Automation Accounts pane, click the new automation account to open it.

pane, click the new automation account to open it. On the Automation Account pane, click the Runbooks tile on the right.

pane, click the tile on the right. At the top of the Runbooks pane, click Browse gallery .

pane, click . In the Browse gallery pane, type scale in the Filter box. Press ENTER .

pane, type in the box. Press . Click on each of the following runbooks and then click Import in the runbook’s pane. To confirm the operation, click OK in the Import pane each time. Scale Down Virtual Machine Scale Set Instances Scale Up Virtual Machine Scale Set Instances Scale Down and Reprovision Virtual Machine Scale Set Instances Scale Up and Reprovision Virtual Machine Scale Set Instances

in the runbook’s pane. To confirm the operation, click in the pane each time. Close the Browse gallery pane, and you will see the four new runbooks on the Runbooks pane.

Before you can use an imported runbook, you need to publish it. First, decide which runbook you want to use. There are two runbooks that scale down VMs in a VMSS. You can choose to scale down, or scale down and reprovision. The latter choice removes existing VMs and replaces them with new ones. In this article, I will use a runbook that does not reprovision the VMs.

On the Runbooks pane, open the runbook you want to publish by clicking it in the list.

pane, open the runbook you want to publish by clicking it in the list. On the pane for the open runbook, click Edit in the list of options on the top right.

in the list of options on the top right. On the Edit PowerShell Runbook pane, click Publish .

pane, click . Click Yes to confirm the operation.

Create a Webhook

A webhook is a URL that will allow a VMSS alert to send information to the runbook. You want to do this so that an action can be automatically triggered.

Back on the Runbook pane, click Webhook at the top of the pane.

pane, click at the top of the pane. On the Add Webhook pane, click Create new webhook .

pane, click . Give the webhook a name on the Create a new webhook pane in the Name field.

pane in the field. Modify the expiration date of the webhook if required.

Before clicking OK , click the Clipboard icon to the right of the URL field. You will need the URL in the next step. Make sure you copy it somewhere safe.

, click the icon to the right of the field. You will need the URL in the next step. Make sure you copy it somewhere safe. Click OK .

. Now click Create parameters and run settings on the left of the Add Webhook pane.

on the left of the pane. On the Parameters pane, leave the default settings. Click OK .

pane, leave the default settings. Click . On the Add Webhook pane, click Create to add the webhook to the runbook.

Add an Alert to the VMSS

The final step is to add an alert to the VMSS. This will raise the alarm when CPU usage rises above five percent. You will need the URL of the webhook that was created above.

Scroll to the bottom of the list of services on the far left, and click More services .

. In the Filter field, type scale and click Virtual machine scale sets in the results.

field, type and click in the results. On the Virtual machine scale sets pane, click your VMSS in the list to open it.

pane, click your VMSS in the list to open it. Under MONITORING on the VMSS pane, click Alert rules .

on the pane, click . Click Add metric alert at the top of the Alert rules window.

at the top of the window. On the Add rule pane, give the new rule a name in the Name field. Add a description if required.

pane, give the new rule a name in the field. Add a description if required. Scroll down to the Condition drop-down menu and select Less than .

drop-down menu and select . In the Threshold field, type the percentage threshold on which you would like to generate an alert for CPU usage. I’m going to use 5.

field, type the percentage threshold on which you would like to generate an alert for CPU usage. I’m going to use 5. Microsoft recommends setting a Period of at least 20-30 minutes to avoid triggering vertical scaling too often. I am going to select a period of Over the last 30 minutes .

of at least 20-30 minutes to avoid triggering vertical scaling too often. I am going to select a period of . In the Notify via section, copy the URL of the webhook you created in the previous steps into the Webhook field.

section, copy the URL of the webhook you created in the previous steps into the field. Click OK to add the rule.

to add the rule. The new rule will now appear in the Alert rules pane.

Now all you need to do is wait for an alert to be generated. This alert will trigger the runbook via the webhook. Note that you cannot trigger the runbook manually for testing. It is designed to work only when triggered by an alert. It is also worth noting that not all VMs sizes can be scaled down. For more information on which VMs sizes can be scaled, open the runbook code by clicking View at the top of the runbook’s pane.

In this article, I showed you how to configure vertical scaling for VMSS using Azure Automation and a PowerShell runbook.