0

Based on the autoscale rule criteria I am trying to create a terraform azurerm_monitor_autoscale_setting rule which increase/decreases VM nodes based on Disk Write Operations/Sec or Disk Write Bytes. The metric trigger looks like the following:

rule {
     metric_trigger {
            operator           = "GreaterThan"
            threshold          = 50
            time_grain         = "PT1M"
            statistic          = "Average"
            time_window        = "PT5M"
            metric_name        = "Disk Write Operations/Sec"
            time_aggregation   = "Average"
    }
}

However, I believe this is not the right syntax since once applying the terraform script it never creates this rule. On the other hand replacing the rule with "Percentage CPU" works fine. Have anyone created any metrics other than "Percentage CPU"? Can you help figuring out what is the right way to do it in terraform?

yanis
  • 303
  • 3
  • 15

1 Answers1

1

You lack the metric_resource_id. You could try the following example, this works on my side.

resource "azurerm_monitor_autoscale_setting" "test" {
  name                = "myAutoscaleSetting"
  resource_group_name = "${azurerm_resource_group.test.name}"
  location            = "${azurerm_resource_group.test.location}"
  target_resource_id  = "${azurerm_virtual_machine_scale_set.test.id}"

  profile {
    name = "defaultProfile"

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

    rule {
      metric_trigger {
        metric_name        = "Disk Write Operations/Sec"
        metric_resource_id = "${azurerm_virtual_machine_scale_set.test.id}"
        time_grain         = "PT1M"
        statistic          = "Average"
        time_window        = "PT5M"
        time_aggregation   = "Average"
        operator           = "GreaterThan"
        threshold          = 75
      }

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

    rule {
      metric_trigger {
        metric_name        = "Disk Write Operations/Sec"
        metric_resource_id = "${azurerm_virtual_machine_scale_set.test.id}"
        time_grain         = "PT1M"
        statistic          = "Average"
        time_window        = "PT5M"
        time_aggregation   = "Average"
        operator           = "LessThan"
        threshold          = 25
      }

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

  notification {
    email {
      send_to_subscription_administrator    = true
      send_to_subscription_co_administrator = true
      custom_emails                         = ["admin@contoso.com"]
    }
  }
}

Result: enter image description here enter image description here

Nancy
  • 26,865
  • 3
  • 18
  • 34
  • In my case the Disk Write Operations/Sec 's `metric_resource_id` will be the same as the one for Percentage CPU. Does that mean that `metric_name` is what Azure uses to determine which metric actually to use? – yanis Oct 01 '19 at 14:21
  • Yes, you can specify the `metric_name`, any update on this thread? – Nancy Oct 07 '19 at 01:19