2

I have a list of servers as below for which I need to create cloudwatch alerts. I can't seem to find many examples of this.

variable "vms" {

type = list

default = ["server1","server2","server3"]

}

I want to use for_each for my cloudwatch alarms:

resource "aws_cloudwatch_metric_alarm" "ec2-warning" {

count = length(var.vms)

for_each = {for vms in var.vms: vm.host => vms}

alarm_name = 

comparison_operator = "GreaterThanThreshold"

evaluation_periods = "1"

metric_name = "disk_used_percent"

namespace = "CWAgent"

dimensions = {

path = "/"

fstype = "xfs"

host = data.aws_instance.myec2.private_

dnsdevice = "xvda1"

}

Edit: I believe i need to do something like this

locals {
  my_list = [
    "server1",
    "server2",
    "server3",
    "server4"
  ]
}

resource "aws_cloudwatch_metric_alarm" "ec2-disk-space-warning-for" {
  for_each = toset(local.my_list)
  alarm_name          = {each.key}-"ec2-disk-space-warning"
  comparison_operator = "GreaterThanThreshold"
  evaluation_periods  = "1"
  metric_name         = "disk_used_percent"
  namespace           = "CWAgent"
  dimensions = {
    path   = "/"
    fstype = "xfs"
    host   = {each.key}
    device = "xvda1"
  }
user2499710
  • 119
  • 1
  • 8
  • 2
    Hi, I also noticed that you have a number of questions asked. All of them have answers, yet none were accepted. Its good practice to accept answers that were helpful, as this limits the number of duplicates and informs other users in future with similar or same problems, that the answer given are correct. – Marcin Jan 01 '21 at 12:38
  • Correct. I can't accept my answer for another 17 hours though. – user2499710 Jan 02 '21 at 18:01

2 Answers2

1

You can use your var.vms if you want. There is no need for locals. However, in your first attempt you can't use count and for_each at the same time. In your second attempt, you are missing some arguments (statistic and period) and using incorrectly string interpolation.

Thus, the following should be tried:

variable "vms" {
   type = list
   default = ["server1","server2","server3"]
}


resource "aws_cloudwatch_metric_alarm" "ec2-disk-space-warning-for" {
  for_each            = toset(var.vms)
  alarm_name          = "${each.key}-ec2-disk-space-warning"
  comparison_operator = "GreaterThanThreshold"
  evaluation_periods  = "1"
  metric_name         = "disk_used_percent"
  namespace           = "CWAgent"
  statistic           = "Average"
  period              = 60
  dimensions = {
    path   = "/"
    fstype = "xfs"
    host   = each.key
    device = "xvda1"
  }
}

The alarms will not work if your custom metrics don't exist, but I assume that the metrics are working and set correctly.

Marcin
  • 215,873
  • 14
  • 235
  • 294
  • I also realised I have dimensions = { instanceid = data.aws_instance.myec2.instance_id instancename = data.aws_instance.myec2.private_dns } but not sure how these would work with for_each. Looks like I need a for_each for data? – user2499710 Jan 02 '21 at 18:02
  • @user2499710 I think it would be better to make new question for this new issue with relevant details. – Marcin Jan 02 '21 at 21:31
0

This finally worked for me

locals {
  my_list = [
    "server1",
    "server2",
    "server3",
    "server4"
  ]
}

resource "aws_cloudwatch_metric_alarm" "ec2-disk-space-warning" {
  for_each            = toset(local.my_list)
  alarm_name          = "ec2-disk-space-warning-for-${each.key}"
  comparison_operator = "GreaterThanThreshold"
  evaluation_periods  = "1"
  metric_name         = "disk_used_percent"
  namespace           = "CWAgent"
  dimensions = {
    path   = "/"
    fstype = "xfs"
    host   = each.key
    device = "xvda1"
  }
user2499710
  • 119
  • 1
  • 8
  • I'm surprised this works, because I thought `threshold` would be needed. – John Jan 01 '21 at 20:54
  • @John `threshold` is not needed, by default `0` is going to be used. But `statistic` and `period` are needed, so the alarm will not deploy anyway as you've noticed. – Marcin Jan 01 '21 at 21:57
  • Both of you are correct. This was just a snippet of the problem code. Apologies for the confusion. – user2499710 Jan 02 '21 at 18:02
  • @user2499710 Can I know why did you umarked my answer and accepted your own whish is based on mine. ALso your answer does not deploy as explained in the comments. – Marcin Jan 04 '21 at 10:51
  • 1
    @marcin , sorry that was by mistake. fixed it. Thank you for helping. – user2499710 Jan 04 '21 at 13:49
  • Thank you! Will do. Could you also help with "Lookup two lists in one resource" question. – user2499710 Jan 04 '21 at 22:01