Code review for infrastructure changes in Terraform - Time & Space Complexity
When reviewing infrastructure code changes, it's important to understand how the time to check and apply these changes grows as the code grows.
We want to know: how does the review and apply process scale with the number of resources changed?
Analyze the time complexity of reviewing and applying changes for multiple Terraform resources.
resource "aws_instance" "example" {
count = var.instance_count
ami = var.ami_id
instance_type = "t2.micro"
}
resource "aws_security_group" "example" {
count = var.sg_count
name = "example-sg-${count.index}"
description = "Example security group"
}
This code creates multiple instances and security groups based on input counts.
Each resource block triggers API calls to create or update resources.
- Primary operation: API calls to provision or update each resource instance.
- How many times: Once per resource instance, repeated for each count.
As the number of instances or security groups increases, the number of API calls grows proportionally.
| Input Size (n) | Approx. API Calls/Operations |
|---|---|
| 10 | About 10 calls for instances + 10 for security groups = 20 |
| 100 | About 100 calls for instances + 100 for security groups = 200 |
| 1000 | About 1000 calls for instances + 1000 for security groups = 2000 |
Pattern observation: The total operations grow directly with the number of resources defined.
Time Complexity: O(n)
This means the time to review and apply changes grows linearly with the number of resources.
[X] Wrong: "Reviewing or applying changes takes the same time no matter how many resources are changed."
[OK] Correct: Each resource requires separate API calls and checks, so more resources mean more work and time.
Understanding how infrastructure changes scale helps you plan and communicate about deployments clearly and confidently.
"What if we used modules to group resources instead of individual resource blocks? How would that affect the time complexity?"