Prerequisites

The Problem

You are trying to add auto scaling to an existing VMSS.

How does auto scaling in a VMSS work?

Virtual Machines (or VMSS) scale depending on guest metrics. These guest metrics are generated by either the Windows Azure Diagnostics Extension (on Windows) or the Linux Diagnostics Extension (on Linux).

You can see the output of the diagnostics extension when viewing the VMSS in the portal.

For auto scaling to actually work you need to deploy an auto scale resource into the VMSS’ resource group. The resource allows you to configure scaling rules.
The JSON template at the end of this post shows an example incremental template that deploys an auto scale setting collection. The collection is configured to scale out when the average VMSS CPU reaches 60% in any five minute time window.

It will scale in when the average CPU consumption falls below 30% in a 5-minute time window.

A cooldown period has been specified to give the new machines time to come online, before further machines are created.

More information: https://azure.microsoft.com/en-us/documentation/articles/virtual-machine-scale-sets-autoscale-overview/#set-up-scaling-by-using-resource-manager-templates

Using the JSON template

The template is intended for an incremental deployment to a resource group that has already got a VMSS in it. The template will provision auto scaling rules only.
I would recommend reviewing the above article for guidance on the usage of these rules.

Note that the maximum capacity is specified as 10 VMs and the minimum capacity is specified as 1 VM.

Step-by-Step Guide

Start with an existing VMSS that does not have auto scale configured. New VMSS can be configured with auto scaling out of the box.
Start with an existing VMSS that does not have auto scale configured. New VMSS can be configured with auto scaling out of the box.
Start a new template deployment.
Start a new template deployment.
Copy the code.
Copy the code.
Adjust parameters.
Adjust parameters.
Deployment should complete successfully.
Deployment should complete successfully.
Auto scaling should now be enabled with the rules defined by you in the template.
Auto scaling should now be enabled with the rules defined by you in the template.

JSON Template below

{
"$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"vmssName": {
"type": "string",
"metadata": {
"description": "The VMSS you want to add auto scale to."
}
},
"autoScaleSettingsName": {
"type": "string",
"metadata": {
"description": "Name your set of settings."
}
},
"azureLocation": {
"type": "string",
"metadata": {
"description": "Specify the Azure location in which you VMSS resides."
}
}
},
"variables": { },
"resources": [
{
"comments": "AutoScale settings for an existing VMSS",
"type": "microsoft.insights/autoscalesettings",
"name": "[parameters('autoScaleSettingsName')]",
"apiVersion": "2014-04-01",
"location": "[parameters('azureLocation')]",
"tags": { },
"properties": {
"profiles": [
{
"name": "Profile1",
"capacity": {
"minimum": "1",
"maximum": "10",
"default": "1"
},
"rules": [
{
"metricTrigger": {
"metricName": "\\Processor\\PercentProcessorTime",
"metricNamespace": "",
"metricResourceUri": "[resourceId('Microsoft.Compute/virtualMachineScaleSets', parameters('vmssName'))]",
"timeGrain": "PT1M",
"statistic": "Average",
"timeWindow": "PT5M",
"timeAggregation": "Average",
"operator": "GreaterThan",
"threshold": 60
},
"scaleAction": {
"direction": "Increase",
"type": "ChangeCount",
"value": "1",
"cooldown": "PT1M"
}
},
{
"metricTrigger": {
"metricName": "\\Processor\\PercentProcessorTime",
"metricNamespace": "",
"metricResourceUri": "[resourceId('Microsoft.Compute/virtualMachineScaleSets', parameters('vmssName'))]",
"timeGrain": "PT1M",
"statistic": "Average",
"timeWindow": "PT5M",
"timeAggregation": "Average",
"operator": "LessThan",
"threshold": 30
},
"scaleAction": {
"direction": "Decrease",
"type": "ChangeCount",
"value": "1",
"cooldown": "PT5M"
}
}
]
}
],
"enabled": true,
"name": "[parameters('autoScaleSettingsName')]",
"targetResourceUri": "[resourceId('Microsoft.Compute/virtualMachineScaleSets', parameters('vmssName'))]"
}
}
]
}