Plan and apply separation in pipelines in Terraform - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When using Terraform pipelines, we want to know how the time to plan and apply changes grows as we add more separated pipeline stages.
We ask: How does splitting work into separate pipeline steps affect the total operations needed?
Analyze the time complexity of this Terraform pipeline setup.
terraform {
backend "s3" {}
}
module "network" {
source = "./modules/network"
count = var.env_count
}
module "compute" {
source = "./modules/compute"
count = var.env_count
}
This pipeline separates infrastructure into network and compute modules, each applied in stages for multiple environments.
Look at what repeats as we increase environments.
- Primary operation: Terraform plan and apply for each module per environment.
- How many times: Twice per environment (once for network, once for compute).
As the number of environments grows, the total operations increase proportionally.
| Input Size (n) | Approx. API Calls/Operations |
|---|---|
| 10 | 20 (2 modules x 10 envs) |
| 100 | 200 (2 modules x 100 envs) |
| 1000 | 2000 (2 modules x 1000 envs) |
Pattern observation: Operations grow linearly with the number of environments multiplied by the number of pipeline stages.
Time Complexity: O(n)
This means the total work grows directly in proportion to how many environments you have.
[X] Wrong: "Separating pipeline stages will reduce total operations to a constant time."
[OK] Correct: Each environment still needs its own plan and apply per stage, so total operations add up, not stay fixed.
Understanding how pipeline separation affects operation counts helps you design scalable infrastructure workflows confidently.
What if we combined all modules into one pipeline stage? How would the time complexity change?
Practice
terraform plan and terraform apply in pipelines?Solution
Step 1: Understand the purpose of
This command shows what changes Terraform will make without applying them.terraform planStep 2: Understand the purpose of
This command applies the changes to the infrastructure based on the plan.terraform applyFinal Answer:
It allows you to review changes before applying them -> Option DQuick Check:
Plan shows changes first = B [OK]
- Thinking plan applies changes
- Believing apply skips state files
- Assuming steps run faster combined
myplan.tfplan?Solution
Step 1: Identify the correct syntax for saving a plan
The correct command usesterraform plan -out=filenameto save the plan.Step 2: Check each option
terraform plan -out=myplan.tfplan matches the correct syntax exactly.Final Answer:
terraform plan -out=myplan.tfplan -> Option CQuick Check:
Plan with -out saves file = C [OK]
- Using apply instead of plan to save
- Using incorrect command like 'save'
- Missing the '-out=' flag
terraform plan -out=planfileterraform apply planfileWhat happens when you run
terraform apply planfile?Solution
Step 1: Understand the role of the saved plan file
The plan file contains the exact changes Terraform will apply.Step 2: Understand how
This command applies the saved plan exactly, without recalculating changes.terraform apply planfileworksFinal Answer:
Terraform applies exactly the changes saved in planfile -> Option AQuick Check:
Apply saved plan = exact changes [OK]
- Thinking apply recalculates plan
- Assuming apply ignores plan file
- Believing apply with plan file causes errors
terraform apply myplan.tfplan but get an error saying the plan file is invalid. What is the most likely cause?Solution
Step 1: Identify compatibility issues with plan files
Plan files are version-specific and may be invalid if Terraform versions differ.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.Final Answer:
The plan file was created with a different Terraform version -> Option BQuick Check:
Version mismatch causes invalid plan file error [OK]
- Assuming missing init causes invalid plan file error
- Thinking empty plan files cause invalid file errors
- Confusing missing -out with invalid plan file
Solution
Step 1: Identify correct order for plan and apply separation
First, runterraform plan -out=planfileto save the plan and review it.Step 2: Apply the saved plan exactly
Then, runterraform apply planfileto apply the reviewed changes exactly.Final Answer:
terraform plan -out=planfile && terraform apply planfile -> Option AQuick Check:
Plan with -out then apply saved plan = A [OK]
- Applying before planning
- Not saving plan with -out
- Using apply -out which is invalid
