๐Ÿ”— 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.


๐Ÿง  Why State Matters: Terraformโ€™s โ€œMemoryโ€

๐Ÿ’ก Terraform is stateful โ€” unlike tools like CloudFormation, it remembers what it created.

How It Works:

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:

โš ๏ธ Critical:

Editing .tfstate manually = breaking Terraformโ€™s trust.

โ†’ Leads to state drift, failed plans, and orphaned resources.


๐Ÿšซ Problems with Local State (Default)

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.