๐ Video: Day 4: State File & S3 Backend
โฑ๏ธ Prerequisite: Day 3 (S3 Bucket Lab) โ
๐ Repo: /day04/ โ TASK.md
๐ฏ Goal: Move from local (risky!) to remote, encrypted, locked state โ the foundation of production-ready Terraform.
๐ก Terraform is stateful โ unlike tools like CloudFormation, it remembers what it created.
flowchart LR
A[main.tf] -->|Desired State| B[Terraform Core]
C[terraform.tfstate] -->|Actual State| B
B -->|Compare| D{{Drift?}}
D -->|Yes| E[Plan: 1 to add, 2 to changeโฆ]
D -->|No| F[โNo changes.โ]
โ
State file (terraform.tfstate) stores:
s3-abc123, vpc-xyz789)bucket_region, vpc_cidr)โ ๏ธ Critical:
Editing
.tfstatemanually = breaking Terraformโs trust.โ Leads to state drift, failed plans, and orphaned resources.
| Risk | Impact |
|---|---|
| ๐ฅ๏ธ Local-only | Only you can run apply โ no team collaboration |
| ๐ No locking | Two engineers run apply โ state file corruption ๐ฅ |
| ๐๏ธ No backup | Accidental rm terraform.tfstate = infrastructure โorphanedโ |
| ๐ Git temptation | Committing state = exposing account IDs, resource mappings |
โ Real-world analogy:
Local state = saving your bank ledger on a sticky note.
Remote backend = using a vault with audit logs and dual control.