IAC / A CONCEPT NOTE
Terraform State
the source of truth Terraform uses to reconcile real-world resources
Overview · mechanism
pitfall · examples
01 / THE SHORT VERSION
The idea in a few sentences.
Every terraform apply records what it created in a state file (usually terraform.tfstate). On subsequent runs, Terraform compares the state against both your config and the real cloud provider — creating, updating, or deleting resources to match. The state is the bridge between your .tf files and what's actually deployed.
02 / FOLLOW THE MECHANISM
How state drives a plan
Terraform
reads the backend (local file, S3, or Consul) to fetch the current state.
Terraform
refreshes state by querying the provider for the real-world attributes of each resource.
Terraform
compares refreshed state against your
.tfconfig to compute a diff.Terraform
prints the plan: resources to create (green), destroy (red), or modify in-place (yellow).
Apply
executes the plan, updating state after each API call. If it crashes mid-way, you get a partial state.
04 / COMMAND NOTES
Read the command, then the result.
Inspect the flags and arguments before trying an example. Snippets can need local setup, replacement values, or resources in your own environment.
show every resource tracked in state
terraform state listinspect a single resource's attributes
terraform state show RESOURCE_ADDR05 / CHECK YOURSELF
Could you explain Terraform State to a teammate?
Try it out loud in two sentences: what it is, and the one detail that changes the picture. If you stall, the gap is the part to reread.
Up next in Infrastructure as codeTerraform Modulesreusable, versioned infrastructure components