Terraform
Basics & Fundamentals
- Infrastructure as Code (IaC)
- Declarative Syntax in IaC
- Terraform Configuration Files
- Terraform CLI
- Terraform Init
- Terraform Plan
- Terraform Apply
- Terraform Destroy
Providers & Resources
๐ Terraform Plan: Safely Previewing Infrastructure Changes
When managing infrastructure with Terraform, the last thing you want is to accidentally break production with unexpected changes. Thatโs where terraform plan
comes in.
The Terraform Plan command is like a dry run or blueprint of what Terraform intends to do. Instead of applying changes directly, it analyzes your .tf
configuration files and shows:
- Which resources will be created.
- Which resources will be updated.
- Which resources will be destroyed.
๐ Think of it as a flight simulator for infrastructure. You can test your changes safely before actually deploying them.
โ๏ธ What Does Terraform Plan Do?
When you run:
terraform plan
Terraform:
-
Loads Current State
- Reads the state file (
terraform.tfstate
or remote backend).
- Reads the state file (
-
Compares State vs Config
- Compares your current infrastructure with
.tf
configuration files.
- Compares your current infrastructure with
-
Generates Execution Plan
- Outputs a list of actions (add, change, destroy).
-
Prepares Apply
- Stores the plan so that it can be passed to
terraform apply
.
- Stores the plan so that it can be passed to
๐ Nothing is actually deployed or destroyed. Itโs a read-only preview.
๐ Terraform Plan Syntax & Options
Basic usage:
terraform plan
Useful options:
-out=plan.out
โ Save plan for later execution.-input=false
โ Disable interactive input.-destroy
โ Show what will be destroyed.-refresh=false
โ Skip refreshing state.-var
or-var-file
โ Pass variables into the plan.
๐ 3 Unique Real-World Examples of Terraform Plan
โ Example 1: Previewing AWS S3 Bucket Creation
Step 1: main.tf
provider "aws" { region = "us-east-1"}
resource "aws_s3_bucket" "example" { bucket = "terraform-plan-example-bucket" acl = "private"}
Step 2: Run Terraform Plan
terraform initterraform plan
Output Example:
+ aws_s3_bucket.example bucket: "terraform-plan-example-bucket" acl: "private"
๐ +
means Terraform will create this resource.
โ Example 2: Detecting Updates in Resources
Step 1: Update ACL in main.tf
resource "aws_s3_bucket" "example" { bucket = "terraform-plan-example-bucket" acl = "public-read"}
Step 2: Run Plan
terraform plan
Output Example:
~ aws_s3_bucket.example acl: "private" โ "public-read"
๐ ~
means Terraform will update the existing resource.
โ Example 3: Planning Destruction of Resources
Step 1: Remove S3 Bucket Resource
# (Removed aws_s3_bucket block from main.tf)
Step 2: Run Plan
terraform plan
Output Example:
- aws_s3_bucket.example bucket: "terraform-plan-example-bucket"
๐ -
means Terraform will destroy this resource.
๐ฏ Why is Terraform Plan Important?
-
Prevents Mistakes
- Avoids accidentally deleting production resources.
-
Improves Collaboration
- Teams can review plan outputs before applying.
-
Compliance & Approvals
- Execution plans can be audited and approved.
-
Safer CI/CD Pipelines
- Ensures infrastructure deployments are predictable.
-
Debugging Tool
- Helps identify why Terraform thinks something should change.
๐ง How to Remember Terraform Plan (Exam & Interview)
Mnemonic: P.C.D.A
- P โ Preview changes
- C โ Compare current vs desired state
- D โ Detect create/update/destroy actions
- A โ Approve before apply
๐ Interview Answer: โTerraform plan generates an execution plan showing what resources will be created, modified, or destroyed without making actual changes. Itโs a dry run that helps ensure safe deployments.โ
๐ Best Practices for Terraform Plan
- Always run
terraform plan
before every apply. - Use
terraform plan -out=plan.out
in CI/CD pipelines. - Commit plan outputs for audit purposes.
- Review plan carefully when working with production resources.
- Use variables and workspaces to test multiple environments.
๐ฎ Future Enhancements of Terraform Plan
- AI-assisted plan review โ Detect risky changes.
- Visual execution plans โ Graphical previews of create/update/destroy.
- Policy-driven approvals โ Auto-block unsafe plans.
- Drift detection โ Highlight infrastructure drift more clearly.
๐ Summary
-
terraform plan
shows what Terraform will do without actually doing it. -
It compares the desired configuration with the current state.
-
Symbols:
+
โ Create~
โ Update-
โ Destroy
-
Essential for preventing mistakes in production.
-
Best remembered with P.C.D.A โ Preview, Compare, Detect, Approve.
โ Final Takeaway
The Terraform Plan command is your safety net. Without it, applying changes would be like deploying blindfolded. By running terraform plan
, you:
- See exactly what will change.
- Share plans with teammates.
- Avoid unexpected outages.
๐ Mastering terraform plan
builds confidence in your Terraform workflow and makes you a more reliable DevOps engineer.