繁体   English   中英

循环包含地图的列表并有条件地创建资源

[英]Loop over list containing maps and create resources conditionaly

我有以下 json 结构:

{
  "serviceConfig": {
    "city_shared": {
      "schedules": [
        {
          "EcsServiceRestartOrder": [
            "redis",
            "redis2",
            "redisblahblah"
          ],
          "Description": "daily",
          "CityRestartCronExpression": "cron(0 4 * * ? *)",
          "CityRestartEnabled": true
        },
        {
          "EcsServiceRestartOrder": ["redis-stg"],
          "Description": "weekly",
          "CityRestartCronExpression" : "cron(0 4 * * 1 *)",
          "CityRestartEnabled": true
        }
      ],
      "MonitoringStartUTC": "0500",
      "MonitoringEndUTC": "2000"
    }
  }
}

我有以下 terraform:

resource "aws_cloudwatch_event_rule" "scheduled_restart" {
  for_each = { for i in var.serviceConfig.city_shared.schedules : i.Description => i
  if i.CityRestartEnabled == true }

  name                = "${local.awsResourceName}-${each.value.Description}-scheduled_restart"
  schedule_expression = each.value.CityRestartCronExpression

  is_enabled = each.value.CityRestartEnabled
}

resource "aws_iam_role" "cloudwatch_event_scheduled_restart" {
  for_each = { for i in var.serviceConfig.city_shared.schedules : i.Description => i
  if var.serviceConfig.city_shared.schedules.CityRestartEnabled == true }

  name = "${join("", [local.multiRegionCityPrefix, local.awsResourceName])}-cloudwatch_event_scheduled_restart"

  assume_role_policy = jsonencode({
    "Version" : "2012-10-17",
    "Statement" : [
      {
        "Effect" : "Allow",
        "Principal" : {
          "Service" : "events.amazonaws.com"
        },
        "Action" : "sts:AssumeRole"
      }
    ]
    }
  )

  tags = {
    tag-key = "${join("", [local.multiRegionCityPrefix, local.awsResourceName])}-cloudwatch_event_scheduled_restart"
  }
}

我试图遍历结构并仅在CityRestartEnabled设置为true时才创建资源,但是我收到一些错误:

Error: Unsupported attribute

  on cloudwatch_events.tf line 3, in resource "aws_iam_role" "cloudwatch_event_scheduled_restart":
   3:   if var.serviceConfig.city_shared.schedules.CityRestartEnabled == true }
    |----------------
    | var.serviceConfig.city_shared.schedules is list of object with 2 elements

This value does not have any attributes.

和以下错误:

Error: Unsupported attribute

  on cloudwatch_events.tf line 52, in resource "aws_cloudwatch_event_rule" "scheduled_restart":
  52:   for_each = { for i in var.serviceConfig.city_shared.schedules : i.Description => i

This object does not have an attribute named "Description".

我不太清楚我哪里出了问题,如果有人有一些建议,我将不胜感激。

问题是我自己的配置。 serviceConfig变量在 terraform 代码中定义为 object,然后我们通过.auto.tfvars.json文件加载配置。 这意味着serviceConfig变量必须与 json 完全匹配。 我只是忘记更新变量以包含Description.

variable "serviceConfig" {
  type = object({
    city_shared = object({
      schedules = list(object({
        EcsServiceRestartOrder    = list(string)
        CityRestartCronExpression = string
        CityRestartEnabled        = bool
        Description               = string
      }))
      MonitoringStartUTC = string
      MonitoringEndUTC   = string
    })
  })
}

感谢@MarkoE 进行测试,你说它正在工作这一事实让我返回 go 并仔细检查所有内容。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM