IAC / A CONCEPT NOTE

Terraform State

the source of truth Terraform uses to reconcile real-world resources

~85 sec read

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

  1. Terraform

    reads the backend (local file, S3, or Consul) to fetch the current state.

  2. Terraform

    refreshes state by querying the provider for the real-world attributes of each resource.

  3. Terraform

    compares refreshed state against your .tf config to compute a diff.

  4. Terraform

    prints the plan: resources to create (green), destroy (red), or modify in-place (yellow).

  5. 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.

EXAMPLE 01 · REFERENCE

show every resource tracked in state

terraform state list

EXAMPLE 02 · REFERENCE

inspect a single resource's attributes

terraform state show RESOURCE_ADDR

Explore command anatomy in the CLI lab