Terraform State
Terraform keeps track of the resources it creates using a state file. This file maps your configuration to real‑world resources and stores metadata. Understanding state is crucial for managing infrastructure correctly.
What Is Terraform State?
The state file (default
terraform.tfstate) is a JSON file that contains:- Resource IDs and attributes
- Dependency information
- Provider metadata
- Output values
terraform apply, Terraform updates this file. When you run terraform plan, it compares the configuration against the current state to determine what needs to change.The state file is Terraform’s source of truth for your managed infrastructure.
Why Is State Important?
- Resource tracking: Knows which resources belong to your configuration.
- Performance: Caches attribute values so Terraform doesn’t have to query APIs repeatedly.
- Dependency resolution: Stores relationships between resources.
Local State vs. Remote State
By default, Terraform stores state locally in
terraform.tfstate. For teams, you should use remote state (e.g., AWS S3, Azure Storage, Terraform Cloud). Remote state:- Allows team members to share state
- Provides locking to prevent concurrent modifications
- Keeps state safe (backed up, encrypted)
Sensitive Data in State
The state file often contains secrets (database passwords, API keys) in plain text. Even if you mark variables as
sensitive, the state may still store them. Always encrypt remote state at rest and restrict access.Never Edit State Manually
Do not edit
terraform.tfstate directly. Use state commands (terraform state mv, terraform state rm, etc.) if you need to modify state. Manual edits can break Terraform’s ability to manage resources.Viewing State
You can inspect the current state with:
terraform showOr list all resources in state:terraform state listTwo Minute Drill
- State file maps config to real resources; it is Terraform’s source of truth.
- Local state is fine for personal use; remote state is required for teams.
- State may contain sensitive data – encrypt remote state.
- Use
terraform showandterraform state listto inspect state. - Never edit
.tfstatemanually.
Need more clarification?
Drop us an email at career@quipoinfotech.com
