amazon-web-services - Terraform foreach 对象列表
问题描述
我正在寻求帮助来遍历 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
解决方案
我认为应该是:
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
}
推荐阅读
- performance - 为什么我的 for 循环代码比迭代器慢?
- bazel - Bazel 用于测试资源 - 输入路径不存在:
- encryption - 将 256 字节块更改为数字以进行加密
- git - 即使在 git commit -am b/c origin 有一个带有反大写文件名的文件后,“更改未暂存以进行提交”
- javascript - 具有 2 个键的 ES6 Hashmap
- graphql - 功能有效,但控制台提供无关错误:“未提供所需类型“ID!”的变量“$id”。”
- reactjs - React context Api,使用 Consumer 外部提供者
- symfony4 - 在 FormType Symfony4 中获取 $this->getUser()
- python - 安装的 python 包给出 ModuleNotFoundError
- python - 如何通过文本文件过滤数据并删除剩余数据