Search code examples
azurearmazure-rm-templateazure-autoscaling-block

Not able to create autoscale in and out property for my VMSS template plan using ARM template


I am trying to create ARM template to add scale in and scale out property inside Resources as per below using above template as reference. Other resources have been created but ARM is unable to create any type of autoscale group in my VMSS template.

{
            "type": "microsoft.insights/autoscalesettings",
            "apiVersion": "2015-04-01",
            "name": "AutoScaleSet",
            "location": "[parameters('location')]",
            "dependsOn": [
                "[resourceId('Microsoft.Compute/virtualMachineScaleSets/', parameters('VMSS_Name'))]"
            ],
            "properties": {
                "name": "autoscalehost",
                "targetResourceUri": "[resourceId('Microsoft.Compute/virtualMachineScaleSets/', parameters('VMSS_Name'))]",
                "enabled": true,
                "profiles": [
                    {
                        "name": "autoscalehost",
                        "capacity": {
                            "minimum": "1",
                            "maximum": "3",
                            "default": "1"
                        },
                        "rules": [
                            {
                                "metricTrigger": {
                                    "metricName": "Percentage CPU",
                                    "metricResourceUri": "[concat('Microsoft.Compute/virtualMachineScaleSets', parameters('VMSS_Name'))]",
                                    "timeGrain": "PT1M",
                                    "statistic": "Average",
                                    "timeWindow": "PT5M",
                                    "timeAggregation": "Average",
                                    "operator": "GreaterThan",
                                    "threshold": 50
                                },
                                "scaleAction": {
                                    "direction": "Increase",
                                    "type": "ChangeCount",
                                    "value": "1",
                                    "cooldown": "PT5M"
                                }
                            },
                            {
                                "metricTrigger": {
                                    "metricName": "Percentage CPU",
                                    "metricResourceUri": "[concat('Microsoft.Compute/virtualMachineScaleSets', parameters('VMSS_Name'))]",
                                    "timeGrain": "PT1M",
                                    "statistic": "Average",
                                    "timeWindow": "PT5M",
                                    "timeAggregation": "Average",
                                    "operator": "LessThan",
                                    "threshold": 30
                                },
                                "scaleAction": {
                                    "direction": "Decrease",
                                    "type": "ChangeCount",
                                    "value": "1",
                                    "cooldown": "PT5M"
                                }
                            }
                        ]
                    }
                ]
            }
        }

Solution

  • I can see two separate issues here:

    1) The name for the autoscale setting is 'AutoScaleSet' in one position and 'autoscalehost' in another:

        {
              "type": "microsoft.insights/autoscalesettings",
              "apiVersion": "2015-04-01",
    HERE -->  "name": "AutoScaleSet",             
              "location": "[parameters('location')]",
              "dependsOn": [
                "[resourceId('Microsoft.Compute/virtualMachineScaleSets/', parameters('VMSS_Name'))]"
              ],
              "properties": {
    HERE -->    "name": "autoscalehost",
                "targetResourceUri": "[resourceId('Microsoft.Compute/virtualMachineScaleSets/', parameters('VMSS_Name'))]",
                "enabled": true
              }
    

    This needs to be the same value or the deployment fails with the following error:

    {
      "code": "BadRequest",
      "message": "The autoscale setting that you provided does not have the same name as the name in the request. Name in autoscale setting: autoscalehost. Name in the request: AutoScaleSet"
    }
    

    2) The proper resource ID is not provided to the metricTrigger rule

      {
        "metricTrigger": {
        "metricName": "Percentage CPU",
    --> "metricResourceUri": "[concat('Microsoft.Compute/virtualMachineScaleSets', parameters('VMSS_Name'))]", <--
        "timeGrain": "PT1M"
      }
    

    You need the full resource ID here. Not only provider name + name of VMSS. Otherwise the deployment will fail with the following error:

    {
      "code": "LinkedInvalidPropertyId",
      "message": "Property id 'Microsoft.Compute/virtualMachineScaleSetssotest' is invalid. Expect fully qualified resource Id that start with '/subscriptions/{subscriptionId}' or '/providers/{resourceProviderNamespace}/'."
    }
    

    Use the resourceId() function instead of concat() in your template like this:

    [resourceId('Microsoft.Compute/virtualMachineScaleSets/', parameters('VMSS_Name'))]