Change the instance count of an Azure VM Scale Set

image

This article describes how to change the number of VMs in an Azure VM Scale Set.

The most important property of a scale set is “capacity”, which represents the number of VMs in the set. The main premise of a scale set is that you can easily change the number of VMs, i.e. scale it in or out, without having to worry about underlying resources like NICs, storage accounts, update domain/fault domain placement, changing VM properties etc.

So scaling a scale set is easy right? Yes. There is a scale slider in the Azure portal which lets you set the number of VMs. The rest of this article concerns changing the capacity of a scale set using command line and SDK.

The basic steps to change the capacity of a scale set are:

  • GET the “model”, i.e. configuration of the scale set.
  • Change the “capacity” setting in the model.
  • PUT the model, i.e. update the configuration of the scale set.

Fortunately when you update the model of a scale set, you don’t have to include all the details, so you can skip the first two steps and do a “PATCH” which only passes in the “sku”, i.e. the information packet pictured above.

Here’s a quick review of how to change the capacity of a scale set using Azure templates, PowerShell, CLI, and the REST API (you could also change capacity using Azure Resource Manager, not covered in this article)..

Change capacity using a template

This is less useful now since the portal supports changing scale set capacity directly, but you can change capacity by deploying a small template which only contain the “sku” which includes the capacity: https://github.com/Azure/azure-quickstart-templates/tree/master/201-vmss-scale-existing and click on the “Deploy to Azure” button. When you enter the parameters in the portal, make sure you use the same resource group, VM Scale Set name, and vmSku (machine size) as your existing scale set. You could deploy a similar template by any other means too like CLI, PowerShell, SDK etc.

Change capacity using PowerShell

Here’s an example using Azure PowerShell that uses the GET/Change/PUT approach to set a scale set called winvmss to 10 VMs:

$vmss = Get-AzureRmVmss -ResourceGroupName winvmss -VMScaleSetName winvmss  
$vmss.Sku.Capacity = 10
Update-AzureRmVmss -ResourceGroupName winvmss -Name winvmss -VirtualMachineScaleSet $vmss  

Change capacity using CLI

CLI has a built-in scale command which makes scaling very easy.

Azure CLI 1.0 azure vmss scale resourcegroupname vmssname newcapacity.

E.g. azure vmss scale myrg myvmss 10

Azure CLI 2.0 az vmss scale -g rgname -n vmssname –new-capacity newcapacity

E.g. az vmss scale -g myrg -n myvmss –new-capacity 10

Change capacity using the REST API

The good thing about calling the Azure REST API directly is that it makes it easy to wrap a simple function call around a scale operation so it can be as simple as it should be.

Here’s an example in Python for doing a PATCH call to a scale set which only passes the ‘sku’ property. The azurerm unofficial Python library implements a function like this:

# scale_vmss(access_token, subscription_id, resource_group, vmss_name, size, tier, capacity)
# change the instance count of an existing VM Scale Set
def scale_vmss(access_token, subscription_id, resource_group, vmss_name, size, tier, capacity):
    endpoint = ''.join([azure_rm_endpoint,
                        '/subscriptions/', subscription_id,
                        '/resourceGroups/', resource_group,
                        '/providers/Microsoft.Compute/virtualMachineScaleSets/', vmss_name,
                        '?api-version=', COMP_API])
    body = '{"sku":{ "name":"' + size + '", "tier":"' + tier + '", "capacity":"' + str(capacity) + '"}}'
    return do_patch(endpoint, body, access_token)

Advertisements
This entry was posted in Cloud, Computers and Internet, Python, VM Scale Sets and tagged , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s