State File Contents and Structure
You'll learn what's inside Terraform's state file and how it organizes information about your infrastructure.
What's Inside terraform.tfstate?
When you run terraform apply, Terraform creates or updates a file called terraform.tfstate. This file is written in JSON format—a structured, machine-readable way to store data. Think of it as Terraform's memory bank: a detailed inventory of every resource it manages, complete with all the settings and identifiers needed to track and update them.
The JSON Structure
The state file contains several key sections:
- Version metadata: Tracks which Terraform version created the state
- Terraform version: The exact version of Terraform used
- Resources array: The heart of the file—a list of all managed resources
- Outputs: Values you've declared to be exposed
Each resource entry stores:
- Type and name: How you defined it (e.g.,
aws_instance.web_server) - Provider: Which provider manages it
- Attributes: Every property of the resource (IP addresses, IDs, DNS names, etc.)
- Dependencies: Which other resources it depends on
- Metadata: Creation time, schema version, and more
Why This Structure Matters
Terraform reads this JSON to understand what already exists in your cloud account. When you run terraform plan, it compares your configuration files against these stored attributes to determine what changes are needed.
Key Takeaway: The state file is a JSON document that stores a complete snapshot of your managed infrastructure's resource types, unique identifiers, attributes, and relationships—serving as Terraform's reference for tracking and updating your infrastructure.