0
0
Terraformcloud~5 mins

Blue-green infrastructure pattern in Terraform - Time & Space Complexity

Choose your learning style9 modes available
Time Complexity: Blue-green infrastructure pattern
O(n)
Understanding Time 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.

Scenario Under Consideration

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.

Identify Repeating Operations

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).
How Execution Grows With Input

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
10About 20 instance creations plus 2 load balancers
100About 200 instance creations plus 2 load balancers
1000About 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.

Final Time Complexity

Time Complexity: O(n)

This means the work grows linearly with the number of instances, doubled due to two environments.

Common Mistake

[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.

Interview Connect

Understanding how resource counts grow in blue-green setups helps you design scalable and maintainable infrastructure.

Self-Check

What if we switched from blue-green to a single environment with rolling updates? How would the time complexity change?