【问题标题】:Terraform For Loop Return Object is EmptyTerraform For 循环返回对象为空
【发布时间】:2021-10-17 15:52:47
【问题描述】:

我正在尝试从对象数组中创建一个带有多个portMappingsaws_ecs_task_definition。当我使用for 语句时,它会打印出一个空的对象数组,而不是添加我希望它添加的值。知道我做错了什么吗?

附加上下文:

在我的实际设置中,aws_ecs_task_definition 在一个模块中,并且在调用模块时传入变量,这就是我首先使用for 循环的原因。

尽可能简单的例子:

variable "test" { default = [{ container_port = 80 }, { container_port = 3333 }] }

resource "aws_ecs_task_definition" "default" {
  family                = "foo"
  container_definitions = jsonencode([
    {
      name         = "foo"
      image        = "foo"
      portMappings = [
        for item in var.test :
          { container_port = item.container_port }
      ]
    }
  ])
}

预期(为简洁起见):

+ resource "aws_ecs_task_definition" "default" {
  + container_definitions    = jsonencode(
        [
          + {
              + portMappings     = [
                  + {
                  +   container_port = 80
                  + },
                  + {
                  +   container_port = 3333
                  + },
                ]
            },
        ]
    )
}

实际(为简洁起见):

+ resource "aws_ecs_task_definition" "default" {
  + container_definitions    = jsonencode(
        [
          + {
              + portMappings     = [
                  + {},
                  + {},
                ]
            },
        ]
    )
}

【问题讨论】:

    标签: amazon-web-services terraform terraform-provider-aws


    【解决方案1】:

    应该是:

              { containerPort = item.container_port }
    

    不是

              { container_port = item.container_port }
    

    【讨论】:

    • 工作就像一个魅力!非常感谢你!我花了很多时间在这上面。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-08-03
    • 2021-05-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-13
    • 2021-04-05
    相关资源
    最近更新 更多