【问题标题】:Terraform foreach list of objectsTerraform foreach 对象列表
【发布时间】:2021-12-13 04:51:39
【问题描述】:

我正在寻求帮助来遍历 Terraform 中的对象列表。

我有以下几点:

local.distinct_rule_list =

      + {
          + customer_name = "test125231"
          + rightsubnet   = [
              + "10.41.0.0/16",
            ]
        },
      + {
          + customer_name = "test125231"
          + rightsubnet   = [
              + "10.71.0.0/16",
            ]
        },
      + {
          + customer_name = "real-test-4-2323"
          + rightsubnet   = [
              + "10.42.0.0/16",
            ]
        },
    ]

我想在下面使用它:

resource "aws_security_group_rule" "dh_ingress_sg_rule" {
  for             = local.distinct_rule_list
  type              = "ingress"
  from_port         = 8000
  to_port           = 8080
  protocol          = "tcp"
  cidr_blocks       = each.value["rightsubnet"]
  description       = each.value["customer_name"]
  security_group_id = aws_security_group.sgtest.id
}

我收到以下错误:

│ Error: Invalid for_each argument
│ 
│   on sg.tf line 42, in resource "aws_security_group_rule" "dh_ingress_sg_rule":
│   42:   for_each          = local.distinct_rule_list
│     ├────────────────
│     │ local.distinct_rule_list is list of object with 3 elements
│ 
│ The given "for_each" argument value is unsuitable: the "for_each" argument
│ must be a map, or set of strings, and you have provided a value of type
│ list of object.
╵
Releasing state lock. This may take a few moments...
ERRO[0017] 1 error occurred:
        * exit status 1
          

【问题讨论】:

  • 好的,但是有什么问题?有什么错误吗?什么local.distinct_rule_list?它的定义和值都没有显示。
  • @Marcin 感谢您的推荐。我更新了所要求的详细信息。

标签: amazon-web-services list foreach terraform aws-security-group


【解决方案1】:

我认为应该是:

resource "aws_security_group_rule" "dh_ingress_sg_rule" {
  for_each          = {for idx, val in local.distinct_rule_list: idx=>val}
  type              = "ingress"
  from_port         = 8000
  to_port           = 8080
  protocol          = "tcp"
  cidr_blocks       = each.value["rightsubnet"]
  description       = each.value["customer_name"]
  security_group_id = aws_security_group.sgtest.id
}

【讨论】:

    猜你喜欢
    • 2020-07-31
    • 1970-01-01
    • 2020-03-01
    • 2020-09-27
    • 2022-01-04
    • 2020-10-22
    • 2020-12-27
    • 2022-01-19
    • 2021-09-20
    相关资源
    最近更新 更多