Search code examples
azureazure-rm-templateazure-bicep

Azure Deployment: UnsupportedMetric on first deploy


We are facing an annoying problem where we are getting an error on the first deployment attempt of a fresh environment:

resource: /subscriptions/XX/resourceGroups/rg-YY/providers/Microsoft.Web/serverfarms/plan-ZZ, 
metricnamespace: microsoft.web/serverfarms, metricname: CpuPercentage (Code: UnsupportedMetric)

If we redeploy then everything works. The metric 'CpuPercentage' should be supported according to the documentation: https://learn.microsoft.com/en-us/azure/azure-monitor/essentials/metrics-supported#microsoftwebserverfarms

Our Bicep code:

resource appServicePlan 'Microsoft.Web/serverfarms@2022-03-01' = {
  name: appName
  location: location
  kind: 'windows'
  properties: {
    reserved: false
    maximumElasticWorkerCount: 10
  }
  sku: {
    tier: tier
    name: skuName
  }
  tags: tags
}


resource autoscaleSettings 'Microsoft.Insights/autoscalesettings@2015-04-01' = {
  name: 'autoscale-${appName}'
  location: location
  tags: tags
  properties: {
    targetResourceLocation: location
    targetResourceUri: appServicePlan.id
    enabled: true
    name: 'autoscale-${appName}'
    profiles: [
      {
        capacity: {
          default: '1'
          maximum: '3'
          minimum: '1'
        }
        name: 'DefaultAutoscaleProfile'
        rules: [
          {
            metricTrigger: {
              dimensions: []
              metricName: 'CpuPercentage'
              metricNamespace: 'microsoft.web/serverfarms'
              metricResourceUri: appServicePlan.id
              dividePerInstance: false
              timeGrain: 'PT1M'
              statistic: 'Average'
              timeWindow: 'PT10M'
              timeAggregation: 'Average'
              operator: 'GreaterThan'
              threshold: 70
            }
            scaleAction: {
              cooldown: 'PT10M'
              direction: 'Increase'
              type: 'ChangeCount'
              value: '1'
            }
          }
          {
            metricTrigger: {
              dimensions: []
              metricName: 'CpuPercentage'
              metricNamespace: 'microsoft.web/serverfarms'
              metricResourceUri: appServicePlan.id
              dividePerInstance: false
              timeGrain: 'PT1M'
              statistic: 'Average'
              timeWindow: 'PT10M'
              timeAggregation: 'Average'
              operator: 'LessThan'
              threshold: 45
            }
            scaleAction: {
              cooldown: 'PT10M'
              direction: 'Decrease'
              type: 'ChangeCount'
              value: '1'
            }
          }
        ]
      }
    ]    
  }
}

Any ideas as to what might cause this or if there is any workaround? Tried to split into separate modules to introduce a little bit of delay but that does not seem to work either.


Solution

  • Azure Deployment: Unsupported Metric on first deploy:

    Usually, unsupported Metric error comes when the metrics given in the code is not supported for specific Azure resource.

    But "CpuPercentage" should be supported as it is also given in MS Doc clearly. It may take some more time for the metrics to become available for newly added resources.

    In such case, redeploying the deployment frequently resolves the issue because the metrics will be available by then.

    Though redeploying fixes the issue quickly, there is another way to resolve this. You can add a deployment script in the existing code by adding a timeout flag with some time period for deployment. Use deployment script templates and alter the timeout flag value as per your environment.

    resource  awaitscript  'Microsoft.Resources/deploymentScripts@2020-10-01' = {
    name: ''
    location: location
    kind: 'AzurePowerShell'
    properties: {
    azPowerShellVersion: '9.7'
    scriptContent: '''
    timeout: 'PT40M'
    retentionInterval: ''
    '''
    }
    }
    

    Once you added the above one, Add a dependsOn block to point out the await script & an app service plan as shown.

    resource autoscaleSettings 'Microsoft.Insights/autoscalesettings@2015-04-01' = {
      name: 'autoscale-lalluapp'
    ....code...
    dependsOn: [
        appServicePlan,
        awaitscript
      ]
    }
    

    I tried the same code as you in my environment without adding any deployment scripts and it worked for me in the first deployment itself.

    main.bicep:

    param webAppName string = ''
    param location string = resourceGroup().location 
    var appServiceplanname = ''
    
    resource appServicePlan 'Microsoft.Web/serverfarms@2020-06-01' = {
      name: appServiceplanname
      location: location
      properties: {
        reserved: true
      }
      sku: {
        name: sku
      }
      kind: 'linux'
    }
    
    resource autoscaleSettings 'Microsoft.Insights/autoscalesettings@2015-04-01' = {
      name: 'autoscale-lalluapp'
      location: location
      //tags: ''
      properties: {
        targetResourceLocation: location
        targetResourceUri: appServicePlan.id
        enabled: true
        name: 'autoscale-lalluapp'
        profiles: [
          {
            capacity: {
              default: '1'
              maximum: '3'
              minimum: '1'
            }
            name: 'DefaultAutoscaleProfile'
            rules: [
              {
                metricTrigger: {
                  dimensions: []
                  metricName: 'CpuPercentage'
                  metricNamespace: 'microsoft.web/serverfarms'
                  metricResourceUri: appServicePlan.id
                  dividePerInstance: false
                  timeGrain: 'PT1M'
                  statistic: 'Average'
                  timeWindow: 'PT10M'
                  timeAggregation: 'Average'
                  operator: 'GreaterThan'
                  threshold: 70
                }
                scaleAction: {
                  cooldown: 'PT10M'
                  direction: 'Increase'
                  type: 'ChangeCount'
                  value: '1'
                }
              }
              {
                metricTrigger: {
                  dimensions: []
                  metricName: 'CpuPercentage'
                  metricNamespace: 'microsoft.web/serverfarms'
                  metricResourceUri: appServicePlan.id
                  dividePerInstance: false
                  timeGrain: 'PT1M'
                  statistic: 'Average'
                  timeWindow: 'PT10M'
                  timeAggregation: 'Average'
                  operator: 'LessThan'
                  threshold: 45
                }
                scaleAction: {
                  cooldown: 'PT10M'
                  direction: 'Decrease'
                  type: 'ChangeCount'
                  value: '1'
                }
              }
            ]
          }
        ]
      }
    }
    

    main.parameters.json:

    {
    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentParameters.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
    "webAppName": {
    "value": "xxxxxapp"
    },
    "sku": {
    "value": "F1"
    },
    "linuxFxVersion": {
    "value": "xxxx"
         }
       }
    }
    

    Deployment succeeded:

    enter image description here

    enter image description here

    enter image description here

    Refer MS github for the relevant issues.