Terraform apply for execution - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When we run terraform apply, it creates or updates cloud resources. We want to understand how the time it takes grows as we add more resources.
How does the number of resources affect the total work Terraform does?
Analyze the time complexity of applying a Terraform configuration that creates 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 a number of virtual machines equal to instance_count. Terraform provisions each instance one by one.
Identify the API calls, resource provisioning, data transfers that repeat.
- Primary operation: Creating each virtual machine resource via cloud API calls.
- How many times: Once per instance, equal to
instance_count.
As you increase the number of instances, Terraform makes more API calls, one per instance.
| Input Size (n) | Approx. API Calls/Operations |
|---|---|
| 10 | 10 API calls to create 10 instances |
| 100 | 100 API calls to create 100 instances |
| 1000 | 1000 API calls to create 1000 instances |
Pattern observation: The number of API calls grows directly with the number of instances.
Time Complexity: O(n)
This means the time to apply grows in a straight line with the number of resources you create.
[X] Wrong: "Terraform applies all resources instantly, so time stays the same no matter how many resources."
[OK] Correct: Each resource requires a separate API call and provisioning time, so more resources mean more total time.
Understanding how Terraform scales with resource count helps you design efficient infrastructure and estimate deployment times in real projects.
What if Terraform could create multiple resources in parallel? How would the time complexity change?
Practice
terraform apply command do in simple terms?Solution
Step 1: Understand the purpose of terraform apply
The command is used to make your cloud setup real by creating or updating resources defined in your configuration files.Step 2: Compare options with command behavior
Only It creates or changes cloud resources based on your configuration. correctly describes this behavior. Options A, B, and C describe actions terraform apply does not perform.Final Answer:
It creates or changes cloud resources based on your configuration. -> Option BQuick Check:
terraform apply = create/change resources [OK]
- Thinking terraform apply only shows plans without applying
- Confusing terraform apply with terraform destroy
- Assuming terraform apply backs up resources
Solution
Step 1: Recall terraform apply options for skipping approval
The official option to skip manual approval is-auto-approve.Step 2: Verify other options
Options B, C, and D are not valid terraform flags and will cause errors.Final Answer:
terraform apply -auto-approve -> Option CQuick Check:
-auto-approve skips approval [OK]
- Using incorrect flags like --no-approval
- Trying to use --force which is invalid
- Assuming terraform apply never asks for approval
Plan: 2 to add, 1 to change, 0 to destroy.
What will happen after you approve the apply?
Solution
Step 1: Interpret the plan summary
The plan says 2 to add means 2 new resources will be created, 1 to change means 1 existing resource will be updated, and 0 to destroy means no resources will be deleted.Step 2: Match the plan to the options
Only Two resources will be created, one resource will be updated, none deleted. correctly describes this outcome. Options A, C, and D misunderstand the counts.Final Answer:
Two resources will be created, one resource will be updated, none deleted. -> Option AQuick Check:
Plan add=2, change=1, destroy=0 [OK]
- Mixing up add and destroy counts
- Assuming zero destroy means no changes at all
- Confusing change with destroy
terraform apply but get an error: Error: No configuration files found. What is the likely cause?Solution
Step 1: Understand the error message
The error says no configuration files found, meaning terraform cannot find any .tf files in the current folder.Step 2: Evaluate options
You ran terraform apply in a directory without terraform files. matches this cause. You forgot to initialize terraform with terraform init. would cause a different error about initialization. You used the wrong terraform version. is unlikely to cause this specific error. You did not approve the plan before applying. is unrelated because approval happens after plan generation.Final Answer:
You ran terraform apply in a directory without terraform files. -> Option AQuick Check:
No .tf files = error [OK]
- Assuming terraform init missing causes this error
- Thinking approval is needed before apply runs
- Blaming terraform version without checking files
Solution
Step 1: Understand CI/CD automation needs
Automation requires no manual approval and safe application of changes. Saving a plan file ensures the exact changes are applied.Step 2: Evaluate command options
Useterraform apply -auto-approveafter runningterraform planand saving the plan file. correctly uses terraform plan to create a plan file, then applies it with -auto-approve to skip manual confirmation. Runterraform applydirectly without plan or approval. skips planning, which is risky. Runterraform planonly, then manually approve changes later. requires manual approval, not automated. Useterraform apply --forceto skip all checks. uses an invalid flag.Final Answer:
Use terraform apply -auto-approve after running terraform plan and saving the plan file. -> Option DQuick Check:
Plan then apply with -auto-approve for automation [OK]
- Skipping plan step in automation
- Using invalid flags like --force
- Expecting manual approval in CI/CD
