对每个对象列表进行地形变换



我正在寻找帮助在Terraform的对象列表。

我有以下内容:

本地。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
}

最新更新