Terraform: Passing list into Dimension field in Cloudwatch alarm?

Created on 1 Sep 2016  ยท  5Comments  ยท  Source: hashicorp/terraform

v0.7.2
Could anyone suggest the correct syntax to pass a list of instances in the dimensions filed for a cloudwatch alarm?

resource "aws_cloudwatch_metric_alarm" "cpu_credits" {
  alarm_name = "test - Low CPU Credits"
  comparison_operator = "LessThanOrEqualToThreshold"
  #dimensions { InstanceId = "${var.instID}" }
  dimensions { InstanceId = "i-07656896d6947814c", InstanceId = "i-042e664331a74e385" }
  evaluation_periods = "10"
  metric_name = "CPUCreditBalance"
  namespace = "AWS/EC2"
  period = "120"
  statistic = "Average"
  threshold = "200"
  alarm_description = "This metric monitors low ec2 cpu credits for T2 instances"
  insufficient_data_actions = []
  alarm_actions = ["arn:aws:sns:eu-west-1:623990728151:CloudwatchAlerts"]
}
provideaws question

Most helpful comment

Some ways to accomplish what you need follows, I've added 2 alternatives for selecting dimensions, count would need to match the count of instances you have:

resource "aws_cloudwatch_metric_alarm" "cpu_credits" {
  count = 2
  ...
  dimensions { InstanceId = "${element(list("i-07656896d6947814c", "i-042e664331a74e385"), count.index)}" }
  dimensions { InstanceId = "${element(aws_instance.my_instance.*.id, count.index)}" }
}

All 5 comments

+1

Some ways to accomplish what you need follows, I've added 2 alternatives for selecting dimensions, count would need to match the count of instances you have:

resource "aws_cloudwatch_metric_alarm" "cpu_credits" {
  count = 2
  ...
  dimensions { InstanceId = "${element(list("i-07656896d6947814c", "i-042e664331a74e385"), count.index)}" }
  dimensions { InstanceId = "${element(aws_instance.my_instance.*.id, count.index)}" }
}

Was this addressed in a newer release?

@downspot looks like this issue was migrated to https://github.com/terraform-providers/terraform-provider-aws/issues/248 during the core-provider split for 0.10 (June 13th, 2017), however our issue migration application never commented back in this ticket that it was migrated, its just referenced above (at least on Github web UI). The migrated issue is still open in the new repository, so might be worth commenting or asking questions there.

I'm going to lock this issue because it has been closed for _30 days_ โณ. This helps our maintainers find and focus on the active issues.

If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

Was this page helpful?
0 / 5 - 0 ratings