Hello,
and yet again, this same " for_each on maps of complex objects" issue
The desire here is to supply ( tfvars) name(s) of security groups while building out a vm.
variables.tfvars:
vms = {
DS1 = {
...
instance_type = "r6i.large"
...
security_group_name = [
"DSsg",
]
....
}
DS2 = {
...
instance_type = "r6i.large"
...
security_group_name = [
"DSsg",
]
....
}
vars.tf:
variable "vms" {
type = map(object({
...
instance_type = string
....
security_group_name = list (string)
...
}))
}
and the code :
data aws_security_group sg-name {
for_each = var.vms
name = each.value.security_group_name
}
resource "aws_instance" "this" {
for_each = var.vms
vpc_security_group_ids = [ data.aws_security_group.sg-name[each.key].id ]
# vpc_security_group_ids = ["${join(",", data.aws_security_group.sg-name[each.key].id ) }" ]
# vpc_security_group_ids = [each.value.security_group_id]
...
}
needless to say that all lines (vpc_security_group_ids ) are failing with different errors.
in the current version, the error is :
Error: Incorrect attribute value type
β
β on ec2.tf line 24, in data "aws_security_group" "sg-name":
β 24: name = each.value.security_group_name
β βββββββββββββββββ
β β each.value.security_group_name is list of string with 1 element
β
β Inappropriate value for attribute "name": string required
if i change the code to:
vpc_security_group_ids = ["${join(",", data.aws_security_group.sg-name[each.key].id ) }" ]
the error is :
Error: Incorrect attribute value type
β
β on ec2.tf line 24, in data "aws_security_group" "sg-name":
β 24: name = each.value.security_group_name
β βββββββββββββββββ
β β each.value.security_group_name is list of string with 2 elements
β
β Inappropriate value for attribute "name": string required.
Clearly iβm not understanding how the loop on the complex objects with a βlook up into data resourceβ work in this scenario.
Appreciate your guidance.