count meta-argument
The count argument, which creates that many numbered copies of a resource or module, addressed as name[0], name[1] and so on.
In more detail
Use it when the copies are interchangeable, or with 0 or 1 to switch something off or on. Identity is positional: remove an item from the middle of a list and every later instance shifts, which Terraform sees as changes or replacements. Use for_each when each instance has its own name.
Example
resource "null_resource" "machine" {
count = var.instance_count
triggers = { name = "machine-${count.index}" }
}Related terms
- for_each — The for_each argument, which creates one instance of a resource or module per element of a map or set, addressed by key such as name["a"].
- resource instance — One of the objects a resource block produces; a block with count or for_each has several, each with its own index or key.
- meta-argument — An argument Terraform itself understands on any resource or module block, such as count, for_each, depends_on, provider and lifecycle.
Taught in
- Scaling Resources With Count — Infrastructure as Code with Terraform
- Iterating Over Maps With For Each — Infrastructure as Code with Terraform
- The Plan That Wanted To Destroy Production — HashiCorp Terraform Associate (004) Exam Prep
- One Hour, No Weights Published — HashiCorp Terraform Associate (004) Exam Prep
- State: Terraform's Memory Of What It Built — HashiCorp Terraform Associate (004) Exam Prep
- Reading A Plan, Saving It, Applying It — HashiCorp Terraform Associate (004) Exam Prep
- Resources, Data Sources And The References Between Them — HashiCorp Terraform Associate (004) Exam Prep
- Input Variables, Outputs, And Which Value Wins — HashiCorp Terraform Associate (004) Exam Prep
- Expressions And Functions: Making Configuration Compute — HashiCorp Terraform Associate (004) Exam Prep
- Count, For Each And Dynamic Blocks — HashiCorp Terraform Associate (004) Exam Prep