Why the workflow matters in Terraform - Performance Analysis
Start learning this pattern below
Jump into concepts and practice - no test required
When using Terraform, the order of steps affects how long it takes to set up or change resources.
We want to see how the workflow impacts the number of actions Terraform performs.
Analyze the time complexity of this Terraform workflow.
resource "aws_instance" "example" {
count = var.instance_count
ami = var.ami_id
instance_type = "t2.micro"
}
output "instance_ids" {
value = aws_instance.example[*].id
}
This creates multiple instances based on a count variable and outputs their IDs.
Look at what repeats when we increase the number of instances.
- Primary operation: Creating each AWS instance resource.
- How many times: Once per instance, equal to
var.instance_count.
As you ask for more instances, Terraform creates more resources one by one.
| Input Size (n) | Approx. API Calls/Operations |
|---|---|
| 10 | 10 instance creations |
| 100 | 100 instance creations |
| 1000 | 1000 instance creations |
Pattern observation: The number of operations grows directly with the number of instances.
Time Complexity: O(n)
This means the time to complete grows in a straight line as you add more instances.
[X] Wrong: "Adding more instances won't change the time much because Terraform handles all at once."
[OK] Correct: Each instance requires a separate API call and setup, so more instances mean more work and longer time.
Understanding how workflows affect time helps you plan and explain infrastructure changes clearly and confidently.
"What if we used modules to create instances instead of count? How would the time complexity change?"
Practice
terraform plan before terraform apply?Solution
Step 1: Understand the purpose of
This command previews the changes Terraform will make to your infrastructure without applying them.terraform planStep 2: Compare with
terraform applyterraform applyactually makes the changes, so planning first helps avoid mistakes.Final Answer:
It shows the changes Terraform will make before applying them -> Option AQuick Check:
Plan previews changes = D [OK]
- Skipping plan and applying directly
- Thinking plan applies changes
- Confusing plan with destroy
Solution
Step 1: Identify the initialization command
terraform initsets up the working directory by downloading providers and preparing backend.Step 2: Check other options
Commands liketerraform start,deploy, orconfiguredo not exist in Terraform.Final Answer:
terraform init -> Option AQuick Check:
Initialize with init = B [OK]
- Using non-existent commands
- Trying to apply before init
- Confusing init with plan
terraform init tf plan tf apply
What will happen if you run
terraform apply without running terraform plan first?Solution
Step 1: Understand
Terraform always performs an internal plan, shows the execution plan (preview of changes), and prompts for confirmation before applying.terraform applybehaviorStep 2: Check if plan is mandatory
Runningterraform planfirst is recommended for detailed review and saving plans, butterraform applyautomatically runs the plan step.Final Answer:
Terraform will automatically run plan before apply -> Option CQuick Check:
Apply auto-runs plan [OK]
- Assuming apply always errors without plan
- Thinking apply auto-runs plan silently
- Believing apply deletes resources by default
terraform apply but noticed unexpected resource deletions. What is the most likely cause related to workflow?Solution
Step 1: Identify workflow mistake
Skippingterraform planmeans you miss previewing changes, leading to surprises like deletions.Step 2: Evaluate other options
Running init twice, outdated version, or missing env vars usually cause errors, not unexpected deletions.Final Answer:
Skippingterraform planand not reviewing changes -> Option BQuick Check:
Skip plan = risk of unwanted deletions [OK]
- Ignoring plan output
- Blaming init for deletions
- Assuming version causes deletions
Solution
Step 1: Understand the correct workflow order
First,terraform initprepares the workspace, thenterraform planpreviews changes, followed by reviewing the plan carefully.Step 2: Apply changes only after review
Applying changes after review ensures safety and avoids downtime or mistakes.Final Answer:
terraform init -> terraform plan -> review plan -> terraform apply -> Option DQuick Check:
Init, plan, review, apply = C [OK]
- Applying before planning
- Skipping plan review
- Running commands in wrong order
