Phase1 Cloud Iac

Terraform State Management

Introduction

Terraform keeps track of what it has created in a "state file." This file is how Terraform knows what exists in the real world and what changes need to be made. State management is one of the trickiest parts of Terraform. Get it wrong and you'll have "drift" between what Terraform thinks exists and what actually exists, leading to confusing errors and potentially destructive changes.

Why This Matters

By default, Terraform stores state in a local file (terraform.tfstate). This works for learning but breaks immediately when: - Two people work on the same infrastructure (whose state file is correct?) - You run Terraform from CI/CD (where does the state file live?) - You accidentally delete the state file (Terraform "forgets" everything it created) The solution is "remote state" — storing the state file in S3 with locking via DynamoDB. This is standard practice for any team or production use. Understanding state also helps you debug issues: "Why is Terraform trying to recreate this resource?" is almost always a state question.

Tasks

Hint: It's JSON — look for your S3 bucket in there
Hint: You should see a lock error — this is the DynamoDB lock working
Hint: terraform import aws_s3_bucket.example bucket-name

Check Understanding

AI Assistant

Ask questions about this topic. The assistant has context about what you're learning.