Most Terraform errors around for_each are not resource problems. They are data shape problems.
Nested for expressions often produce a list of lists. Terraform cannot iterate that structure directly. The flatten function removes that extra layer and converts it into a single sequence.
That is the mechanism.
The mistake comes next.
Many engineers flatten too early and lose the parent context. Once that happens, resource names, tags, and logic lose meaning. The configuration still runs, but the infrastructure becomes ambiguous.
The correct pattern is strict:
Attach context → flatten → project into a keyed map.
That sequence preserves identity and produces stable resource addresses.
This lesson breaks down how flatten actually works and where it fails in real deployments.
Watch the full breakdown here.

