Terraform CLI overview - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
We want to understand how the time to run Terraform commands changes as we manage more resources.
Specifically, how does the number of resources affect the work Terraform CLI does?
Analyze the time complexity of running Terraform plan and apply on multiple resources.
resource "aws_instance" "example" {
count = var.instance_count
ami = "ami-123456"
instance_type = "t2.micro"
}
output "instance_ids" {
value = aws_instance.example[*].id
}
This code creates a number of virtual machines based on the input count variable.
Terraform performs these repeated actions:
- Primary operation: API calls to create or update each virtual machine resource.
- How many times: Once per resource, so equal to the number of instances specified.
As you increase the number of instances, the number of API calls grows in the same way.
| Input Size (n) | Approx. API Calls/Operations |
|---|---|
| 10 | About 10 API calls |
| 100 | About 100 API calls |
| 1000 | About 1000 API calls |
Pattern observation: The work grows directly with the number of resources.
Time Complexity: O(n)
This means the time to run Terraform commands grows linearly with the number of resources.
[X] Wrong: "Terraform runs all resources in constant time no matter how many there are."
[OK] Correct: Each resource requires separate API calls and processing, so more resources mean more work.
Understanding how Terraform scales with resource count helps you design efficient infrastructure and shows you grasp cloud automation basics.
"What if we used modules that create multiple resources inside? How would that affect the time complexity?"
Practice
terraform init command?Solution
Step 1: Understand the role of
This command sets up the working directory by downloading necessary provider plugins and preparing the environment.terraform initStep 2: Differentiate from other commands
Commands likeapplyanddestroyperform changes, whileinitonly prepares the setup.Final Answer:
To initialize a working directory and download required provider plugins -> Option DQuick Check:
terraform initprepares environment [OK]
- Confusing
initwithapply - Thinking
initchanges infrastructure - Mixing
initwithdestroy
Solution
Step 1: Identify the command to preview changes
terraform planshows what changes will happen without applying them.Step 2: Check other options for correctness
apply --previewandprevieware not valid commands;showdisplays current state, not planned changes.Final Answer:
terraform plan -> Option AQuick Check:
Preview changes =terraform plan[OK]
- Using
apply --previewwhich is invalid - Confusing
showwithplan - Assuming
previewis a Terraform command
terraform init tf plan terraform apply
What will happen when you run these commands?
Solution
Step 1: Understand each command's role
terraform initsets up the environment,terraform planshows what will change, andterraform applymakes those changes.Step 2: Analyze the sequence
The commands run in order: initialize, plan, then apply, which is the recommended workflow.Final Answer:
Initialize the directory, show planned changes, then apply those changes -> Option BQuick Check:
Init -> Plan -> Apply = safe workflow [OK]
- Running apply before plan
- Skipping init before plan
- Confusing plan and apply order
terraform apply but get an error saying the backend is not configured. What is the most likely cause?Solution
Step 1: Understand backend configuration role
The backend stores state remotely and must be set up before applying changes.Step 2: Identify how backend is configured
terraform initconfigures the backend; skipping it causes errors.Final Answer:
You forgot to runterraform initfirst -> Option CQuick Check:
Init configures backend before apply [OK]
- Skipping init and running apply directly
- Assuming credentials cause backend error
- Thinking destroy fixes backend issues
Solution
Step 1: Understand the purpose of
This command deletes all resources managed by Terraform.terraform destroyStep 2: Use
Planning shows what will be destroyed, helping avoid mistakes.terraform planbefore destroyStep 3: Confirm the sequence
Runningterraform planfirst, thenterraform destroyis the safest approach.Final Answer:
terraform destroy after terraform plan to review changes -> Option AQuick Check:
Plan before destroy to avoid surprises [OK]
- Destroying without planning first
- Applying changes before destroying
- Running init after plan
