Blue-green infrastructure pattern in Terraform - Time & Space Complexity
We want to understand how the work grows when using the blue-green infrastructure pattern in Terraform.
Specifically, how many resources and API calls happen as we manage two environments.
Analyze the time complexity of this Terraform snippet managing blue and green environments.
resource "aws_instance" "blue" {
count = var.instance_count
ami = var.ami_id
instance_type = var.instance_type
}
resource "aws_instance" "green" {
count = var.instance_count
ami = var.ami_id
instance_type = var.instance_type
}
resource "aws_lb" "blue_lb" {
# Load balancer for blue environment
}
resource "aws_lb" "green_lb" {
# Load balancer for green environment
}
This creates two sets of instances and load balancers for blue and green environments.
Look at what repeats when we increase instance count.
- Primary operation: Creating EC2 instances for blue and green environments.
- How many times: Twice the number of instances (once for blue, once for green).
As we increase the number of instances, the total work grows because we create two sets of instances.
| Input Size (n) | Approx. Api Calls/Operations |
|---|---|
| 10 | About 20 instance creations plus 2 load balancers |
| 100 | About 200 instance creations plus 2 load balancers |
| 1000 | About 2000 instance creations plus 2 load balancers |
Pattern observation: The number of instance creations doubles as input size doubles, because of blue and green sets.
Time Complexity: O(n)
This means the work grows linearly with the number of instances, doubled due to two environments.
[X] Wrong: "Managing blue-green environments only doubles the load balancer work, not the instances."
[OK] Correct: Each environment has its own full set of instances, so instance creation doubles, not just load balancers.
Understanding how resource counts grow in blue-green setups helps you design scalable and maintainable infrastructure.
What if we switched from blue-green to a single environment with rolling updates? How would the time complexity change?