【发布时间】:2022-01-04 19:08:48
【问题描述】:
我想遍历一个包含两个警报对象的列表,配置如下 - 问题是如何通过循环每个警报指标列表来动态生成 metric_query 块..
locals{
prefix = terraform.workspace
alarm_WithExpression = [
{ "name" : "XXX",
"expression" : "m1/m2",
"metrics" : [
{ "id" : "m1", "name" : "YYYY", "statistic" : "Sum", "period" : 21600 },
{ "id" : "m2", "name" : "ZZZZZ" ,"statistic" : "Sum", "period" : 21600 }
],
"threshold" : 0.2,
"comparison_operator" : "LessThanThreshold",
"period" : 21600,
"data_points" : 2,
"treat_missing_data" : "notBreaching"
},
{ "name" : "AAAA",
"expression" : "m1/(m2-m3)",
"metrics" : [
{ "id" : "m1", "name" : "BBBB", "statistic" : "Sum", "period" : 21600 },
{ "id" : "m2", "name" : "CCCC", "statistic" : "Sum", "period" : 21600 },
{ "id" : "m3", "name" : "DDDD", "statistic" : "Sum", "period" : 21600 }
],
"threshold" : "0.2",
"comparison_operator" : "LessThanThreshold",
"period" : 21600,
"data_points" : 2,
"treat_missing_data" : "notBreaching"
}
]
}
这是指标警报声明:
resource "aws_cloudwatch_metric_alarm" "metrics-withExpression_alarm" {
for_each = local.prefix == "production" ? {} : {for index,m in local.alarm_WithExpression : index => m}
alarm_name = "${local.prefix}-${each.value.name}"
comparison_operator = each.value.comparison_operator
evaluation_periods = each.value.period
threshold = each.value.threshold
treat_missing_data = each.value.treat_missing_data
datapoints_to_alarm = each.value.data_points
metric_query {
id = "e"
expression = each.value.expression
return_data = "true"
}
# loop inside list of metrics ??
dynamic "metric_query" {
for_each = {for index,k in local.alarm_WithExpression.metrics : index => k}
content {
id = each.value.id
metric {
metric_name = each.value.name
namespace = "${local.prefix}-metrics"
period = each.value.period
stat = each.value.statistic
}
}
}
}
我收到此计划错误:
╷
│ Error: Unsupported attribute
│
│ on cloudwatch-alarms.tf line 177, in resource "aws_cloudwatch_metric_alarm" "metrics-withExpression_alarm":
│ 177: for_each = {for index,k in local.alarm_WithExpression.metrics : index => k}
│ ├────────────────
│ │ local.alarm_WithExpression is tuple with 2 elements
│
│ This value does not have any attributes.
╵
ERROR: 1
如何遍历每个警报指标列表?
【问题讨论】:
标签: terraform terraform-provider-aws