
- Resource management: Only resources recorded in the state file are controlled by Terraform. This prevents accidental changes to unmanaged infrastructure and ensures updates target only intended resources.
- Dependency management: State stores the relationships and ordering Terraform relies on to create, update, and destroy resources in the correct sequence (implicit references or explicit
depends_on). - Team collaboration: When using a remote backend with locking (e.g., S3 + DynamoDB, Terraform Cloud), a shared state becomes a single source of truth that prevents conflicting concurrent changes.

Representative JSON fragment from a Terraform state file:
aws_instance block and the actual EC2 instance (via AMI, ARN, availability zone, etc.). Terraform uses these attributes to plan changes, detect drift, and determine operation ordering.
State commands and safe practices
You rarely need to open or edit the state file manually. Terraform provides subcommands to inspect and manipulate state safely:
Avoid editing the state file by hand unless you have a specific, well-tested reason. Prefer Terraform state subcommands or remote backend features to manage shared state safely.
- Left: Terraform configuration (HCL files you author)
- Middle: Terraform state (mapping and metadata)
- Right: Real-world infrastructure (cloud provider resources)
terraform plan / terraform apply:
- Terraform reads your configuration files.
- It reads (and by default refreshes) the state to reconcile with live resources.
- Terraform queries providers to compare live resource attributes with state and configuration.
- It calculates an execution plan to create, update, or destroy resources in the correct order based on dependencies.
- When applied, Terraform performs the operations and updates the state to reflect the new reality.
- You add a
kubernetes_clusterblock to your configuration. terraform plansees the desired resource, reads state (no cluster entry yet), and queries the provider.- Terraform creates the cluster during
applyand writes an entry in state mapping your HCL block to the new cluster ID and attributes.
- Use a remote backend (Terraform Cloud, S3 + DynamoDB, Azure Blob Storage, GCS) for team collaboration and automatic locking.
- Enable state locking where supported to avoid concurrent mutations.
- Encrypt state at rest and control access—state can contain sensitive values.
- Use workspaces or separate state files when managing multiple environments (dev/staging/prod).
- Avoid frequent manual state edits; prefer
terraform statesubcommands for refactorings.