10
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."

SiddhiMorajkar
  • 395
  • 4
  • 7
  • 18

1 Answers1

14

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.

felickz
  • 4,292
  • 3
  • 33
  • 37
Charles Xu
  • 29,862
  • 2
  • 22
  • 39
  • @SiddhiMorajkar Well, then please accept it as the answer. – Charles Xu Nov 08 '19 at 01:14
  • @SiddhiMorajkar Why not accept my answer when it solved your problem? You just need to click the button, this is no difficulty. – Charles Xu Nov 13 '19 at 02:48
  • I had the exact same problem. Locally, I was able to run terraform plan with no issues by having the metric names "ScaleIn" and "ScaleOut". But when I would run terraform apply within the pipeline, it would fail – ccoutinho Mar 07 '21 at 14:44
  • @ccoutinho Maybe you can add a new question with mew details. – Charles Xu Mar 08 '21 at 01:51