Terraform

count and for_each

Definition

Meta-arguments that let you create multiple instances of the same resource or module from a single definition. count iterates over an integer (0..n-1); for_each iterates over a set or map, letting you reference each instance by a stable key instead of a numeric index.

Exam context

In Terraform Associate this is a frequent question: for_each is preferable when the order of deletion/creation matters, because index-based count can unnecessarily recreate resources if an item in the middle of the list is removed. for_each requires a set or map, never a plain unconverted list.

Studying for the Terraform Associate?

count and for_each is a concept that can show up on the exam. Check the full guide to see it in context.