Count and list reordering
Understand what the index identifies
Suppose three labeled cards are managed in one resource block. This local configuration assigns a name using its position in a list. It deliberately illustrates a design that is fragile when the list is edited. Add the version block in the lab fixture.
variable "names" { type = list(string) default = ["blue", "green", "amber"] validation { condition = length(var.names) <= 5 error_message = "Use at most five names in this lab." }}
resource "terraform_data" "card" { count = length(var.names) input = var.names[count.index]}
output "cards" { value = terraform_data.card[*].output}After applying the default values, card[0] contains blue, card[1] contains green and card[2] contains amber. Terraform uses those numeric addresses to associate the configuration with state. It does not infer a permanent identity from the input string.
Predict a removal in the middle
Assume the original three-card configuration has been applied, with no external changes. Now remove green from the list. This table is a conceptual comparison, not actual plan output.
Address Before Aftercard[0] blue bluecard[1] green ambercard[2] amber absentThe desired number falls to two. The surviving address card[1] now has a different input; card[2] is no longer desired. For this plain terraform_data fixture, predict an input update at [1] and removal at [2]. Removing the last item instead leaves earlier positions unchanged. In a real provider, a changed argument can require either an update or a replacement depending on its schema and lifecycle rules. A list removal is not a request to find and delete a resource by its input label.
Reordering can change configuration without changing count
Compare the original list with ["amber", "green", "blue"]. The cardinality remains three and the addresses are the same, but [0] and [2] have changed values. No additional resource address is needed. With ordinary input changes in the local fixture, expect two updates. If replacement is deliberately configured, the same address can receive a new object. In a separate fresh fixture, replace the resource block with this version; never put both blocks in one module.
resource "terraform_data" "card" { count = length(var.names) input = var.names[count.index] triggers_replace = var.names[count.index]}With triggers_replace tied to the name, the reorder is expected to replace instances [0] and [2], not [1]. Generated IDs are not fixed; verify changes using address and action categories, not copied IDs. Do not add a replacement trigger merely to correct poor identity design.
Select count or stable keys
Use count where numeric identity is appropriate, for example a pool of similarly configured replicas, or a zero-or-one optional resource. When distinct members have independent meaning, a keyed design is usually clearer. A conceptual preview: stable keys blue, green and amber identify instances independently of list order. Removing green from a keyed map would remove the green address while leaving the other addresses present, assuming their values and other dependencies are unchanged. The next chapter teaches for_each. Changing an already-managed block from numeric addresses to keyed addresses requires a reviewed state migration; simply changing the meta-argument can propose destructive changes.
Review the plan before accepting the change
For the lab, read each address and expected action in the offline mapping cases before comparing with a real plan. A plan with the right instance count may still contain unexpected replacement or deletion. Do not apply a surprising plan. Compare desired identity with prior state, choose a suitable address strategy, and review migrations with the relevant owners. No exact Terraform diagnostics or plan output were executed in this batch.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Count and list reordering — knowledge check
Check numeric addresses, count and list change reasoning.
Which property identifies a count instance in state?
After removing green from [blue, green, amber], which old address is no longer desired?
In the plain fixture, what happens to card[1] after that removal?
What can triggers_replace do when the value changes?
Which design is generally clearer for independently named members?