Count and numbered resource instances
Create several instances from one resource block
Imagine three numbered seats at a workshop. They use the same structure but need different labels. Terraform’s count is a meta-argument that controls the number of instances of a block; it is not an ordinary provider-specific argument. In this standalone local example, terraform_data stores synthetic values without creating servers or calling a cloud API. Add the version block from the lab file; the inherited pin is provisional and not verified here.
variable "replicas" { type = number default = 3 validation { condition = var.replicas >= 0 && var.replicas <= 5 && floor(var.replicas) == var.replicas error_message = "Choose a whole number from 0 to 5." }}
resource "terraform_data" "seat" { count = var.replicas input = "seat-${count.index + 1}"}
output "labels" { value = terraform_data.seat[*].output}With the default input, the expected addresses are terraform_data.seat[0], terraform_data.seat[1] and terraform_data.seat[2]. The displayed labels after apply should be seat-1, seat-2 and seat-3. The numbering in the labels is a choice: count.index itself starts at zero. The variable validation bounds the lab to five instances and rejects fractions.
Read a counted resource as a collection
A block describes the family; an indexed address identifies one member. The unindexed resource reference in an expression represents a sequence of instance objects, even when there is only one. Compare these expressions in the numbered fixture; they are not standalone configuration files.
terraform_data.seat[0].outputterraform_data.seat[*].outputlength(terraform_data.seat)After applying three seats, the first expression returns seat-1, the second collects three labels in index order, and the third returns 3. A generated resource ID is different from the configuration address. Do not expect IDs to be the same across runs. When count is zero the collection is empty, the length is zero, and indexing position 0 is invalid. The splat is safe for an empty collection.
Make an optional instance explicit
A boolean setting can select between zero and one instance. This separate complete configuration body shows a guarded single-value output and an always-collection output.
variable "enabled" { type = bool default = false}
resource "terraform_data" "notice" { count = var.enabled ? 1 : 0 input = "maintenance"}
output "notice" { value = var.enabled ? terraform_data.notice[0].output : null}
output "notices" { value = terraform_data.notice[*].output}With enabled=false, there are no notice instances; the scalar result is null (a null root output may be omitted from CLI output/state) and notices is an empty sequence. With enabled=true, address notice[0] exists and the scalar result after apply is maintenance. The collection contains one string. Turning the flag off after enabling it proposes removing the managed instance; in a real provider this may delete infrastructure. Always inspect the plan.
Know how many before creating instances
For ordinary Terraform planning, the cardinality must be a known nonnegative whole number. A variable supplied before planning is a suitable source. An ID generated only when a new resource is applied is not. The following is an intentionally invalid first-plan example in a fresh directory, not a lab to apply.
resource "terraform_data" "seed" { input = "demo"}
resource "terraform_data" "bad" { count = length(terraform_data.seed.id) input = count.index}The generated seed ID is unavailable in the initial plan, so its length cannot determine the count. Changing the resource input to a known variable or using a known collection length fixes the design; do not make a targeted apply the default workaround. Instance inputs themselves may contain values that become known later. The key distinction is whether Terraform can enumerate addresses.
Practice with the local plan
In an approved isolated lab, inspect the numbered fixture with default replicas=3, then compare a plan using replicas=0. Start from fresh state for the first prediction. After applying the three-instance plan only in the disposable fixture, review the zero-instance plan before cleanup. All results in this lesson are predictions, not a captured CLI transcript. See the lab README for the sequence and the separate worksheet for address predictions.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Count and numbered resource instances — knowledge check
Check numeric addresses, count and list change reasoning.
What addresses does count=3 produce for terraform_data.seat?
What is count.index in the third instance?
With count=0, what does the output splat collect?
Which value can determine count in an initial ordinary plan?
What happens when an enabled flag changes from true to false in a previously applied zero-or-one fixture?