Deploying a new version of an application to a production environment is the most critical task in the software development lifecycle. And a small mistake in the release cycle leads to cascading failures and outages.

To achieve the deployment of a new version of the application, is there an option that reduces the chances of failure or outages as well as ensures zero downtime during deployment?

Yes, the solution to this problem is the blue-green strategy.

A blue/green is the type of deployment in which it creates two identical environments but with different versions of applications running on it.
The blue environment is where the current version is running while the green environment is where the new version of the application will run.

In this blog, I have created a pipeline on AWS which uses blue/green deployment strategy to deploy a new version of the application.

Architecture Diagram:

Advantages:

1. Rapid Releasing: With blue-green deployment, one can release an application in a production environment as there is no downtime associated with it and it has no negative impact on users.
2. Fast Rollbacks: Easy rollbacks are achieved with this approach between the current and new versions of the application, which in turn reduces the risks inherent in experimenting in a production environment
3. Built-in Disaster Recovery: As the blue-green environment uses two identical environments, so the dual production environment is its own hot backup

 

Creation of roles for different services:

We need to create 3 roles to allow services to function.
a) CodeDeploy Role:
This role will be used while creating the Deploy stage while creating CodePipeline.

Go to “IAM” click on roles.
Create role, select use case as “CodeDeploy”
Give name as “bluegreencodedeploy” and create a role.
Likewise create role for CodeDeploy – ECS and give name asbluegreenECS”.
Also create role for CodeBuild Role and give name asbluegreencodebuild”.

Creation of Elastic Container Registry (ECR):

We need to create an ECR to store Docker images.

Go to “ECR” service and create repository.
Give name as “bluegreenrepo” and create repository.
Initially, we have to manually build the image from the Dockerfile for that Launch one EC2 instance and install Git and Docker on it.
Clone the github repository and create the image.
Run the below commands shown in an image on an EC2 instance to push the image to ECR.

 

Creation of Target groups and Load balancer:

Follow the below steps to create target groups and a load balancer.

Go to “EC2” service, on left side search for “Target Groups” option and open it.
Click on create “target group”
Choose a target type: select “IP addresses”
Give name as “ALB-Tgroup1” and specify port as 8080.
Leave the rest of the settings default and create a target group.

Following the above steps, create a second target group named ALB-Tgroup2 and port 8080.

With the below steps, create an application load balancer.

Go to “EC2” service, search for “Load balancers” option and open it.
Click on “create load balancer”, select load balancer type as “Application load balancer”.
Give name as “ALB-app” and scheme type as “internet-facing”.
Network mappings: select default VPC and select all the subnets.
The security group should have port 8080 open.

 

We cannot add 2 target groups at a time while creating a load balancer. We will have to add another target group once our load balancer is created.

Once Load balancer is created click on it.
Go to “Listeners & routings”
Specify port as 8080 and action as “forward” and add another target group.

 

Creation of ECS Cluster, task and service:

Create an ECS cluster, task, and service by following the below steps.

Go to “ECS” service, on left side click on “Clusters” and create cluster.
Select cluster template as “Networking only”.
Give name as “Bluegreencluster”.
Leave the rest of the settings as default and create a cluster.

 

 

 

On the left side, search for an option “Task defination” and create new task definition.
Select launch type compatibility as “Fargate”.
Give name as “bluegreen-taskdef”.
Select task role as “ecsTaskExecutionRole”.
Specify task memory as 0.5 Gb and task CPU as 0.25 vcpu.

Now, click on “Add Container” and give name as “bluegreencontainer”.
Mentioned the image url, which can be found in the ECR console.
Specify memory limit as 128 and port mapping as 8080.

 

 

Once task definition is created, click on task definition and then click on service.
Click on “Create service”, select launch type as “fargate”.
Give service name as “bluegreen-service” and number of tasks as 2.
Select deployment type as “Blue/green deployment”.
Select service role for codedeploy as “bluegreenECS” (2nd role which we created in step 1).
Select cluster vpc as “default vpc” and select all the subnets.
Select load balancer type as “Application load balancer” and specify port as 80 and test port as 8080.
Select both target groups.

 

Once the container is created, copy the public IP of each task and test it in the browser.
It should host your version v1.1 and test the load balancer DNS endpoint to see if it is working or not.

 

Creation of CodeBuild project:

We have to create a codebuild project that will fetch a Dockerfile from GitHub and generate an image from it.

Go to “CodeBuild” service and create codebuild project.
Give name as “bluegreen-Codebuild”.
Specify git url as “https://github.com/Vijaygawate/Blue-Green-Deployment-On-ECS.git”.
Select operating system as “Ubuntu”, enables privileged checkbox.
Select service role as “bluegreencodebuild” (3rd role which we created in step 1)
Uncheck cloudwatch logs section.
Leave the rest of the settings as default and create.

 

 

Creation of CodePipeline:

Finally, we have to combine CodeBuild and CodeBuild and CodeDeploy by creating CodePipeline.

Go to “CodePipeline” and create pipeline.
Give pipeline name as “bluegreenpipeline” and service role: new service role.

 

Select source as Github version1 in source stage and click n connect to Github
Select change detection options as GitHub Webhooks.

 

Add build provider as “CodeBuild” .
Select project name as “bluegreen-codebuild” and click on next step.

 

 

Select Deploy provider as “Amazon ECS (Blue/Green)”.
Select the CodeDeploy application name and deployment group.
Amazon ECS task definition: BuildArtifact
AWS CodeDeploy AppSpec file: BuildArtifact
Placeholder text in the task definition as “ImageURI”.
Review and create pipeline.

 

Once the pipeline is triggered, we will be able to see the below page.

Now, if you make some changes to the server.js. It would start a new pipeline that would create a new image from a Dockerfile and push it to ECR, as well as add two new tasks in the ECS cluster.

Both blue and green environments are available, and we can decide which we would like to keep running.
If we want to keep version 2 running, then we can simply tap on the “Terminate” tab so that it will keep version 2 running while terminating the previous version’s resources.

And let’s say our version 2 is not performing as expected and would like to roll back to the previous version.
For that, just tap on “Stop and rollback deployment” and it will roll back to version 1 while terminating version 2 resources.

 

.

Conclusion:

By this way, we can achieve zero downtime while switching to different versions of applications, and this type of strategy plays a vital role in the deployment of critical applications.