Terraform plan for preview - Time & Space Complexity
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?"