DevOpsInterviewPrep logo
← 🏗️ Declarative Infrastructure
Foundational

Terraform unknown values: keep instance keys known before apply

Explain why Terraform can plan an unknown attribute but cannot expand an unknown set of resource keys. Reproduce the failure without cloud resources and repair the collection's identity.

TL;DR: Terraform must determine resource instance addresses while planning. A map can have known keys and unknown values; a set of unknown generated IDs cannot supply those addresses. Choose stable keys from configuration and carry generated IDs as values.

Planning needs identity before every attribute is available

A cloud service often assigns an ID when creation succeeds. Terraform represents that pending value as unknown during planning. An expression that depends on it may also remain unknown. This is normal when the value fills an attribute of an already identifiable resource.

Expansion is different. Terraform needs to know whether it is planning binding["worker"], binding["api"] or another set of instances. If generated IDs become the keys, even the addresses are unavailable. The for_each reference requires known map keys or known set members. See unknown-value evaluation for how uncertainty propagates through expressions.

Expression shapeCan identify instances?Reason
Map with key worker and a generated-ID valueYesAddress uses the known key
Set containing a generated IDNo on first creationThe unknown value is also the key
Map filtered by an unknown booleanPotentially noTerraform cannot know which keys survive
Known numeric count with unknown attributesYesNumeric addresses can be expanded

Reproduce the distinction with local state

Use Terraform 1.5 or later in an empty temporary directory. This example uses the built-in terraform_data resource, with no downloaded provider, remote backend, provisioner or cloud account. It creates local state if applied. Save as main.tf:

terraform {
  required_version = ">= 1.5, < 2.0"
}

resource "terraform_data" "worker" {
  input = "worker"
}

resource "terraform_data" "binding" {
  for_each = { worker = terraform_data.worker.id }
  input    = each.value
}

Run terraform init -backend=false, then terraform plan. Terraform can plan two creations even though the first resource's generated ID is unknown. The second instance has address terraform_data.binding["worker"]; its input can wait for the producer's ID.

Before any apply, replace the for_each expression with toset([terraform_data.worker.id]). A fresh plan fails because the set member would determine the binding's address. Restore the map and plan again. The terraform_data reference explains the local resource used to make this experiment independent of a cloud API.

rendering diagram…

A later successful plan can hide a broken first run

If the producer already exists in state, its ID may be known and the set-based version may appear to work. That does not prove the module supports a new environment or replacement of that producer. Test the interface from empty state as well as from representative prior state.

Adding depends_on supplies ordering information, but does not generate the missing ID during planning. A broad module dependency can also postpone data reads and make additional values unknown. Routine targeted applies split the operation into separate decisions and can conceal the defective interface. Prefer a known-key design when both resources belong to the same lifecycle.

A genuine separation between independently owned deployments may justify separate states and an explicit output contract. That is an architectural boundary to review, including ownership and failure recovery. It should not be introduced merely to silence one expression error.

Preserve membership as well as key spelling

A map comprehension can use literal keys and still have unknown membership. For example, filtering its entries using a provider-computed readiness flag makes inclusion undecidable until apply. Move the decision to a plan-known input if it represents user intent, or redesign the resource boundary if runtime discovery truly determines the desired set.

Avoid replacing unknown values with null or an empty collection without understanding the result. Unknown means the value is pending. Null is a concrete absence, and an empty collection can propose deleting every previously managed instance. Such a “fallback” can convert an obvious planning failure into an unintended change.

Self-check: the value under key worker remains unknown, but the map always contains exactly that key. Is for_each blocked? No. The address is known. If the key's inclusion depends on that unknown value, the answer changes because membership is no longer established.

Apply the distinction in the unknown-key interview scenario. For existing deployments, review address refactoring before changing keys, since a new key can represent a new instance identity.

RELATED CONCEPTS
PRACTICE THIS IN REAL QUESTIONS