Why patterns solve common problems in Terraform - Performance Analysis
We want to see how using common patterns in Terraform affects the work done as we add more resources.
How does the number of repeated steps grow when we use these patterns?
Analyze the time complexity of this Terraform module pattern.
module "web_server" {
source = "./modules/web_server"
count = var.server_count
name = "web-server-${count.index}"
}
resource "aws_security_group" "web_sg" {
count = var.server_count
name = "web-sg-${count.index}"
}
This pattern creates multiple web servers and security groups using a module and count.
Look at what repeats as we increase servers.
- Primary operation: Creating each web server and its security group.
- How many times: Once per server, so it repeats as many times as the server count.
Each new server adds one module and one security group to create.
| Input Size (n) | Approx. Api Calls/Operations |
|---|---|
| 10 | About 20 (10 servers + 10 security groups) |
| 100 | About 200 (100 servers + 100 security groups) |
| 1000 | About 2000 (1000 servers + 1000 security groups) |
Pattern observation: The work grows directly with the number of servers.
Time Complexity: O(n)
This means the work grows in a straight line as you add more servers.
[X] Wrong: "Using modules with count means Terraform does all work once regardless of number."
[OK] Correct: Each count creates separate resources, so work repeats for each one.
Understanding how repeating patterns affect work helps you design scalable infrastructure and explain your choices clearly.
"What if we replaced count with for_each using a map? How would the time complexity change?"