A local builds subnet pairs with flatten([for tier, subnets in local.network : [for s in subnets : { tier = tier, subnet = s }]]), where local.network is {app = ["a", "b"], db = ["c"]}. What does the expression produce before it is converted for for_each?
Select an answer to reveal the explanation.
Short Explanation
Nested for expressions hand you boxes inside boxes. flatten() tips all the inner boxes out onto one table, so you get a single list of three tier-and-subnet pairs. The trap is thinking it reshapes the items themselves; it only removes the nesting.
Full Explanation
The outer for expression iterates the map and, for each tier, produces an inner list of objects, giving a list of lists: one list with two objects for app and one list with one object for db. flatten() replaces any nested list elements with their contents, recursively, yielding a single flat list of three objects, each pairing a tier with one subnet. This is the documented pattern for expanding nested structures before for_each, because for_each needs one element per instance. The flat list is then projected into a map with a unique key per element, for example a for expression over local.pairs whose key joins p.tier and p.subnet with a dot and whose value is p. flatten() does not stop at empty lists; it flattens every nested list. It never produces a map, since its output is always a list, and the outer brackets of the for expression already make that outer value a tuple. It does not merge objects either; each object remains a separate element and keeps its own attributes. Exam caveat: the keys built from the flat list must be unique and known at plan time, or for_each fails. Operational check: evaluate local.pairs in terraform console, confirm three elements, and then evaluate the projected map to verify its keys.