Dear All,
I’m seeking help for a problem I cannot seem to resolve despite many sunk hours; all likely lack of understanding.
I’m trying to transform data from a module output into a map that for_each can consume. I’ve been through posts discussing the use of flatten()
but I can’t seem to make it work for my use-case. If someone can assist I’d be very grateful.
- Source module output data
> var.test_data_map
{
"vm_amis" = [
"ami-067436817bb0256c1",
"ami-067436817bb0256c1",
]
"vm_availability_zones" = [
"eu-central-1a",
"eu-central-1b",
]
"vm_hostnames" = [
"ecas01",
"ecas02",
]
"vm_ids" = [
"i-013d3a8efb84bfdbf",
"i-06e869a7c822b34f6",
]
"vm_instance_type" = [
"t2.medium",
"t2.medium",
]
"vm_private_dns" = [
"ip-10-10-10-37.eu-central-1.compute.internal",
"ip-10-10-10-88.eu-central-1.compute.internal",
]
"vm_private_ips" = [
"10.10.10.37",
"10.10.10.88",
]
"vm_subnet_ids" = [
"subnet-075464d83a0ce90f8",
"subnet-0102e530cae8471ed",
]
}
- From this
variable
variable "test_data_map" {
//type = map(list(string))
type = any
default = {
"vm_amis" = ["ami-067436817bb0256c1", "ami-067436817bb0256c1"]
"vm_availability_zones" = ["eu-central-1a", "eu-central-1b"]
"vm_hostnames" = ["ecas01", "ecas02"]
"vm_ids" = ["i-013d3a8efb84bfdbf", "i-06e869a7c822b34f6"]
"vm_instance_type" = ["t2.medium", "t2.medium"]
"vm_private_dns" = ["ip-10-10-10-37.eu-central-1.compute.internal", "ip-10-10-10-88.eu-central-1.compute.internal"]
"vm_private_ips" = ["10.10.10.37", "10.10.10.88"]
"vm_subnet_ids" = ["subnet-075464d83a0ce90f8", "subnet-0102e530cae8471ed"]
}
}
- If I run the following on the data it’s close to how I need the data, but not quite.
> { for k,v in var.test_data_map : k => v[0] }
{
"vm_amis" = "ami-067436817bb0256c1"
"vm_availability_zones" = "eu-central-1a"
"vm_hostnames" = "ecas01"
"vm_ids" = "i-013d3a8efb84bfdbf"
"vm_instance_type" = "t2.medium"
"vm_private_dns" = "ip-10-10-10-37.eu-central-1.compute.internal"
"vm_private_ips" = "10.10.10.37"
"vm_subnet_ids" = "subnet-075464d83a0ce90f8"
}
- To be consumed by
for_each
I think I need the data structure to be a map and ideally representative of this, but can’t work out viaflatten()
or lambda functions how to transform it properly. Using flatten I ended up with many maps not the 2x required.
i-013d3a8efb84bfdbf" = {
"vm_amis" = "ami-067436817bb0256c1"
"vm_availability_zones" = "eu-central-1a"
"vm_hostnames" = "ecas01"
"vm_ids" = "i-013d3a8efb84bfdbf"
"vm_instance_type" = "t2.medium"
"vm_private_dns" = "ip-10-10-10-37.eu-central-1.compute.internal"
"vm_private_ips" = "10.10.10.37"
"vm_subnet_ids" = "subnet-075464d83a0ce90f8"
}
"i-06e869a7c822b34f6" = {
"vm_amis" = "ami-067436817bb0256c1"
"vm_availability_zones" = "eu-central-1b"
"vm_hostnames" = "ecas02"
"vm_ids" = "i-06e869a7c822b34f6"
"vm_instance_type" = "t2.medium"
"vm_private_dns" = "ip-10-10-10-88.eu-central-1.compute.internal"
"vm_private_ips" = "10.10.10.88"
"vm_subnet_ids" = "subnet-0102e530cae8471ed"
}
Appreciate any help anyone can give.
Thanks!