Search code examples
azureazure-web-app-serviceazure-app-service-plansterraform-provider-azureapp-service-environment

Error creating auto-scaling rule for app service using terraform in azure


resource "azurerm_monitor_autoscale_setting" "test" {
  name                = "AutoscaleSetting"
  resource_group_name = "${azurerm_resource_group.main.name}"
  location            = "${azurerm_resource_group.main.location}"
  target_resource_id  = "${azurerm_app_service_plan.main.id}"

 profile {
name = "defaultProfile"

capacity {
  default = 1
  minimum = 1
  maximum = 10
}

rule {
  metric_trigger {
    metric_name        = "Percentage CPU"
    metric_resource_id = "${azurerm_app_service_plan.main.id}"
    time_grain         = "PT1M"
    statistic          = "Average"
    time_window        = "PT5M"
    time_aggregation   = "Average"
    operator           = "GreaterThan"
    threshold          = 80
  }

  scale_action {
    direction = "Increase"
    type      = "ChangeCount"
    value     = "1"
    cooldown  = "PT1M"
  }
}

rule {
  metric_trigger {
    metric_name        = "Percentage CPU"
    metric_resource_id = "${azurerm_app_service_plan.main.id}"
    time_grain         = "PT1M"
    statistic          = "Average"
    time_window        = "PT5M"
    time_aggregation   = "Average"
    operator           = "LessThan"
    threshold          = 80
  }

  scale_action {
    direction = "Decrease"
    type      = "ChangeCount"
    value     = "1"
    cooldown  = "PT1M"
  }
}}   

I tried setting an auto scaling rule in terraform on azure. While doing so it threw this error. kindly help with this. What is this error and how can this error be solved?

Error : Error creating AutoScale Setting "AutoscaleSetting" (Resource Group "sm-prod-resources"): insights.AutoscaleSettingsClient#CreateOrUpdate: Failure responding to request: StatusCode=400 -- Original Error: autorest/azure: Service returned an error. Status=400 Code="UnsupportedMetric" Message="Exception of type 'Microsoft.WindowsAzure.Management.Monitoring.MonitoringServiceException' was thrown."


Solution

  • The error shows that it's an UnsupportedMetric. According to the document in Terraform, it describes like this:

    metric_name - (Required) The name of the metric that defines what the rule monitors, such as Percentage CPU for Virtual Machine Scale Sets and CpuPercentage for App Service Plan.

    I think it's just a mistake you made, the name with "Percentage CPU" is for Virtual Machine Scale Sets, you need to change it into "CpuPercentage", it's for App Service Plan as you want. For details, see metric_name.