Terraform-provider-aws: Passing list into Dimension field in Cloudwatch alarm?

Created on 13 Jun 2017  ยท  5Comments  ยท  Source: hashicorp/terraform-provider-aws

_This issue was originally opened by @davyt10 as hashicorp/terraform#8600. It was migrated here as part of the provider split. The original body of the issue is below._


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"]
}
question serviccloudwatch

Most helpful comment

Hi @davyt10
Sorry for the very delayed response!
AWS CloudWatch alarms are instance-specific. If you want to monitor multiple instances, you would need to create an alarm for each instance.

Here's an example that creates multiple alarms without repeated code:

```hcl

variable "instances" {
default = ["i-07656896d6947814c", "i-042e664331a74e385"]
}

resource "aws_cloudwatch_metric_alarm" "cpu_credits" {
count = "${length(var.instances)}"
## alarm names must be unique in an AWS account
alarm_name = "test-${count.index} - Low CPU Credits
comparison_operator = "LessThanOrEqualToThreshold"
#dimensions { InstanceId = "${var.instID}" }
dimensions { InstanceId = "${element(var.instances, count.index)}"
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"]
}```

All 5 comments

Hi @davyt10
Sorry for the very delayed response!
AWS CloudWatch alarms are instance-specific. If you want to monitor multiple instances, you would need to create an alarm for each instance.

Here's an example that creates multiple alarms without repeated code:

```hcl

variable "instances" {
default = ["i-07656896d6947814c", "i-042e664331a74e385"]
}

resource "aws_cloudwatch_metric_alarm" "cpu_credits" {
count = "${length(var.instances)}"
## alarm names must be unique in an AWS account
alarm_name = "test-${count.index} - Low CPU Credits
comparison_operator = "LessThanOrEqualToThreshold"
#dimensions { InstanceId = "${var.instID}" }
dimensions { InstanceId = "${element(var.instances, count.index)}"
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"]
}```

This is useful but I am wandering how to do the same thing when you are also creating the instances with terraform? I tried locals

locals {
  instance_ids = ["${aws_instance.host1.id}", "${aws_instance.host2.id}", "${aws_instance.host3.id}"]
}

But later on when I use the count I get the following

value of 'count' cannot be computed
```

Closing due to lack of response from original author.

For tracking value of 'count' cannot be computed issues, I would recommend looking upstream in the Terraform core issue tracker, as count is not handled by Terraform providers themselves. See also: https://github.com/hashicorp/terraform/issues?utf8=%E2%9C%93&q=is%3Aissue+is%3Aopen+value+of+count+cannot+be+computed

AWS CloudWatch alarms are instance-specific.

No, they are attribute specific. It is perfectly valid to define dimensions that use tags, or ami-id, or instance size as selection criteria. The more interesting question is whether the provider can be extended to support an array of multiple types. The typical 'hack' is to pass a comma-delimited string with no delimiting spaces but one would have to read the source to see how the code is written and whether it handles that case.

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 feel this issue should be reopened, we encourage creating a new issue linking back to this one for added context. Thanks!

Was this page helpful?
0 / 5 - 0 ratings