I have a map variable
variable "tiers" {
default = {
DEMO_APP = {
env = "demo"
sg = "tier-f5b5d2b4"
}
DEMO_DB = {
env = "demo"
sg = "tier-5425ce73"
}
DEV_APP = {
env = "dev"
sg = "tier-8e96a095"
}
DEV_DB = {
env = "dev"
sg = "tier-3ac50a95"
}
QA_APP = {
env = "qa"
sg = "tier-a176357f"
}
QA_DB = {
env = "qa"
sg = "tier-0d05fa93"
}
}
description = "Map of security group IDs"
type = map(object({
env = string
sg = string
}))
}
I need to add a field to it that maps to a list created by a data object
data "loadbalancer" "lbs" {
subproject = subproject.id
}
The end result should have each map element having an additional “lb” attribute:
DEMO_APP = {
env = "demo"
sg = "tier-f5b5d2b4"
lb = "lb-8956l096"
}
The new element is mapped based on the env value, so each env = “demo” would have the same lb value.
I’ve gone through a few attempts at achieving this, but from digging around what I’ve come up with is to flatten out the structure with something like this:
locals {
App_LBs = flatten([
for keys, values in var.tiers : {
name = keys
env = values.env
sg = values.sg
lb = join("",
[
for each in data.loadbalancer.lbs.results : each.id if each.name == "${var.lb_prefix}-${values.env}"
])
}
])
}
My hope was that this solution would create something like:
{
name = "DEMO_APP"
env = "demo"
sg = "tier-f5b5d2b4"
lb = "lb-8956l096"
}
{
name = DEMO_DB
env = "demo"
sg = "tier-5425ce73"
lb = "lb-8956l096"
}
Unfortunately, this is failing when running with for_each due to being a tuple value. Is there a better way to simply add this element to the array, or potentially turn this into something for_each will accept?