Reputation: 360
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:
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:
aws_autoscaling_policy
docs which seem to allow thisUpvotes: 0
Views: 791
Reputation: 360
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.
Upvotes: 0