terraform aws_cloudwatch_metric_alarm动态尺寸键错误

时间:2019-04-30 14:01:41

标签: terraform

想知道是否有可能在地形aws_cloudwatch_metric_alarm维度名称中使用动态求值列表元素。

使用类似这样的本地列表来创建x数量的cloudwatch警报。

locals {
 alarm_list = ['service1-status', 'service2-status2', service3-status3']
}

在我的aws_cloudwatch_metric_alarm的资源块中

dimensions = {
     "${element(split("-", local.alarms_list[count.index]), 0)}" = 
     "${element(split("-", local.alarms_list[count.index]), 1)}"

}

这是tf计划获得的输出。

  + aws_cloudwatch_metric_alarm.alarm[0]
      id:                                                                   <computed>
      actions_enabled:                                                      "true"
      alarm_description:                                                    "response codes from nginx status"
      alarm_name:                                                           "nginx-status-alarm"
      arn:                                                                  <computed>
      comparison_operator:                                                  "GreaterThanThreshold"
      dimensions.${element(split("-", local.alarms_list[count.index]), 0)}: ""

1 个答案:

答案 0 :(得分:1)

例如在Terraform 0.12中,使用地图for-expressions,这很简单

  dimensions = {
    for value in local.alarm_list :
      element(split("-", value), 0) => element(split("-", value), 1)
  }