Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Blue-green infrastructure pattern
📖 Scenario: You are managing a web application deployment on the cloud. To avoid downtime during updates, you want to use the blue-green deployment pattern. This means you have two identical environments: blue and green. Only one environment serves live traffic at a time. When you update, you deploy to the inactive environment, then switch traffic to it.
🎯 Goal: Create a Terraform configuration that defines two identical infrastructure environments named blue and green. Then add a variable to select which environment is active. Finally, configure a load balancer resource that routes traffic to the active environment only.
📋 What You'll Learn
Define two identical infrastructure resources named blue and green.
Add a variable called active_environment with allowed values "blue" and "green".
Use the active_environment variable to select which environment the load balancer routes traffic to.
The Terraform configuration must be valid and deployable.
💡 Why This Matters
🌍 Real World
Blue-green deployment is a common pattern to update applications without downtime by switching traffic between two identical environments.
💼 Career
Cloud engineers and DevOps professionals use this pattern to ensure smooth, reliable application updates in production.
Progress0 / 4 steps
1
Define blue and green infrastructure resources
Create two identical aws_instance resources named blue and green. Use the AMI ID "ami-12345678" and instance type "t2.micro" for both.
Terraform
Hint
Define two aws_instance blocks with names blue and green. Use the same AMI and instance type for both.
2
Add active environment variable
Add a Terraform variable called active_environment with type string and allowed values ["blue", "green"]. Set the default value to "blue".
Terraform
Hint
Use a variable block with type = string, default = "blue", and a validation block to restrict values.
3
Select active instance using the variable
Create a local value called active_instance_id that uses a conditional expression to select aws_instance.blue.id if var.active_environment is "blue", otherwise aws_instance.green.id.
Terraform
Hint
Use a locals block with a conditional expression to pick the active instance ID.
4
Configure load balancer to route to active environment
Create an aws_lb_target_group_attachment resource named active_attachment that attaches the load balancer target group aws_lb_target_group.main.id to the instance ID from local.active_instance_id.
Terraform
Hint
Use aws_lb_target_group_attachment with target_group_arn, target_id, and port set to 80.
Practice
(1/5)
1. What is the main purpose of the blue-green infrastructure pattern in Terraform deployments?
easy
A. To reduce infrastructure costs by using a single environment
B. To automate database backups during deployment
C. To increase the number of servers in a single environment
D. To avoid downtime by switching traffic between two identical environments
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 D
Quick Check:
Blue-green pattern = avoid downtime [OK]
Hint: Remember: blue-green means two environments for zero downtime [OK]
Common Mistakes:
Thinking it reduces costs by using one environment
Confusing it with scaling servers in one environment
Assuming it automates backups
2. Which Terraform resource is commonly used to switch traffic between blue and green environments in a blue-green deployment?
easy
A. aws_lb_listener_rule
B. aws_instance
C. aws_s3_bucket
D. aws_security_group
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
The aws_lb_listener_rule resource allows switching traffic between blue and green target groups.
Final Answer:
aws_lb_listener_rule -> Option A
Quick Check:
Traffic switch uses listener rules [OK]
Hint: Traffic routing uses listener rules, not instances or buckets [OK]
Common Mistakes:
Choosing aws_instance which manages servers, not traffic
Selecting aws_s3_bucket which is for storage
Picking aws_security_group which controls firewall rules
3. Given this Terraform snippet for blue-green deployment traffic switching: