Skip to main content
In this lesson we’ll examine Terraform state: what it contains, how to inspect it, and how to refresh it when infrastructure changes outside of Terraform. We previously wrote configuration and applied it — Terraform’s true power comes from how it tracks resources in state. Here we’ll learn how to view that state, perform safe state maintenance, and reconcile Terraform’s view with the actual provider.

What is Terraform state?

Terraform stores a state file that serves as the authoritative record of resources Terraform manages. This file contains the mapping between resource addresses in your configuration and the actual provider-managed resources (IDs, computed attributes, metadata). Terraform relies on state to plan changes and detect drift.

Inspecting state

Use the terraform state family of commands to inspect and manage the state. To see all tracked resources:
To view detailed attributes for a single tracked resource:
The state show output includes both configuration-declared attributes and provider-populated fields (IDs, defaults, and runtime properties). This detailed view is essential for debugging, auditing, and preparing precise state changes.

State management subcommands

The terraform state command exposes subcommands for advanced state maintenance. Terraform automatically creates a timestamped backup of state before any operation that modifies it. Below is a concise reference: The commands are intentionally simple and script-friendly (work well with grep, awk, etc.) for advanced automation and auditing.
State modification commands can cause drift between Terraform and your real infrastructure if used incorrectly. Always work against a backup and prefer non-destructive commands unless you fully understand the implications.

Refreshing state

The image contains the text "Terraform Refresh" with a copyright notice for KodeKloud at the bottom left.
Terraform expects the state file to reflect reality. When resources are modified outside Terraform (console edits, manual CLI/API actions, other orchestration tools), the stored state can diverge — this is configuration drift. Use terraform refresh to reconcile Terraform’s state with the provider by re-querying resource attributes and updating the state file. Important: terraform refresh updates only the state file; it does not change infrastructure. Example configuration using a variable for the storage account name:
Running a refresh (supplying the variable):
After running, Terraform has fetched the current resource attributes from Azure and updated the local state to match — no remote changes are made.
Modern Terraform performs an automatic refresh during operations like terraform plan and terraform apply, so manually running terraform refresh is less common. Still, it’s valuable when resolving drift or preparing for direct state edits.

Key takeaways

  • The Terraform state file is the authoritative record of managed infrastructure.
  • Inspect state with terraform state list and terraform state show.
  • Use terraform state subcommands for advanced maintenance — Terraform creates backups automatically for safety.
  • Run terraform refresh to update the state from provider data; it updates only the state file and does not alter resources.
  • Be cautious with state-modifying commands; test on non-production state or use backups to recover if needed.

Watch Video