Search code examples
azureterraformterraform-provider-azureazure-storage-queues

Azure / Terraform - azurerm_monitor_autoscale_setting with storage queues


I want to create a rule to scale an app service plan when the number of messages in a queue is greater than some value.

The queue definition looks something like this:

resource "azurerm_storage_queue" "myqueue" {
  name                 = "myqueue"
  storage_account_name = "storageaccountname"
}

The scaling rule looks something like this:

    rule {
      metric_trigger {
        metric_name        = "ApproximateMessageCount"
        metric_resource_id = azurerm_storage_queue.myqueue.id
        time_grain         = "PT1M"
        statistic          = "Average"
        time_window        = "PT1M"
        time_aggregation   = "Average"
        operator           = "GreaterThanOrEqual"
        threshold          = 100
      }

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

The issue is in the metric_resource_id - I'm not sure exactly what attribute or ID it expects. The error message is The number of path segments is not divisible by 2. I believe this is because the id for the queue is just its name, and the resource_id for this should be something like /subscriptions/xxxxxxx/resourceGroups/xxxxxx/providers/Microsoft.Storage/storageAccounts/storageaccountname/services/queue/queues/myqueue" but I'm not sure how to get this from terraform.


Solution

  • The output ofazurerm_storage_queue.myqueue.id is a Url format https://storageaccountname.queue.core.windows.net/mysamplequeue.

    While metric_resource_id requires that the resource ID of the resource that emits the metric. As far as I know, we can not get the resource format directly from Terraform resource attributes but we could self-construct the resource id to

    metric_resource_id = join("/",["${azurerm_storage_account.example.id}","services/queue/queues","mysamplequeue"])
    

    For example,

    rule {
      metric_trigger {
        metric_name        = "ApproximateMessageCount"
        metric_resource_id = join("/",["${azurerm_storage_account.example.id}","services/queue/queues","myqueue"])
        time_grain         = "PT1M"
        statistic          = "Average"
        time_window        = "PT5M"
        time_aggregation   = "Average"
        operator           = "GreaterThanOrEqual"
        threshold          = 100
      }