0
0
Terraformcloud~10 mins

Blue-green infrastructure pattern in Terraform - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Blue-green infrastructure pattern
Deploy Blue Environment
Route Traffic to Blue
Deploy Green Environment
Switch Traffic to Green
Blue Environment Idle
Update or Rollback as Needed
This flow shows deploying two identical environments (blue and green), routing traffic to one while updating the other, then switching traffic to the updated environment.
Execution Sample
Terraform
resource "aws_instance" "blue" {
  ami           = "ami-123456"
  instance_type = "t2.micro"
}

resource "aws_instance" "green" {
  ami           = "ami-123456"
  instance_type = "t2.micro"
}
This Terraform code defines two identical AWS instances representing blue and green environments.
Process Table
StepActionBlue Environment StateGreen Environment StateTraffic Routing
1Deploy Blue EnvironmentRunningNot DeployedTraffic to Blue
2Deploy Green EnvironmentRunningRunningTraffic to Blue
3Switch Traffic to GreenRunningRunningTraffic to Green
4Blue Environment IdleRunning (Idle)Running (Active)Traffic to Green
5Update or Rollback BlueUpdated or Rolled BackRunning (Active)Traffic to Green
6EndStableStableTraffic to Green
💡 Traffic switched to green environment; blue environment is idle and ready for updates or rollback.
Status Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4After Step 5Final
Blue EnvironmentNot DeployedRunningRunningRunningRunning (Idle)Updated or Rolled BackStable
Green EnvironmentNot DeployedNot DeployedRunningRunningRunning (Active)Running (Active)Stable
Traffic RoutingNoneTraffic to BlueTraffic to BlueTraffic to GreenTraffic to GreenTraffic to GreenTraffic to Green
Key Moments - 3 Insights
Why do we keep the old environment running after switching traffic?
Keeping the old environment running (see Step 4 in execution_table) allows quick rollback if the new environment has issues.
When is the green environment deployed?
The green environment is deployed after the blue is running (Step 2), so we have two identical environments before switching traffic.
How does traffic routing change during deployment?
Traffic is initially routed to blue (Steps 1 and 2), then switched to green (Step 3) once green is ready.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, at which step does traffic switch to the green environment?
AStep 4
BStep 2
CStep 3
DStep 5
💡 Hint
Check the 'Traffic Routing' column in execution_table rows.
According to variable_tracker, what is the state of the blue environment after Step 5?
ANot Deployed
BUpdated or Rolled Back
CRunning (Active)
DTerminated
💡 Hint
Look at the 'Blue Environment' row under 'After Step 5' in variable_tracker.
If traffic was switched to green before deploying it, what would happen in the execution_table?
ATraffic would route to green while green is not running
BBlue environment would be idle earlier
CGreen environment would be updated first
DTraffic would remain on blue
💡 Hint
Consider the 'Traffic Routing' and environment states in execution_table.
Concept Snapshot
Blue-green pattern deploys two identical environments.
Traffic routes to one (blue) while the other (green) is updated.
Switch traffic to green when ready.
Old environment stays idle for quick rollback.
Ensures zero downtime and safer updates.
Full Transcript
The blue-green infrastructure pattern involves deploying two identical environments called blue and green. Initially, traffic is routed to the blue environment while the green environment is deployed and updated. Once the green environment is ready, traffic switches to it, and the blue environment becomes idle but remains running. This setup allows quick rollback if needed by switching traffic back to blue. The pattern ensures zero downtime and safer updates by having two environments ready and switching traffic between them.