2

I’m having trouble understanding how to create a loop for my resource below. I need to create multiples resources based on the variable "instance" which is nested within the "dimentions" block. From my understanding after reading some documentation, I should use the for_each parameter, however I can't seem to figure it out.

resource "aws_cloudwatch_metric_alarm" "disk_percentage_low" {
  alarm_name                = "disk_percentage_low"
  comparison_operator       = "LessThanOrEqualToThreshold"
  evaluation_periods        = "1"
  metric_name               = "disk_used_percent"
  namespace                 = "AWS/CWAgent"
  period                    = "60"
  statistic                 = "Average"
  threshold                 = "20"
  alarm_description         = "This metric monitors ec2 disk utilization"
  actions_enabled           = "true" 
  alarm_actions             = [aws_sns_topic.disk_alarm.arn]
  insufficient_data_actions = []
 
  dynamic = "dimensions" {
    for_each = var.instance
    content { 
      instanceid   = var.instanceid
      instancetype = var.instancetype
      imageid      = var.imageid
      instance     = var.instance.value 
    }
  }  
 }

variable "instance" {
  type = list
  default = ["E:" , "D:"]
} 

 

I'm pretty sure I'm doing this wrong, but I've been messing around with it for a few days now and have tried a few different things, none of which seem to work. Any help would be much appreciated, thanks.

1 Answer 1

2

You can't use multiple dimensions in a single alarm in aws_cloudwatch_metric_alarm. Your dimension can be only:

  dimensions = 
      instanceid   = var.instanceid
      instancetype = var.instancetype
      imageid      = var.imageid
      instance     = var.instance[0]
    }

or

  dimensions = 
      instanceid   = var.instanceid
      instancetype = var.instancetype
      imageid      = var.imageid
      instance     = var.instance[1]
    }

If you want alarm for the two values in var.instance you have to create *two alarms:

resource "aws_cloudwatch_metric_alarm" "disk_percentage_low" {

  for_each                  = toset(var.instance)

  alarm_name                = "disk_percentage_low"
  comparison_operator       = "LessThanOrEqualToThreshold"
  evaluation_periods        = "1"
  metric_name               = "disk_used_percent"
  namespace                 = "AWS/CWAgent"
  period                    = "60"
  statistic                 = "Average"
  threshold                 = "20"
  alarm_description         = "This metric monitors ec2 disk utilization"
  actions_enabled           = "true" 
  alarm_actions             = [aws_sns_topic.disk_alarm.arn]
  insufficient_data_actions = []
 
  dimensions = {
      instanceid   = var.instanceid
      instancetype = var.instancetype
      imageid      = var.imageid
      instance     = each.value
    } 
 }
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for the quick response. I've just tried this and I get the following error: Error: Unsupported block type on alert.tf line 15, in resource "aws_cloudwatch_metric_alarm" "disk_percentage_low": 15: dynamic "dimensions" { Blocks of type "dimensions" are not expected here.
This was an error I was getting before which is why I added the equals sign after "dimensions". However doing that throws out the following error: on alert.tf line 15, in resource "aws_cloudwatch_metric_alarm" "disk_percentage_low": 15: dynamic = "dimensions" { An argument definition must end with a newline.
@ish I see now. Let me delete my answer for know.
thank you so much that worked! I used the later option to create two alarms so that this code can be applied to any instance regardless of how many disk partitions an instance has.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.