Known keys and instance changes
Separate known identity from unknown settings
Before planning ordinary managed resources, Terraform needs the set of addresses. In the previous chained fixture the names are literal even though the generated IDs are unknown. Contrast this intentionally invalid fresh-plan configuration, provided only as text for diagnosis.
resource "terraform_data" "seed" { input = "sample"}
resource "terraform_data" "bad" { for_each = toset([terraform_data.seed.id]) input = each.key}The seed ID is computed when the resource is created. Using it as a set member makes the proposed bad instance address unknown on a fresh plan. Use a known name as the map key and put the generated ID in the resource input instead. Similarly, a map can contain unknown non-key values when its keys are known and the collection is otherwise valid. Do not use a targeted apply as the usual solution to a badly chosen identity.
Protect stable public keys
Addresses may appear in plans, state and logs. A sensitive-marked collection is unsuitable for for_each; keep public identifiers separate from sensitive settings instead of stripping secrecy markings indiscriminately. Ephemeral-derived identity is also unsuitable for managed resources: those keys must survive between operations, whereas ephemeral values are not persisted. Avoid deferred functions such as uuid() and timestamp() as identity sources. A null collection is invalid; express an intentionally empty desired family with an empty map or empty string set. Null map values have separate semantics and are not automatically an instruction to omit an instance. The previous lab rejects them as an explicit input policy.
Predict changes by comparing keys and settings
Assume the development/test example is already applied and no drift or other changes exist. Compare each desired key with the existing address, then inspect its values. Here is a conceptual comparison, not captured Terraform output.
Desired change Expected managed-resource actionsReverse map declaration No address or input changesAdd qa = small Create environment["qa"]Change dev to large Update environment["dev"] inputRemove test Remove environment["test"]Rename dev to sandbox Remove dev and create sandbox (without a move)A stable key avoids churn from reordering, not all changes. A real provider can require replacement for a changed argument. Renaming a key changes the address even when the input label is unchanged. Before editing, decide if you are adding a new logical member or renaming an existing one. Review a proposed removal instead of assuming it is harmless.
Migrate an existing numbered family deliberately
A small disposable fixture starts with the following numbered configuration. Its state must actually contain the two old instances before the migration experiment makes sense.
resource "terraform_data" "card" { count = 2 input = ["blue", "green"][count.index]}Once the reviewed initial plan has been applied, verify the input values at [0] and [1]. Replace the old configuration (not append to it) with this keyed version containing explicit moves.
resource "terraform_data" "card" { for_each = { blue = "blue", green = "green" } input = each.value}
moved { from = terraform_data.card[0] to = terraform_data.card["blue"]}
moved { from = terraform_data.card[1] to = terraform_data.card["green"]}For this exact baseline, the intended plan maps [0] to ["blue"] and [1] to ["green"] without creating or removing managed objects, assuming no other changes. The move declaration describes a change of state address, not a general guarantee of no updates or replacements. The actual plan is authoritative. Never guess which old index belongs to which name: compare the previous configuration, state and intended settings. A key-to-key rename can use the same mechanism with two string-indexed addresses. Retain appropriate migration history for users who may upgrade from older configurations.
Practice reviewing changes rather than applying guesses
Use the synthetic mapping cases and migration instructions in the lab README. Predict the address changes for insertion, deletion, rename and an empty collection. Review the migration plan from the local baseline only. Stop if it proposes unexpected create, update or deletion operations. Exact diagnostics and runtime output are not verified; no real cloud resources or external provider plugins are needed for these fixtures.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Known keys and instance changes — knowledge check
Check keyed identity, collection semantics and safe migration reasoning.
Which collection is invalid as the managed for_each argument in a fresh plan?
After reversing the order of the environment map entries with the same keys and settings, what is expected?
Without a moved block, renaming dev to sandbox with the same input normally proposes what?
What does a moved block describe?
Which identifier is suitable as a managed instance key?