Terraform plan for preview - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When using Terraform plan to preview changes, it is important to understand how the time to generate this preview grows as the number of resources increases.
We want to know how the number of resources affects the time Terraform takes to prepare the plan.
Analyze the time complexity of running a Terraform plan on multiple resources.
resource "aws_instance" "example" {
count = var.instance_count
ami = "ami-12345678"
instance_type = "t2.micro"
}
output "instance_ids" {
value = aws_instance.example[*].id
}
This code creates multiple AWS instances based on a variable count and outputs their IDs. Terraform plan previews the changes before applying.
Identify the API calls, resource provisioning, data transfers that repeat.
- Primary operation: Terraform queries the state and provider APIs for each resource to check current and desired states.
- How many times: Once per resource instance, so it repeats as many times as the count of resources.
As the number of resources increases, the number of API calls and checks grows proportionally.
| Input Size (n) | Approx. API Calls/Operations |
|---|---|
| 10 | About 10 resource checks |
| 100 | About 100 resource checks |
| 1000 | About 1000 resource checks |
Pattern observation: The work grows directly with the number of resources, so doubling resources roughly doubles the work.
Time Complexity: O(n)
This means the time to prepare the plan grows linearly with the number of resources.
[X] Wrong: "Terraform plan time stays the same no matter how many resources I have."
[OK] Correct: Each resource requires separate checks, so more resources mean more work and longer plan times.
Understanding how Terraform plan scales helps you design infrastructure that is easier to manage and predict in real projects.
"What if we used modules that create multiple resources internally? How would that affect the time complexity of the plan?"
Practice
terraform plan command?Solution
Step 1: Understand the role of
This command shows what changes Terraform will make but does not apply them.terraform planStep 2: Compare with other commands
terraform applyapplies changes,terraform destroyremoves resources, andterraform initinitializes the directory.Final Answer:
To preview changes Terraform will make without applying them -> Option DQuick Check:
Preview changes = terraform plan [OK]
- Confusing plan with apply
- Thinking plan modifies resources
- Mixing plan with init or destroy
vars.tfvars?Solution
Step 1: Recall correct flag for variable files
The correct flag is-var-filewith a hyphen between var and file.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.Final Answer:
terraform plan -var-file=vars.tfvars -> Option CQuick Check:
Use -var-file=filename for variable files [OK]
- Using --varfile instead of -var-file
- Mixing underscores or missing hyphens
- Using --vars-file which is invalid
# 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?
Solution
Step 1: Understand Terraform plan symbols
The plus sign (+) means Terraform plans to add or create the resource.Step 2: Differentiate from other symbols
Minus (-) means destroy, ~ means update, no symbol means unchanged.Final Answer:
The resource will be created -> Option AQuick Check:
Plus sign (+) = create resource [OK]
- Confusing + with update or destroy
- Ignoring symbols and guessing
- Assuming no symbol means create
terraform plan but get this error:Error: No configuration files found!
What is the most likely cause?
Solution
Step 1: Analyze the error message
The error says no configuration files found, meaning Terraform can't find .tf files.Step 2: Identify common causes
This usually happens if you run the command outside the folder containing Terraform files.Final Answer:
You ran the command outside the Terraform configuration directory -> Option AQuick Check:
No config files = wrong directory [OK]
- Assuming init fixes missing files
- Blaming credentials for config file errors
- Ignoring current working directory
aws_s3_bucket.mybucket without affecting others. Which command achieves this?Solution
Step 1: Recall how to target specific resources in plan
The-targetflag limits the plan to specified resources.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.Final Answer:
terraform plan -target=aws_s3_bucket.mybucket -> Option BQuick Check:
Use -target to preview specific resources [OK]
- Using non-existent flags like -only or --filter
- Forgetting to specify resource type and name
- Assuming plan always shows all resources
