Hi,
I have a variable in terraform.auto.tfvars
that looks like this:
teams = [
{
name = "team1"
members = [
"member1",
"member2"
]
maintainers = [
"maintainer1",
"maintainer2"
]
}
]
I want to flatten it to looks like this:
d = [
{
"member_name" = "maintainer1"
"member_role" = "maintainer"
"team_name" = "team1"
},
{
"member_name" = "maintainer2"
"member_role" = "maintainer"
"team_name" = "team1"
},
{
"member_name" = "member1"
"member_role" = "member"
"team_name" = "team1"
},
{
"member_name" = "member2"
"member_role" = "member"
"team_name" = "team1"
},
]
I can achieve that as follows, but I am sure there must be an easier way to do this in one pass, rather than three.
locals {
a = flatten([for v1 in var.teams :
{
name = v1.name
members = v1.members
maintainers = v1.maintainers
}
])
b = flatten([for v1 in local.a : [
for v2 in v1.maintainers :
{
team_name = v1.name
member_name = v2
member_role = "maintainer"
}
]])
c = flatten([for v1 in local.a : [
for v2 in v1.members :
{
team_name = v1.name
member_name = v2
member_role = "member"
}
]])
d = concat(local.b, local.c)
}
output "d" {
value = local.d
}
can anyone advise on how I can make my locals
better?