Table of Contents

Introduction

Every Terraform workflow revolves around three core commands: init, plan, and apply. Understanding exactly what each does and when to use them is fundamental to working with Terraform effectively.

The Terraform Workflow

terraform init  →  terraform plan  →  terraform apply
    ↓                    ↓                   ↓
 Setup providers    Preview changes    Execute changes
 Download modules   Show diff          Create/update/destroy
 Configure backend  Detect errors      Update state

terraform init

terraform init initializes a Terraform working directory. Run it first, every time.

What It Does

  1. Downloads providers from the Terraform registry
  2. Downloads modules referenced in your configuration
  3. Initializes the backend (local or remote state)
  4. Creates .terraform directory and lock file

Usage

# Basic initialization
terraform init

# Upgrade providers to latest compatible versions
terraform init -upgrade

# Reconfigure backend
terraform init -reconfigure

# Migrate state to new backend
terraform init -migrate-state

When to Run

  • First time in a new project
  • After adding new providers or modules
  • After changing backend configuration
  • After pulling changes that modify providers

terraform plan

terraform plan creates an execution plan showing what Terraform will do.

What It Does

  1. Reads current state from backend
  2. Reads configuration files
  3. Refreshes resource state from cloud APIs
  4. Calculates diff between desired and actual state
  5. Shows planned changes without executing them

Usage

# Basic plan
terraform plan

# Save plan to file
terraform plan -out=tfplan

# Plan with variables
terraform plan -var="environment=prod"

# Plan specific resource
terraform plan -target=aws_instance.web

# Destroy plan
terraform plan -destroy

# JSON output
terraform plan -json

Reading Plan Output

Plan: 2 to add, 1 to change, 0 to destroy.
  • + Create new resource
  • ~ Update existing resource
  • - Destroy resource
  • -/+ Replace (destroy then create)

terraform apply

terraform apply executes the planned changes against your infrastructure.

What It Does

  1. Runs a plan (unless a saved plan is provided)
  2. Shows planned changes and asks for confirmation
  3. Executes changes in dependency order
  4. Updates state file with new resource data
  5. Shows outputs after completion

Usage

# Interactive apply (shows plan, asks for confirmation)
terraform apply

# Apply saved plan (no confirmation)
terraform apply tfplan

# Auto-approve (skip confirmation)
terraform apply -auto-approve

# Apply with variables
terraform apply -var="instance_type=t3.large"

# Apply specific resource
terraform apply -target=aws_instance.web

Other Essential Commands

terraform destroy

terraform destroy              # Interactive
terraform destroy -auto-approve # No confirmation
terraform plan -destroy        # Preview destruction

terraform validate

terraform validate  # Check syntax without accessing APIs

terraform fmt

terraform fmt       # Format files in current directory
terraform fmt -recursive  # Format all subdirectories
terraform fmt -check      # Check without modifying (CI use)

Production Workflow

# 1. Initialize
terraform init

# 2. Format and validate
terraform fmt -check
terraform validate

# 3. Plan and save
terraform plan -out=tfplan

# 4. Review the plan
terraform show tfplan

# 5. Apply the saved plan
terraform apply tfplan

Hands-On Courses

Conclusion

init, plan, and apply are the three pillars of every Terraform workflow. Master these commands and their flags to work confidently with infrastructure as code.