- What Terraform state is and why it matters.
- How Terraform uses state to track resources, detect differences, and produce accurate execution plans.
- Security considerations for storing and sharing state.
- How Terraform refreshes state, detects drift, and how refreshed state affects planning and execution.
- Map resources in your configuration to real-world objects.
- Compute diffs between the desired configuration and current reality.
- Generate a precise execution plan that performs only the necessary changes.
State is the authoritative record Terraform relies on to know which resources exist and how they relate. Losing or corrupting state can prevent Terraform from managing infrastructure correctly.
Quick summary table
How Terraform uses state
Terraform keeps a serialized snapshot of the infrastructure it manages. That snapshot contains:- Resource IDs and attributes needed by providers to reference cloud objects.
- Metadata for dependency graph resolution.
- Provider-specific information (for example, auto-generated names, ARNs, subresource IDs).
- Determine whether a resource already exists or must be created.
- Compute minimal updates instead of recreating resources unnecessarily.
- Maintain stable resource addresses across runs.
Security considerations
State files often contain sensitive information such as resource identifiers, network details, and sometimes secrets or tokens. Treat state with the same care you apply to other secrets:- Never commit state files to source control.
- Use remote backends (for example, S3 with DynamoDB locking, Azure Blob Storage, or Terraform Cloud/Enterprise) for team collaboration.
- Enable server-side encryption and backend-level access controls.
- Use state locking where supported to prevent concurrent modifications.
Terraform state can contain secrets and credentials. Never commit state files to source control. Use remote backends with encryption and access controls for team or production use.
Backends and collaboration (local vs remote)
Choosing the right backend affects collaboration, performance, and safety. Use remote backends for team or production workflows.
Recommended reading:
- Terraform State (Terraform docs)
- Backend-specific docs (AWS S3, Google Cloud Storage, Azure Blob, Terraform Cloud)
Refresh, drift detection, and effects on planning
Terraform can refresh state by querying provider APIs to update the stored attributes before planning. Refresh behavior is central to understanding why plans change:- Refresh updates state attributes to reflect the current live environment.
- If resources changed outside Terraform (drift), a refresh will reveal differences and update state accordingly.
- After refresh, Terraform re-evaluates diffs and may generate a different plan (for example, to reconcile drift or to make further intended changes).
- Run
terraform planfrequently in CI to detect drift early. - Use
terraform refreshorterraform plan -refresh=truewhen you suspect out-of-band changes. - Review plan outputs carefully after refresh—some differences may require manual reconciliation or import.
Best practices
- Use remote backends with locking and encryption for teams.
- Protect access to state storage with fine-grained IAM policies.
- Never store secrets in plain text within state; use secrets management integrations when possible.
- Regularly run plans in CI to detect drift.
- Use
terraform importto bring unmanaged resources under Terraform control rather than editing state manually. - Version your Terraform configurations and maintain a clear drift remediation policy.