Deploy Blue-Green Environments with Terraform

Deploying new versions of an application without causing downtime can feel like walking a tightrope, but it doesn’t have to be. Blue-green deployment is a smart way to release updates while keeping your application running smoothly. The idea is simple, you have two environments: blue (current version) and green (new version). By controlling traffic between them, you can test changes safely and switch over seamlessly when everything looks good.

With Terraform, this process becomes even easier. It lets you define, manage, and apply your infrastructure updates in a way that’s consistent and reliable.

In this tutorial, you’ll learn how to use Terraform and AWS’s Application Load Balancers for canary tests and blue/green deployments. Also, you’ll learn how to add feature flags to your Terraform configuration by using variables and conditionals. 

 

First thing first, let’s define what we need in this  lab.

Prerequisites:

  • Familiarity with Terraform basics.
  • Terraform 1.3+ installed locally.
  • An active AWS account.

 

Step 1 – Provision networking resources (VPC, security groups, ALB) and deploy the blue environment

  • Create a folder
  • Create 5 files:
    • main.tf: Defines the VPC, security groups, and ALB.
  • variables.tf: Manages deployment settings like instance count and environment toggles.
  • blue.tf: Configures the blue environment with two web servers and a target group.
  • terraform.tf: defines the terraform block, which specifies the Terraform binary and AWS provider versions.
  • Initialize your configuration files with; terraform init

Cheat Sheet: When initialize my configuration file, I received an error message.

This error message indicated that the terraform version I am using is not supported. 

I added “, <= 1.7.0” to correct error.

  • Next, run terraform plan. Then, apply your configuration. Respond yes to the prompt to confirm the operation.
  • All the resources have been created in AWS. 

Step 2 – Deploy the green environment alongside the blue environment

  • Verify your blue environment by visiting the load balancer’s DNS name in your browser or cURLing it from your terminal.

  • Notice that the load balancer evenly distributes traffic between the two instances in the blue environment.

  • Add the green.tf configuration and update variables.tf with green environment settings.
  • Apply the changes to provision the green environment.

 

Notice how this configuration is similar to the blue application, except that the web servers return green #${count.index}.

Add the following variables to variables.tf.

  • Apply your configuration to deploy your green application. Remember to confirm your apply with a yes

Step 3 – Conduct canary tests and incrementally promote the green environment

  • Use Terraform feature toggles to direct 10% of traffic to the green environment.

Even though you deployed your green environment, the load balancer does not yet route traffic to it.

Modify the  aws_lb_listener.app’s default_action block in main.tf to match the following. The configuration uses lookup to set the target groups’ weight. Notice that the configuration defaults to directing all traffic to the blue environment if no value is set.

  • Begin canary test

  • Apply your configuration with the traffic_distribution variable set to blue-90 to run a canary test. Remember to confirm your apply with a yes.

  • Verify canary deployment traffic
  • Verify that your load balancer now routes 10% of the traffic to the green environment.

for i in `seq 1 10`; do curl $(terraform output -raw lb_dns_name); done

  • Notice that the load balancer now routes 10% of the traffic to the green environment.

 

  • Increase traffic to green environment
  • Now that the canary deployment was successful, increase the traffic to the green environment.
  • Apply your configuration with the traffic_distribution variable set to split to increase traffic to the green environment. Remember to confirm your apply with a yes.

  • Verify rolling deployment traffic
  • Verify that your load balancer now splits the traffic to the blue and green environments.
  • Notice that the load balancer now evenly splits the traffic between the blue and green environments.

for i in `seq 1 10`; do curl $(terraform output -raw lb_dns_name); done

 

  • Promote green environment
  • Since both the canary and rolling deployments succeeded, route 100% of the load balancer’s traffic to the green environment to promote it.
  • Apply your configuration to promote the green environment by setting the traffic_distribution variable to green. Remember to confirm your apply with a yes.
  • Verify load balancer traffic
  • Verify that your load balancer now routes all traffic to the green environment. Run:

for i in `seq 1 5`; do curl $(terraform output -raw lb_dns_name); done

  • Using this deployment strategy, you successfully promoted your green environment with near-zero downtime.
  • Scale down blue environment: After verifying that your load balancer directs all traffic to your green environment, it is safe to disable the blue environment.
  • Apply your configuration to destroy the blue environment resources by setting the traffic_distribution variable to green and enable_blue_env to false. Remember to confirm your apply with a yes.
  • Deploy new version
  • You deployed the application’s Version 1.0 in the blue environment, and the new version, 1.1, in the green environment. When you promoted the green environment, it became the current production environment.
  • Deploy the next release to the blue environment, which minimizes modifications to your existing configuration by alternating the blue and green environments.

Modify the aws_instance.blue’s user_data and tags blocks in blue.tf to display a new version number, 1.2.

  • Enable new version environment
  • Apply your configuration to provision the new version of your infrastructure. Remember to confirm your apply with a yes. Set the traffic_distribution variable to green to continue directly all traffic to your current production deployment in the green environment

terraform apply -var ‘traffic_distribution=green’

 

  • Start shifting traffic to blue environment
  • Apply your configuration to run a canary test to the blue environment by setting the traffic_distribution variable to green-90. Remember to confirm your apply with a yes.
  • Once the apply completes, verify that your load balancer routes traffic to both environments.
  • Run for i in `seq 1 10`; do curl $(terraform output -raw lb_dns_name); done
  • Promote blue environment: Now that the canary deployment is successful, fully promote your blue environment.
  • Apply your configuration to promote the blue environment by setting the traffic_distribution variable to blue. Remember to confirm your apply with a yes.
  • Verify that your load balancer now routes all traffic to the blue environment.

for i in `seq 1 5`; do curl $(terraform output -raw lb_dns_name); done

 

Congrats! By leveraging AWS ALB and Terraform, you have implemented efficient and reliable blue-green and canary deployments, minimizing downtime and ensuring smooth application updates.

 

NOTE: Do not forget to Clean up your infrastructure

Destroy the resources you provisioned. Remember to respond to the confirmation prompt with yes.

Leave a Reply

Your email address will not be published. Required fields are marked *