Terraform
count and for_each
Appears inTerraform Associate
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.
On the exam
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.
Related terms
- Terraform ResourceA configuration block that describes an infrastructure object (VM, bucket, database) that Terraform creates and manages. Syntax: resource "type" "local_name" { arguments }.
- Terraform VariableAn input parameter that lets you parameterize Terraform configurations. Declared with variable {} and assigned via a .tfvars file, environment variables (TF_VAR_), -var flags, or interactively.