Skip to main content
Welcome to an introduction to Terraform state. This article explains the role of the Terraform state file, what it contains, and why it’s a critical part of the Terraform workflow. If you’ve heard the term “desired state,” this discussion will make that idea practical and concrete. We’ll keep the scope focused on fundamentals. Advanced topics—remote backends, state locking, state migrations, sensitive-data handling, and tooling for state inspection—are out of scope for this introduction. For now, learn what the state file does and how Terraform uses it during planning and apply operations. What this article covers:
  • What the state file is
  • What information the state stores
  • Why Terraform needs state
  • Basic Terraform commands that create and use state
What is the Terraform state file?
  • Think of the state file as Terraform’s memory. It records what Terraform has created and stores metadata that maps your .tf configuration to real infrastructure objects.
  • By default, Terraform writes state as JSON to terraform.tfstate in the working directory unless you configure a remote backend.
Key responsibilities of the state file State file format (simplified) State is stored in JSON. Below is a short, simplified excerpt illustrating typical fields in a small state file:
Basic Terraform workflow that produces and uses state
  • Initialize the working directory (creates or configures the backend and initializes providers):
  • Preview changes (Terraform compares local configuration, saved state, and provider data):
  • Apply changes (updates infrastructure and writes the new state):
Common Terraform CLI commands and their relationship to state
The state file is essential for accurate planning and incremental changes. If the state is missing, corrupted, or out of sync, Terraform cannot reliably determine what changes to make.
Security and collaboration considerations (brief)
  • Sensitive data: The state file can include sensitive values (resource IDs, IPs, endpoints, and sometimes secrets). Do not commit terraform.tfstate or terraform.tfstate.backup to source control.
  • Team use and CI/CD: For collaborative environments, use a remote backend (for example, Terraform Cloud, S3 with DynamoDB locking, or another supported backend) to share state and enable locking. Remote backends help prevent conflicts and support secure storage.
  • Encrypt and restrict access: Ensure backend storage is encrypted at rest and access is limited to only those systems and users that need it.
What to expect next
  • We will inspect a real state file and step through how Terraform uses it during plan and apply.
  • Advanced materials will cover remote backends, state locking, secure handling of sensitive state contents, and state migration strategies.
Links and references

Watch Video