[英]terraform resource should create multiple CloudWatch alarms based on variable, which is confirmed in the plan but only deploys one alarm
terraform 代碼:
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:"]
}
這是 Terraform 計划中的 output。 如您所見,它應該為實例E:
和D:
創建兩個 CloudWatch 警報,但是當我在應用成功運行后將 go 發送到控制台時,它只會為實例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"
}
您需要在警報上設置一個計數,然后使用索引來配置名稱(您的實例資源將需要一個計數,並且警報計數將設置為它將監視的實例計數的長度):
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
...
}
您的dimensions
hash 也將需要使用計數索引,否則 Cloudwatch 將不知道何時顯式監控實例。
我認為這是因為您修復了alarm_name
:
alarm_name = "disk_percentage_low"
因此第二個警報會覆蓋第一個警報,因為它們具有相同的名稱。 快速修復可能是:
alarm_name = "disk_percentage_low_${each.value}"
但不確定上面是否允許使用名稱,因為您有“E :
”和“D:”。 這是你可以檢查的東西。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.