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).
- This uses Terraform Argument Expansion (...) which I tend to forget exists.
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).
| { | |
| "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 | |
| } |