You are viewing a free preview of this lesson.
Subscribe to unlock all 10 lessons in this course and every other course on LearningBro.
Terraform uses a state file to map your configuration to real-world resources. Understanding state is essential for working with Terraform safely and effectively, especially in team environments.
The Terraform state file (terraform.tfstate) is a JSON file that records:
Your Configuration (.tf) State File (.tfstate) Real Infrastructure
┌──────────────────┐ ┌──────────────────┐ ┌──────────────────┐
│ aws_instance.web │ ←──→ │ id: i-0abc123 │ ←──→ │ EC2 Instance │
│ type: t3.micro │ │ type: t3.micro │ │ i-0abc123 │
└──────────────────┘ └──────────────────┘ └──────────────────┘
| Purpose | Description |
|---|---|
| Mapping | Links resource addresses to real-world resource IDs |
| Performance | Caches resource attributes to avoid querying every API on every plan |
| Dependencies | Tracks dependency order for correct creation and destruction |
| Drift detection | Compares state with real infrastructure to detect out-of-band changes |
By default, Terraform stores state locally in terraform.tfstate:
ls -la terraform.tfstate
# -rw-r--r-- 1 user staff 4096 terraform.tfstate
Subscribe to continue reading
Get full access to this lesson and all 10 lessons in this course.