我有一个服务器列表,如下所示,我需要为其创建 cloudwatch 警报。我似乎找不到很多这样的例子。
variable "vms" {
type = list
default = ["server1","server2","server3"]
}
我想将 for_each 用于我的 cloudwatch 警报:
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"
}
编辑:我相信我需要做这样的事情
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"
}