0

I'm trying to implement the EC2 Scaling on SQS queue size example from the AWS docs, in Terraform. Using the autoscaling module from terraform-aws-modules. I have successfully created the ASG (auto-scaling group) without the policy.

I tried adding the three metrics specified in the example:

  scaling_policies = {
    scale_on_sqs_queue_policy = {
      policy_type = "TargetTrackingScaling"
      target_tracking_configuration = {
        target_value = 100
        metric_data_queries = {
          expression = "m1 / m2"
          id = "e1"
          label = "Calculate the backlog per instance"
          return_data = true
        }
        customized_metric_specification = {
          metric_dimension = {
            name = "QueueName"
            value = "my-sqs-queue"
          }
          metric_name = "ApproximateNumberOfMessagesVisible"
          namespace = "AWS/SQS"
          statistic = "Sum"
        }
        customized_metric_specification = {
          metric_dimension = {
            name = "AutoScalingGroupName"
            value = "my_autoscaling_group_name"
          }
          metric_name = "GroupInServiceInstances"
          namespace = "AWS/Autoscaling"
          statistic = "Average"
        }
      }
    }
  }

However, only the first customized_metric_specification is added by Terraform:

module.my_autoscaling_group.aws_autoscaling_policy.this["scale_on_sqs_queue_policy"] will be created

resource "aws_autoscaling_policy" "this" {    
    arn                     = (known after apply)    
    autoscaling_group_name  = "my_autoscaling_group_name"    
    enabled                 = true    
    id                      = (known after apply)    
    metric_aggregation_type = (known after apply)    
    name                    = "scale_on_sqs_queue_policy"    
    policy_type             = "TargetTrackingScaling"    

    target_tracking_configuration {
        disable_scale_in = false
        target_value     = 100

        customized_metric_specification {
            metric_name = "GroupInServiceInstances"
            namespace   = "AWS/Autoscaling"
            statistic   = "Average"

            metric_dimension {
                name  = "AutoScalingGroupName"
                value = "my_autoscaling_group_name"
            }
        }
    }
}

Plan: 1 to add, 0 to change, 0 to destroy.

The metric math query is completely ignored too. This is also reflected in the AWS console:

screenshot of the automatic scaling page of the foo auto-scaling EC2 group page. A box on the page reflects that only one GroupInServiceInstances metric is used.

My question is, how do I use multiple metrics when defining a dynamic policy for an EC2 ASG in Terraform? It seems to be in the spec as multiple TargetTrackingMetricDataQuery objects.

I've also tried making customized_metric_specification an array or an object, which results in errors like this:

// [...]
target_tracking_configuration = {
  target_value = 100
  customized_metric_specification = {
    m1 = {
      metric_dimension = {
        name  = "QueueName"
        value = "my-sqs-queue"
      }
      metric_name = "ApproximateNumberOfMessagesVisible"
      namespace   = "AWS/SQS"
      statistic   = "Sum"
    },
    m2 = {
      metric_dimension = {
        name  = "AutoScalingGroupName"
        value = "my_autoscaling_group_name"
      }
      metric_name = "GroupInServiceInstances"
      namespace   = "AWS/Autoscaling"
      statistic   = "Average"
    }
  }
}
// [...]

Error: Unsupported attribute
on .terraform/modules/my_autoscaling_group/main.tf line 723, in resource "aws_autoscaling_policy" "this":
723:
metric_name = customized_metric_specification.value.metric_name
customized_metric_specification.value is object with 2 attributes
This object does not have an attribute named "metric_name".

I consulted these sources before posting:

dylan-myers
  • 323
  • 3
  • 18
  • 1
    You have two keys in the metric with the same name at a first glance, so that is one thing to fix. – Marko E Jan 11 '23 at 17:38
  • As in, two objects with the key `customized_metric_specification`? Or something else? @MarkoE – dylan-myers Jan 11 '23 at 17:41
  • Yup, that was what I was thinking. – Marko E Jan 11 '23 at 18:49
  • @MarkoE that would make sense, that the first block is overriden. [The code](https://github.com/terraform-aws-modules/terraform-aws-autoscaling/blob/e4ac55948cd2e6895ed41d859b6e91119761a12c/main.tf#L711-L730) would make me expect that `customized_metric_specification` could be a set of objects, but I've not been able to make that work either - see the error message I attached above – dylan-myers Jan 12 '23 at 09:55
  • It looks like the underlying [`aws_autoscaling_policy` resource](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/autoscaling_policy#customized_metric_specification) only allows one, too. I get this error: `No more than 1 "customized_metric_specification" blocks are allowed` – dylan-myers Jan 12 '23 at 11:30

1 Answers1

0

Update: This functionality is available in v4.57.0 of the Terraform AWS Provider:

resource/aws_autoscaling_policy: Add metrics to the target_tracking_configuration.customized_metric_specification configuration block in support of metric math

(source)

See docs for customized_metric_specification > metrics > expression


This functionality isn't supported in the underlying Terraform resources yet, which are made for Application Auto-scaling, not EC2 ASG. It's proposed to be added in this PR however.

dylan-myers
  • 323
  • 3
  • 18