Hi,
I have declared some AWS EC2 instances with terraform
resource aws_instance "server1" {
# ...
}
resource aws_instance "server2" {
# ...
}
resource aws_instance "server3" {
# ...
}
Now, I want to declare some monitoring rules. But since I’m kind of lazy, I don’t want to manually declare the rules for each servers. I have found the for_each
keyword, but I’m not sure how to use it.
resource "aws_cloudwatch_metric_alarm" "alarm_cpu" {
for_each = aws_instance # <= obviously, doesn’t work
alarm_name = "High CPU"
namespace = "AWS/EC2"
metric_name = "CPUUtilization"
dimensions = {
InstanceId = each.key.id
}
period = "60"
evaluation_periods = "3"
datapoints_to_alarm = "3"
statistic = "Average"
comparison_operator = "GreaterThanOrEqualToThreshold"
threshold = "95"
alarm_actions = [
aws_sns_topic.alarms.arn]
ok_actions = [
aws_sns_topic.alarms.arn]
}