Terraform CLI overview - Time & Space Complexity
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?"