Blue-green infrastructure pattern in Terraform - Commands & Configuration
Start learning this pattern below
Jump into concepts and practice - no test required
provider "aws" { region = "us-east-1" } resource "aws_lb" "app_lb" { name = "app-load-balancer" internal = false load_balancer_type = "application" security_groups = ["sg-0123456789abcdef0"] subnets = ["subnet-0123456789abcdef0", "subnet-0fedcba9876543210"] } resource "aws_lb_target_group" "blue" { name = "blue-target-group" port = 80 protocol = "HTTP" vpc_id = "vpc-0123456789abcdef0" } resource "aws_lb_target_group" "green" { name = "green-target-group" port = 80 protocol = "HTTP" vpc_id = "vpc-0123456789abcdef0" } resource "aws_lb_listener" "http_listener" { load_balancer_arn = aws_lb.app_lb.arn port = 80 protocol = "HTTP" default_action { type = "forward" target_group_arn = aws_lb_target_group.blue.arn } } resource "aws_autoscaling_group" "blue_asg" { name = "blue-asg" max_size = 2 min_size = 1 desired_capacity = 1 vpc_zone_identifier = ["subnet-0123456789abcdef0", "subnet-0fedcba9876543210"] launch_configuration = aws_launch_configuration.blue_lc.name target_group_arns = [aws_lb_target_group.blue.arn] health_check_type = "ELB" health_check_grace_period = 300 } resource "aws_autoscaling_group" "green_asg" { name = "green-asg" max_size = 2 min_size = 1 desired_capacity = 0 vpc_zone_identifier = ["subnet-0123456789abcdef0", "subnet-0fedcba9876543210"] launch_configuration = aws_launch_configuration.green_lc.name target_group_arns = [aws_lb_target_group.green.arn] health_check_type = "ELB" health_check_grace_period = 300 } resource "aws_launch_configuration" "blue_lc" { name_prefix = "blue-lc-" image_id = "ami-0c55b159cbfafe1f0" instance_type = "t3.micro" security_groups = ["sg-0123456789abcdef0"] user_data = <<-EOF #!/bin/bash echo 'Blue version running' > /var/www/html/index.html EOF } resource "aws_launch_configuration" "green_lc" { name_prefix = "green-lc-" image_id = "ami-0c55b159cbfafe1f0" instance_type = "t3.micro" security_groups = ["sg-0123456789abcdef0"] user_data = <<-EOF #!/bin/bash echo 'Green version running' > /var/www/html/index.html EOF }
This Terraform file creates two identical environments named blue and green.
The aws_lb resource creates a load balancer that directs traffic.
The aws_lb_target_group resources define groups of servers for blue and green.
The aws_lb_listener listens on port 80 and initially forwards traffic to the blue target group.
The aws_autoscaling_group resources manage the servers for blue and green environments, with blue active and green inactive at start.
The aws_launch_configuration resources define how to launch servers for each environment, with simple user data to show which version is running.
terraform init
terraform apply -auto-approve
-auto-approve - Skip manual approval to apply changes immediatelyaws elbv2 describe-listeners --load-balancer-arn $(terraform output -raw app_lb_arn)
aws elbv2 modify-listener --listener-arn arn:aws:elasticloadbalancing:us-east-1:123456789012:listener/app/app-load-balancer/50dc6c495c0c9188/6d0ecf831eec9f09 --default-actions Type=forward,TargetGroupArn=arn:aws:elasticloadbalancing:us-east-1:123456789012:targetgroup/green-target-group/6d0ecf831eec9f09
terraform apply -auto-approve -target=aws_autoscaling_group.green_asg -var='desired_capacity=1'-target - Apply changes only to the green autoscaling groupIf you remember nothing else from this pattern, remember: keep two identical environments and switch traffic between them to update without downtime.
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
