Terraform count vs for_each Is Not About Loops

Most developers think count and for_each are just two ways to repeat resources.

They are not.

Terraform does not track “resources.”
It tracks addresses in state.

With count, the address is positional.
aws_vpc.example[0] is tied to an index.
Remove the first item in a list and every index shifts.
Shifted index means new address.
New address means destroy and recreate.

With for_each, the address is keyed.
aws_vpc.example["prod"] is tied to a label.
Reordering does not matter.
Stable keys preserve identity.

The real design question is this:

Are your instances interchangeable slots,
or named roles that must persist?

Choose the wrong identity model and a refactor becomes an outage.

https://youtu.be/epuuecPe8_I