Bird
Raised Fist0
Terraformcloud~20 mins

Plan and apply separation in pipelines in Terraform - Practice Problems & Coding Challenges

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
Challenge - 5 Problems
🎖️
Terraform Pipeline Separation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Architecture
intermediate
2:00remaining
Why separate Terraform plan and apply stages in CI/CD pipelines?

Consider a CI/CD pipeline that runs Terraform to manage infrastructure. Why is it important to separate the plan and apply stages into different pipeline steps?

ATo avoid storing the Terraform state file between stages
BBecause Terraform cannot run plan and apply in the same pipeline
CTo speed up the pipeline by running both stages in parallel
DTo allow manual review and approval of planned changes before applying them
Attempts:
2 left
💡 Hint

Think about safety and control when changing infrastructure.

Configuration
intermediate
2:00remaining
Which Terraform CLI command outputs a plan file for later apply?

In a pipeline, you want to generate a plan file that can be applied later. Which Terraform command correctly creates this plan file?

Aterraform plan --save=tfplan.binary
Bterraform apply -out=tfplan.binary
Cterraform plan -out=tfplan.binary
Dterraform apply --plan=tfplan.binary
Attempts:
2 left
💡 Hint

Look for the command that generates a plan file without applying.

service_behavior
advanced
2:00remaining
What happens if you run 'terraform apply' without a saved plan file in a pipeline?

In a pipeline where plan and apply are separated, what is the behavior if the apply stage runs terraform apply without specifying a saved plan file?

ATerraform will prompt for manual confirmation before applying
BTerraform will create a new plan and apply it immediately without review
CTerraform will apply the last saved plan automatically
DTerraform will fail with an error because no plan file is provided
Attempts:
2 left
💡 Hint

Consider what happens when no plan file is given.

security
advanced
2:00remaining
How to securely share Terraform plan files between pipeline stages?

You want to share the Terraform plan file generated in the plan stage with the apply stage in a CI/CD pipeline. What is the best practice to securely handle this file?

AStore the plan file as a pipeline artifact with restricted access and use encryption if supported
BCommit the plan file to the source code repository for easy access
CSend the plan file via email to the apply stage executor
DGenerate the plan file again in the apply stage to avoid sharing
Attempts:
2 left
💡 Hint

Think about secure file transfer and access control in pipelines.

Best Practice
expert
3:00remaining
What is the recommended approach to handle Terraform state in separated plan and apply pipeline stages?

In a pipeline with separated plan and apply stages, how should Terraform state be managed to ensure consistency and avoid conflicts?

AUse a remote backend (e.g., S3, Terraform Cloud) to store state accessible by both stages
BStore the state file locally in the pipeline workspace and pass it between stages
CGenerate a new state file in each stage to isolate changes
DDo not use state files; rely on manual tracking of infrastructure
Attempts:
2 left
💡 Hint

Think about shared access and locking for state files.

Practice

(1/5)
1. What is the main benefit of separating terraform plan and terraform apply in pipelines?
easy
A. It speeds up the deployment by combining steps
B. It skips the need for state files
C. It automatically fixes errors during apply
D. It allows you to review changes before applying them

Solution

  1. Step 1: Understand the purpose of terraform plan

    This command shows what changes Terraform will make without applying them.
  2. Step 2: Understand the purpose of terraform apply

    This command applies the changes to the infrastructure based on the plan.
  3. Final Answer:

    It allows you to review changes before applying them -> Option D
  4. Quick Check:

    Plan shows changes first = B [OK]
Hint: Plan shows changes, apply makes them live [OK]
Common Mistakes:
  • Thinking plan applies changes
  • Believing apply skips state files
  • Assuming steps run faster combined
2. Which command correctly saves a Terraform plan to a file named myplan.tfplan?
easy
A. terraform apply -out=myplan.tfplan
B. terraform save myplan.tfplan
C. terraform plan -out=myplan.tfplan
D. terraform plan save myplan.tfplan

Solution

  1. Step 1: Identify the correct syntax for saving a plan

    The correct command uses terraform plan -out=filename to save the plan.
  2. Step 2: Check each option

    terraform plan -out=myplan.tfplan matches the correct syntax exactly.
  3. Final Answer:

    terraform plan -out=myplan.tfplan -> Option C
  4. Quick Check:

    Plan with -out saves file = C [OK]
Hint: Use 'plan -out=' to save plans, not apply [OK]
Common Mistakes:
  • Using apply instead of plan to save
  • Using incorrect command like 'save'
  • Missing the '-out=' flag
3. Given these commands run in order:
terraform plan -out=planfile
terraform apply planfile
What happens when you run terraform apply planfile?
medium
A. Terraform applies exactly the changes saved in planfile
B. Terraform ignores planfile and creates a new plan
C. Terraform applies changes but may differ from planfile
D. Terraform throws an error because planfile is not a valid command

Solution

  1. Step 1: Understand the role of the saved plan file

    The plan file contains the exact changes Terraform will apply.
  2. Step 2: Understand how terraform apply planfile works

    This command applies the saved plan exactly, without recalculating changes.
  3. Final Answer:

    Terraform applies exactly the changes saved in planfile -> Option A
  4. Quick Check:

    Apply saved plan = exact changes [OK]
Hint: Apply with plan file applies exact saved changes [OK]
Common Mistakes:
  • Thinking apply recalculates plan
  • Assuming apply ignores plan file
  • Believing apply with plan file causes errors
4. You run terraform apply myplan.tfplan but get an error saying the plan file is invalid. What is the most likely cause?
medium
A. You forgot to run terraform init before apply
B. The plan file was created with a different Terraform version
C. You used terraform plan without -out flag
D. The plan file is empty because no changes were detected

Solution

  1. Step 1: Identify compatibility issues with plan files

    Plan files are version-specific and may be invalid if Terraform versions differ.
  2. Step 2: Check other options

    Running init is required but usually causes different errors; missing -out means no plan file saved; empty plan files do not cause invalid file errors.
  3. Final Answer:

    The plan file was created with a different Terraform version -> Option B
  4. Quick Check:

    Version mismatch causes invalid plan file error [OK]
Hint: Check Terraform versions match when using saved plans [OK]
Common Mistakes:
  • Assuming missing init causes invalid plan file error
  • Thinking empty plan files cause invalid file errors
  • Confusing missing -out with invalid plan file
5. You want to implement a pipeline that separates planning and applying Terraform changes. Which sequence of commands ensures you can review the plan before applying exactly those changes?
hard
A. terraform plan -out=planfile && terraform apply planfile
B. terraform apply && terraform plan -out=planfile
C. terraform plan && terraform apply
D. terraform apply -out=planfile && terraform apply planfile

Solution

  1. Step 1: Identify correct order for plan and apply separation

    First, run terraform plan -out=planfile to save the plan and review it.
  2. Step 2: Apply the saved plan exactly

    Then, run terraform apply planfile to apply the reviewed changes exactly.
  3. Final Answer:

    terraform plan -out=planfile && terraform apply planfile -> Option A
  4. Quick Check:

    Plan with -out then apply saved plan = A [OK]
Hint: Plan with -out then apply saved plan file [OK]
Common Mistakes:
  • Applying before planning
  • Not saving plan with -out
  • Using apply -out which is invalid