I want to create a Cloudwatch alarm for Memory Utilisation across an ASG.
This is the SQL query I want to replicate in Terraform:
SELECT AVG(mem_used_percent)
FROM SCHEMA(CWAgent, AutoScalingGroupName,ImageId,InstanceId,InstanceType)
WHERE AutoScalingGroupName = 'my-asg-name'
This is my current Terraform code:
resource "aws_cloudwatch_metric_alarm" "memory_utilization_high" {
alarm_name = "HighMemoryUtilizationAcrossASG"
comparison_operator = "GreaterThanOrEqualToThreshold"
evaluation_periods = 3
threshold = 80
alarm_actions = [aws_sns_topic.asg_alerts.arn]
treat_missing_data = "breaching"
metric_query {
id = "e1"
expression = "AVG([m1])"
label = "Average Memory Utilisation across ASG"
return_data = "true"
}
metric_query {
id = "m1"
metric {
metric_name = "mem_used_percent"
namespace = "CWAgent"
period = 300
stat = "Average"
unit = "Percent"
dimensions = {
AutoScalingGroupName = var.asg_name
InstanceId = ""
InstanceType = ""
ImageId = ""
}
}
}
}
I get this Terraform error:
error ValidationError: 3 validation errors detected: Value '' at 'metrics.2.member.metricStat.metric.dimensions.2.member.value' failed to satisfy constraint: Member must have length greater than or equal to 1; Value '' at 'metrics.2.member.metricStat.metric.dimensions.3.member.value' failed to satisfy constraint: Member must have length greater than or equal to 1; Value '' at 'metrics.2.member.metricStat.metric.dimensions.4.member.value' failed to satisfy constraint: Member must have length greater than or equal to 1
I have tried removing these dimensions:
InstanceId = ""
InstanceType = ""
ImageId = ""
and Terraform is happy with it but Cloudwatch doesn’t show any metrics. The SQL query at the top though does what I need.
Anybody has any idea how to fix this? Thanks!
5