Skip to content

Instantly share code, notes, and snippets.

@rquackenbush
Last active August 20, 2025 16:42
Show Gist options
  • Select an option

  • Save rquackenbush/a876b1448a385b3451e2507cbd39a4ca to your computer and use it in GitHub Desktop.

Select an option

Save rquackenbush/a876b1448a385b3451e2507cbd39a4ca to your computer and use it in GitHub Desktop.
Complex map reduction

Complex map reduction in terraform

This example takes a list of objects, and builds a dictionary of the attributes from that list of objects into a single, flat map(strig).

References

{
"one-atr-a" = "1.a"
"one-atr-b" = "1.b"
"two-atr-a" = "2.a"
"two-atr-b" = "2.b"
}
[
{
"one-atr-a" = "1.a"
},
{
"one-atr-b" = "1.b"
},
{
"two-atr-a" = "2.a"
},
{
"two-atr-b" = "2.b"
},
]
[
[
{
"one-atr-a" = "1.a"
},
{
"one-atr-b" = "1.b"
},
],
[
{
"two-atr-a" = "2.a"
},
{
"two-atr-b" = "2.b"
},
],
]
locals {
# list(object)
source = [
{
name = "one"
attribute_a = "1.a",
attribute_b = "1.b"
},
{
name = "two"
attribute_a = "2.a",
attribute_b = "2.b"
}]
# list(list(map(string)))
intermediate_1 = [for outer in local.source :
[for k, v in {
a = outer.attribute_a,
b = outer.attribute_b
} : {
"${outer.name}-atr-${k}" = v
}]
]
# list(map(string))
intermediate_2 = flatten(local.intermediate_1)
# map(strin)
final = merge(local.intermediate_2...)
}
output "source" {
value = local.source
}
output "_intermediate_1" {
value = local._intermediate_1
}
output "_intermediate_2" {
value = local._intermediate_2
}
output "final" {
value = local.final
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment