The Purpose of Terraform State
Terraform records the infrastructure it manages in state. This lesson explains why Terraform cannot work without state and how it uses state in every run.
Learning outcomes
After completing this lesson, you will be able to:
- Explain why Terraform needs state.
- Describe how state supports mapping, metadata, performance and collaboration.
- Explain how Terraform refreshes state before planning.
Mapping configuration to the real world
A resource block such as resource "aws_instance" "web" describes an object, but the configuration does not say which real instance it is. When Terraform creates the instance, the platform returns an identifier, such as i-0abc123. Terraform records the binding between the address aws_instance.web and that identifier, together with every attribute the provider returned, in state.
On the next run, Terraform reads the state, finds that aws_instance.web already has an object and asks the provider for its current settings. It then compares those settings with the configuration and plans only the differences. Without state, Terraform would have no reliable way to know which of the thousands of objects in an account it manages: not every platform supports tags, and tags can be copied or removed. Each real object should be bound to exactly one resource address in one state.
The following diagram compares the same configuration with and without its state.

Metadata, performance and collaboration
State also stores metadata. The most important is dependency information: if you delete both a subnet block and the instance in it from the configuration, Terraform no longer has the references that would tell it the order, but state remembers that the instance depended on the subnet, so it destroys the instance first. State records which provider configuration, including aliases, manages each resource.
State acts as a cache. By default every plan refreshes each object, which for large configurations means many API calls. terraform plan -refresh=false plans from the cached values, which is faster but can miss drift. Finally, state enables collaboration: when it is stored in a shared remote backend with locking, everyone works from the same record.
- Mapping: binds resource addresses to real object IDs.
- Metadata: keeps dependencies and provider information, including for resources removed from configuration.
- Performance: caches attributes, so refresh can be skipped when appropriate.
- Collaboration: a shared, locked state lets a team work on the same infrastructure.
Why state exists
Refreshing state
Before computing a plan, Terraform refreshes state: it asks each provider to read the current settings of each managed object and updates the in-memory copy of state. If someone changed an object outside Terraform, the refreshed state shows it, and the plan proposes to change it back to the configuration. If an object was deleted outside Terraform, the refreshed state no longer contains it, and the plan proposes to create it again.
A normal plan does not write the refreshed values to the state file; an apply does, as part of applying the plan. To update state with external changes without changing infrastructure, use terraform apply -refresh-only, covered in the module on drift.
What refresh finds
What state prevents
A developer deletes the terraform.tfstate file from a working directory that manages three running servers, then runs terraform apply. What happens?
The decisive fact is that state is the only link between the configuration and the servers.
With no state, Terraform believes nothing exists, so it plans to create three new servers. If the developer approves, there are six servers, three of which Terraform does not know about and will never update or destroy. The fix is to restore the state from a backup, such as terraform.tfstate.backup or a versioned remote backend, or to import the original servers. This is why state belongs in a durable, versioned, shared backend.
Why does Terraform keep dependency information in state?
Show answer
So that it can destroy resources in the right order even after their blocks, and the references between them, have been removed from the configuration.
Exam Tips
Remember for the exam
- State binds each resource address to one real object.
- State keeps dependency metadata for resources removed from configuration.
- Plans refresh state first; -refresh=false uses cached values.
- Losing state makes Terraform try to create everything again.