[英]Terraform foreach list of objects
我正在寻找帮助以对 Terraform 中的对象列表进行 foreach。
我有以下几点:
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
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.