Terraform: How to specify metric dimension

Created on 28 Sep 2015  ยท  7Comments  ยท  Source: hashicorp/terraform

Hello gentlemen, I've looked and looked but I can't find a single example of how to specify the dimensions of aws_cloudwatch_metric_alarm
Here is what I currently have and I keep getting an error at the "dimensions" line. Kindly help me learn how I can define the line correctly

resource "aws_cloudwatch_metric_alarm" "WAFCPUAlarmHigh" {
    alarm_name = "WAFCPUAlarmHigh"
    comparison_operator = "GreaterThanThreshold"
    dimensions = {Name="WAFAutoScalingGroup", Value="${aws_autoscaling_group.WAFAutoScalingGroup.name}"}
    evaluation_periods = "1"
    metric_name = "CPUUtilization"
    namespace = "AWS/EC2"
    period = "300"
    statistic = "Average"
    threshold = "80"
    alarm_description = "Scale-up if CPU > 80% for 300 seconds"
    alarm_actions = ["${aws_autoscaling_policy.WAFServerGroupScaleUpPolicy.arn}", "${var.sns_topic_arn}"]
    insufficient_data_actions = ["${var.sns_topic_arn}"]
    ok_actions = ["${var.sns_topic_arn}"]

Most helpful comment

Hi! I have configured some cloudwatch alarms, and for EC2 CPUUtilization, I don't have to specify the dimension, but for SQS alarms this is mandatory. This must to be a map, but I think the correct syntax is like that:

resource "aws_cloudwatch_metric_alarm" "feeds-qaConvertQueueConsumer-plus-10k" {
    alarm_name = "feeds-qaConvertQueueConsumer-plus-10k"
    comparison_operator = "GreaterThanOrEqualToThreshold"
    evaluation_periods = "1"
    metric_name = "ApproximateNumberOfMessagesVisible"
    dimensions {
      QueueName = "feeds-qaConvertQueueConsumer"
    }
    namespace = "AWS/SQS"
    period = "600"...

Hope this helps!

All 7 comments

Hi! I have configured some cloudwatch alarms, and for EC2 CPUUtilization, I don't have to specify the dimension, but for SQS alarms this is mandatory. This must to be a map, but I think the correct syntax is like that:

resource "aws_cloudwatch_metric_alarm" "feeds-qaConvertQueueConsumer-plus-10k" {
    alarm_name = "feeds-qaConvertQueueConsumer-plus-10k"
    comparison_operator = "GreaterThanOrEqualToThreshold"
    evaluation_periods = "1"
    metric_name = "ApproximateNumberOfMessagesVisible"
    dimensions {
      QueueName = "feeds-qaConvertQueueConsumer"
    }
    namespace = "AWS/SQS"
    period = "600"...

Hope this helps!

@sherabi FYI, @erickfaustino is 100% correct. I just ran an acceptance test (builtin/providers/aws/resource_aws_cloudwatch_metric_alarm_test.go) with the following sets of data:

resource "aws_cloudwatch_metric_alarm" "foobar" {
    alarm_name = "terraform-test-foobar5"
    comparison_operator = "GreaterThanOrEqualToThreshold"
    evaluation_periods = "2"
    metric_name = "CPUUtilization"
    dimensions = {
        Name="WAFAutoScalingGroup"
        Value="test"
    }
    namespace = "AWS/EC2"
    period = "120"
    statistic = "Average"
    threshold = "80"
    alarm_description = "This metric monitor ec2 cpu utilization"
    insufficient_data_actions = []
}

This gave me the result:

make testacc TEST=./builtin/providers/aws TESTARGS='-run=CloudWatchMetric' 2>~/tf.log
go generate ./...
TF_ACC=1 go test ./builtin/providers/aws -v -run=CloudWatchMetric -timeout 90m
=== RUN TestAccAWSCloudWatchMetricAlarm_basic
--- PASS: TestAccAWSCloudWatchMetricAlarm_basic (3.30s)
=== RUN TestAccAWSCloudWatchMetricAlarm_withDimensions
--- PASS: TestAccAWSCloudWatchMetricAlarm_withDimensions (1.85s)

then with your style config

resource "aws_cloudwatch_metric_alarm" "foobar" {
    alarm_name = "terraform-test-foobar5"
    comparison_operator = "GreaterThanOrEqualToThreshold"
    evaluation_periods = "2"
    metric_name = "CPUUtilization"
    dimensions = {Name="WAFAutoScalingGroup",Value="test"}
    namespace = "AWS/EC2"
    period = "120"
    statistic = "Average"
    threshold = "80"
    alarm_description = "This metric monitor ec2 cpu utilization"
    insufficient_data_actions = []
}

and got the following result

make testacc TEST=./builtin/providers/aws TESTARGS='-run=CloudWatchMetric' 2>~/tf.log
go generate ./...
TF_ACC=1 go test ./builtin/providers/aws -v -run=CloudWatchMetric -timeout 90m
=== RUN TestAccAWSCloudWatchMetricAlarm_basic
--- PASS: TestAccAWSCloudWatchMetricAlarm_basic (2.47s)
=== RUN TestAccAWSCloudWatchMetricAlarm_withDimensions
--- FAIL: TestAccAWSCloudWatchMetricAlarm_withDimensions (0.01s)
    testing.go:136: Step 0 error: Error loading configuration: Error parsing /var/folders/gd/yv86v7pd1cd5dpzjrw8702k00000gn/T/tf-test874107579/main.tf: Line 7, column 46: syntax error
FAIL
exit status 1
FAIL    github.com/hashicorp/terraform/builtin/providers/aws    2.491s

@sherabi have you tried this? Is this ready to be closed?

Thanks for responding guys I think the key was to not have a comma between the name and value and also did not require the square brackets "[ ]". I used them coz the documentation stated that dimensions is a list

dimensions = {
      Name = "WAFAutoScalingGroup"
      Value = "${aws_autoscaling_group.WAFAutoScalingGroup.name}"
    }

@catsby / @phinze / @radeksimko please can we close this now? :)

Following worked for me:

dimensions = {
  InstanceId = "${aws_instance.dc2.id}",
  instance = "C:",
  objectname = "LogicalDisk"
}

, might not be necessary

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

Related issues

rjinski picture rjinski  ยท  3Comments

c4milo picture c4milo  ยท  3Comments

darron picture darron  ยท  3Comments

rjinski picture rjinski  ยท  3Comments

ronnix picture ronnix  ยท  3Comments