Bird
Raised Fist0
Terraformcloud~10 mins

Terraform plan for preview - Step-by-Step Execution

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Process Flow - Terraform plan for preview
Write Terraform config
↓
Run 'terraform init'
↓
Run 'terraform plan'
↓
Terraform reads current state
↓
Terraform compares desired vs current
↓
Show planned changes
↓
User reviews plan
↓
Decide to apply or modify config
Terraform plan reads your config and current state, then shows what changes it will make without applying them.
Execution Sample
Terraform
terraform init
terraform plan
Initialize Terraform and preview the infrastructure changes without applying them.
Process Table
StepActionEvaluationResult
1Run 'terraform init'Initialize backend and providersBackend and providers ready
2Run 'terraform plan'Read current stateCurrent state loaded
3Compare desired config with current stateDetect differencesPlan created with changes
4Show plan outputDisplay actions (add, change, destroy)User sees what will happen
5User reviews planDecide next stepPlan accepted or config changed
6ExitNo changes applied yetPreview complete
💡 Plan shows changes but does not apply them; user must run 'terraform apply' to make changes.
Status Tracker
VariableStartAfter initAfter planFinal
terraform stateemptyinitializedloaded current stateunchanged (preview only)
plan outputnonenonegenerated with changesdisplayed to user
Key Moments - 3 Insights
Why does 'terraform plan' not change any real infrastructure?
'terraform plan' only shows what would happen; it reads state and config but does not apply changes, as shown in execution_table step 6.
What does Terraform compare during 'plan'?
It compares the desired config with the current state to find differences, as seen in execution_table step 3.
Why must we run 'terraform init' before 'terraform plan'?
'terraform init' sets up providers and backend needed to read state and config, enabling 'plan' to work correctly (execution_table step 1).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the result after running 'terraform init'?
ABackend and providers ready
BPlan created with changes
CCurrent state loaded
DNo changes applied yet
💡 Hint
Check execution_table row 1 under Result column.
At which step does Terraform compare desired config with current state?
AStep 2
BStep 3
CStep 4
DStep 5
💡 Hint
Look at execution_table rows and find where comparison happens.
If you skip 'terraform init', what will likely happen when running 'terraform plan'?
APlan will show changes correctly
BPlan will apply changes automatically
CPlan will fail due to missing backend/providers
DPlan will ignore current state
💡 Hint
Refer to key_moments about why 'terraform init' is needed before 'plan'.
Concept Snapshot
terraform init: prepares backend and providers
terraform plan: previews changes without applying
Reads current state and compares with config
Shows add/change/destroy actions
No real changes until 'terraform apply' runs
Full Transcript
Terraform plan is a command that lets you see what changes Terraform will make to your cloud infrastructure before actually making them. First, you run 'terraform init' to set up the environment. Then, 'terraform plan' reads the current state and compares it with your desired configuration. It shows a list of changes it will perform, like adding or removing resources. This preview helps you avoid surprises. No real changes happen until you run 'terraform apply'.

Practice

(1/5)
1. What is the main purpose of the terraform plan command?
easy
A. To destroy all existing infrastructure
B. To apply changes directly to cloud resources
C. To initialize the Terraform working directory
D. To preview changes Terraform will make without applying them

Solution

  1. Step 1: Understand the role of terraform plan

    This command shows what changes Terraform will make but does not apply them.
  2. Step 2: Compare with other commands

    terraform apply applies changes, terraform destroy removes resources, and terraform init initializes the directory.
  3. Final Answer:

    To preview changes Terraform will make without applying them -> Option D
  4. Quick Check:

    Preview changes = terraform plan [OK]
Hint: Plan previews changes, apply makes changes [OK]
Common Mistakes:
  • Confusing plan with apply
  • Thinking plan modifies resources
  • Mixing plan with init or destroy
2. Which of the following is the correct syntax to run a Terraform plan with a specific variable file named vars.tfvars?
easy
A. terraform plan --vars-file=vars.tfvars
B. terraform plan --varfile vars.tfvars
C. terraform plan -var-file=vars.tfvars
D. terraform plan -varfile=vars.tfvars

Solution

  1. Step 1: Recall correct flag for variable files

    The correct flag is -var-file with a hyphen between var and file.
  2. Step 2: Check syntax correctness

    terraform plan -var-file=vars.tfvars uses -var-file=vars.tfvars, which is the official syntax. Other options use incorrect flags.
  3. Final Answer:

    terraform plan -var-file=vars.tfvars -> Option C
  4. Quick Check:

    Use -var-file=filename for variable files [OK]
Hint: Use -var-file=filename to specify variable files [OK]
Common Mistakes:
  • Using --varfile instead of -var-file
  • Mixing underscores or missing hyphens
  • Using --vars-file which is invalid
3. Given this Terraform plan output snippet:
  # aws_instance.example will be created
  + resource "aws_instance" "example" {
      + ami           = "ami-123456"
      + instance_type = "t2.micro"
    }

What does the plus sign (+) before the resource indicate?
medium
A. The resource will be created
B. The resource will be destroyed
C. The resource will be updated in place
D. The resource is unchanged

Solution

  1. Step 1: Understand Terraform plan symbols

    The plus sign (+) means Terraform plans to add or create the resource.
  2. Step 2: Differentiate from other symbols

    Minus (-) means destroy, ~ means update, no symbol means unchanged.
  3. Final Answer:

    The resource will be created -> Option A
  4. Quick Check:

    Plus sign (+) = create resource [OK]
Hint: Plus (+) means create, minus (-) means destroy [OK]
Common Mistakes:
  • Confusing + with update or destroy
  • Ignoring symbols and guessing
  • Assuming no symbol means create
4. You run terraform plan but get this error:
Error: No configuration files found!

What is the most likely cause?
medium
A. You ran the command outside the Terraform configuration directory
B. You forgot to run terraform init first
C. Your Terraform version is outdated
D. Your cloud provider credentials are missing

Solution

  1. Step 1: Analyze the error message

    The error says no configuration files found, meaning Terraform can't find .tf files.
  2. Step 2: Identify common causes

    This usually happens if you run the command outside the folder containing Terraform files.
  3. Final Answer:

    You ran the command outside the Terraform configuration directory -> Option A
  4. Quick Check:

    No config files = wrong directory [OK]
Hint: Run plan inside folder with .tf files [OK]
Common Mistakes:
  • Assuming init fixes missing files
  • Blaming credentials for config file errors
  • Ignoring current working directory
5. You want to preview changes only for a specific resource named aws_s3_bucket.mybucket without affecting others. Which command achieves this?
hard
A. terraform plan -only=aws_s3_bucket.mybucket
B. terraform plan -target=aws_s3_bucket.mybucket
C. terraform plan --filter=aws_s3_bucket.mybucket
D. terraform plan -resource=aws_s3_bucket.mybucket

Solution

  1. Step 1: Recall how to target specific resources in plan

    The -target flag limits the plan to specified resources.
  2. Step 2: Verify correct flag usage

    terraform plan -target=aws_s3_bucket.mybucket uses -target=aws_s3_bucket.mybucket, which is the correct syntax. Other options are invalid flags.
  3. Final Answer:

    terraform plan -target=aws_s3_bucket.mybucket -> Option B
  4. Quick Check:

    Use -target to preview specific resources [OK]
Hint: Use -target=resource_name to limit plan scope [OK]
Common Mistakes:
  • Using non-existent flags like -only or --filter
  • Forgetting to specify resource type and name
  • Assuming plan always shows all resources