State bindings and resource addresses
Why Terraform needs state
Configuration describes what you want; state records which managed instances Terraform already knows about. Without a recorded binding, an existing object is not automatically recognized as the object described by your resource block. State also stores observed attributes, provider associations and dependency metadata. It is not simply a cache: deleting it can lose ownership information even though infrastructure still exists. In this chapter every ID is fictional or generated by a local built-in resource.
Read an address-to-object mapping
A resource address identifies an instance in the configuration, whereas the provider determines the identity of the associated object. Here is an invented mapping, not Terraform output:
terraform_data.catalog -> demo-id-Aterraform_data.worker["blue"] -> demo-id-Bterraform_data.worker["green"] -> demo-id-CThe two workers share a resource block but have independent bindings. A child module adds a prefix such as module.catalog.terraform_data.worker["blue"]. Keep an object managed by one instance, rather than importing it at two addresses or into two competing states. A resource name is not necessarily a remote name or ID.
Create a local-only example
The following complete resource configuration uses the built-in provider, not a cloud service. Copy the bindings fixture to a disposable directory and follow its README. The expected state contains three instances after a successful approved local apply. Actual generated IDs are not fixed.
resource "terraform_data" "catalog" { input = "catalog-v1"}
resource "terraform_data" "worker" { for_each = toset(["blue", "green"]) input = each.key}
output "catalog_label" { value = terraform_data.catalog.output}Changing the input of terraform_data.catalog normally updates the existing instance; removing a worker key normally proposes deletion of that instance. Merely renaming an address can look like deletion plus creation unless an explicit refactoring mapping preserves identity. Review the plan before applying; do not make these changes in real infrastructure.
Separate desired, recorded and observed information
Imagine the configuration requests a green worker, state associates green with demo-id-C, and a provider later reports that the associated remote object is missing. This is drift. During a normal plan, Terraform can refresh observations and propose changes to reach the configuration. It does not continuously watch infrastructure, and a prior state snapshot is not a guarantee of current health. The local fixture has no external remote object; the drift scenario is conceptual. Dependencies recorded in state can help determine removal order even after corresponding configuration is removed.
Locate the correct state snapshot
With the default local backend and default workspace, the usual state path is terraform.tfstate; a previous snapshot may be saved as terraform.tfstate.backup. Other local CLI workspaces normally use separate subdirectories of terraform.tfstate.d, and backend settings can change paths. HCP Terraform workspaces have a separate service model. Lineage identifies a state history; serial distinguishes successive revisions within that history. A newer serial from another lineage does not prove it belongs to your environment. The raw JSON format can evolve, so use documented CLI interfaces. A state backup is not a backup of database contents or other application data.
Inspect before changing anything
After the local fixture is successfully applied, run these POSIX-shell commands from its directory:
terraform state listterraform state show 'terraform_data.worker["blue"]'terraform output catalog_labelExpect three addresses, attributes for just the blue instance, and the label catalog-v1. IDs and formatting vary. Do not edit the raw state file to repair a mapping. State migration and recovery are separate topics; stop and confirm ownership, backend and workspace if the binding is unexpected. Complete the mapping worksheet before continuing.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
State bindings and resource addresses — knowledge check
Review state ownership and safe inspection.
What does a state binding connect?
What happens if the same remote object is managed at two addresses?
Why may state retain dependency metadata?
Which describes a state serial?
What does a state backup not necessarily contain?