Blue-green infrastructure pattern in Terraform - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
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?
Practice
blue-green infrastructure pattern in Terraform deployments?Solution
Step 1: Understand the blue-green pattern concept
The blue-green pattern uses two identical environments to ensure zero downtime during updates.Step 2: Identify the main goal in Terraform deployments
Terraform manages these environments and switches traffic between them to avoid downtime.Final Answer:
To avoid downtime by switching traffic between two identical environments -> Option DQuick Check:
Blue-green pattern = avoid downtime [OK]
- Thinking it reduces costs by using one environment
- Confusing it with scaling servers in one environment
- Assuming it automates backups
Solution
Step 1: Identify Terraform resources related to traffic routing
Load balancer listener rules control how traffic is routed to target groups.Step 2: Match resource to blue-green traffic switch
Theaws_lb_listener_ruleresource allows switching traffic between blue and green target groups.Final Answer:
aws_lb_listener_rule -> Option AQuick Check:
Traffic switch uses listener rules [OK]
- Choosing aws_instance which manages servers, not traffic
- Selecting aws_s3_bucket which is for storage
- Picking aws_security_group which controls firewall rules
resource "aws_lb_listener_rule" "blue" {
listener_arn = aws_lb_listener.front_end.arn
priority = 10
action {
type = "forward"
target_group_arn = aws_lb_target_group.blue.arn
}
condition {
path_pattern {
values = ["/blue/*"]
}
}
}
resource "aws_lb_listener_rule" "green" {
listener_arn = aws_lb_listener.front_end.arn
priority = 20
action {
type = "forward"
target_group_arn = aws_lb_target_group.green.arn
}
condition {
path_pattern {
values = ["/green/*"]
}
}
}
What happens when a user visits /green/home?Solution
Step 1: Analyze path pattern conditions in listener rules
The green listener rule matches paths starting with/green/*and forwards to the green target group.Step 2: Match user request path to rules
The request/green/homematches the green rule condition, so traffic goes to the green target group.Final Answer:
Traffic is routed to the green target group -> Option AQuick Check:
Path /green/* routes to green group [OK]
- Assuming default routing to blue group
- Thinking traffic is blocked without default rule
- Believing traffic splits between groups
resource "aws_lb_listener_rule" "blue" {
listener_arn = aws_lb_listener.front_end.arn
priority = 10
action {
type = "forward"
target_group_arn = aws_lb_target_group.blue.arn
}
condition {
host_header {
values = ["blue.example.com"]
}
}
}
resource "aws_lb_listener_rule" "green" {
listener_arn = aws_lb_listener.front_end.arn
priority = 10
action {
type = "forward"
target_group_arn = aws_lb_target_group.green.arn
}
condition {
host_header {
values = ["green.example.com"]
}
}
}
What is the likely problem?Solution
Step 1: Check listener rule priorities
Both rules have priority 10, which causes a conflict because priorities must be unique.Step 2: Understand effect of priority conflict
Load balancer cannot decide which rule to apply, so traffic routing fails or is unpredictable.Final Answer:
Both listener rules have the same priority, causing conflict -> Option CQuick Check:
Unique priorities required for listener rules [OK]
- Ignoring priority uniqueness
- Assuming host_header condition is invalid
- Overlooking target group correctness
Solution
Step 1: Understand blue-green deployment goals
The goal is zero downtime by having two identical environments and switching traffic atomically.Step 2: Evaluate deployment approaches
Deploying to green, testing, then switching load balancer traffic ensures smooth transition without downtime.Step 3: Compare other options
Direct deploy with restart causes downtime; manual deletion delays switch; DNS TTL causes slow switch and possible downtime.Final Answer:
Deploy new version to green environment, test it, then update load balancer to route all traffic to green -> Option BQuick Check:
Blue-green = test new env, then switch traffic [OK]
- Restarting servers causing downtime
- Delaying traffic switch by manual deletion
- Relying on DNS TTL for instant switch
