Reputation: 109
terraform code:
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"LogicalDisk = % Free Space"
namespace = "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
objectname = var.objectname
}
}
variable "instance" {
type = list
default = ["E:" , "D:"]
}
Here is the output from the Terraform plan. As you can see it should create two CloudWatch alarms for instance E:
and D:
However when I go to the console after the apply is run successfully it only creates an alarm for instance E:
# aws_cloudwatch_metric_alarm.disk_percentage_low["D:"] will be created
+ resource "aws_cloudwatch_metric_alarm"
"disk_percentage_low" {
+ actions_enabled = true
+ alarm_actions = (known after apply)
+ alarm_description = "This metric monitors ec2 disk utilization"
+ alarm_name = "disk_percentage_low"
+ arn = (known after apply)
+ comparison_operator = "LessThanOrEqualToThreshold"
+ dimensions = {
+ "ImageId" = "ami-0aac9d7fa83beb6d2"
+ "InstanceId" = "i-021e580bccd130ac6"
+ "InstanceType" = "t2.micro"
+ "instance" = "D:"
+ "objectname" = "LogicalDisk"
}
+ evaluate_low_sample_count_percentiles = (known after apply)
+ evaluation_periods = 1
+ id = (known after apply)
+ metric_name = "LogicalDisk % Free Space"
+ namespace = "CWAgent"
+ period = 60
+ statistic = "Average"
+ threshold = 20
+ treat_missing_data = "missing"
}
# aws_cloudwatch_metric_alarm.disk_percentage_low["E:"] will be created
+ resource "aws_cloudwatch_metric_alarm"
"disk_percentage_low" {
+ actions_enabled = true
+ alarm_actions = (known after apply)
+ alarm_description = "This metric monitors ec2 disk utilization"
+ alarm_name = "disk_percentage_low"
+ arn = (known after apply)
+ comparison_operator = "LessThanOrEqualToThreshold"
+ dimensions = {
+ "ImageId" = "ami-0aac9d7fa83beb6d2"
+ "InstanceId" = "i-021e580bccd130ac6"
+ "InstanceType" = "t2.micro"
+ "instance" = "E:"
+ "objectname" = "LogicalDisk"
}
+ evaluate_low_sample_count_percentiles = (known after apply)
+ evaluation_periods = 1
+ id = (known after apply)
+ metric_name = "LogicalDisk % Free Space"
+ namespace = "CWAgent"
+ period = 60
+ statistic = "Average"
+ threshold = 20
+ treat_missing_data = "missing"
}
Upvotes: 1
Views: 1879
Reputation: 586
You need to set a count on the alarm and then use the index to configure the name (your instance resources are going to need a count as well as the alarm count will be set to the length of the instance count it will be monitoring):
resource "aws_cloudwatch_metric_alarm" "WorkerEC2Disk90" {
count = length(aws_instance.worker)
alarm_name = "worker-${count.index + 1}-${var.app_env}-EC2Disk90%"
comparison_operator = "GreaterThanOrEqualToThreshold"
evaluation_periods = "2"
metric_name = "disk_used_percent"
namespace = "CWAgent"
period = "300"
statistic = "Average"
threshold = "90"
alarm_description = "This metric monitors ec2 disk utilization"
datapoints_to_alarm = 2
alarm_actions = [var.org_account_cloudwatch_to_slack]
ok_actions = [var.org_account_cloudwatch_to_slack]
insufficient_data_actions = []
dimensions = {
InstanceId = aws_instance.worker[count.index].id,
ImageId = aws_instance.worker[count.index].ami,
InstanceType = aws_instance.worker[count.index].instance_type,
path = "/",
device = "nvme0n1p1",
fstype = "ext4"
}
}
resource "aws_instance" "worker" {
count = var.worker_instance_count
ami = var.ubuntu18_ami
...
}
Your dimensions
hash is going to need to use the count index as well, otherwise Cloudwatch isnt going to know when instances to explicitly monitor.
Upvotes: 2
Reputation: 239000
I think this is because you fixed the alarm_name
:
alarm_name = "disk_percentage_low"
So the second alarm overwrites the first one as both have same name. A quick fix could be:
alarm_name = "disk_percentage_low_${each.value}"
But not sure above if :
will be allowed in the name as you have "E:" and "D:". This is something you can check.
Upvotes: 1