I am looking for help to foreach over a list of objects in Terraform.
I have the following:
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",
]
},
]
I would like to use it in the following:
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
}
I receive the following error:
│ 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
I think it should be:
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
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.