Hands-on Amazon ECS for Blue-Green Deployments With CDK Typescript - Part 1
Abstract
- This blog post supports you in hands-on AWS ECS by building the Blue-Green Deployments With CDK Typescript. Instead of using AWS console to create all necessary resources, you will create them through CDK code and automate deployment with CDK pipeline for both project application and infrastructure as code.
Table Of Contents
๐ Solution overview
The whole AWS resources are created using CDK pipleine except the pipeline itself.
The ECS cluster is placed in a private subnet with EC2 instances which is managed by the autoscaling group. We create two services which are Blue and Green, there should be only one service that has a desired count > 0 and the other is 0 at the time so that the application load balancer always forwards the request to one of them.
A container image is built with codepipeline and codebuild which store images to ECR.
๐ Source code structure
We have two Git repositories (codecommit) one for application project
app-project
directory and others for CDK infrastructurecdk-infra
directoryโ ecs-blue-green-deployments tree -L 1 . โโโ README.md โโโ app-project โโโ cdk-infra โโโ images 3 directories, 1 file
We create the codecommit repositories through CDK
Go to
cdk-infra
and runcdk ls
cdk ls simflexcloud-ecs-blue-green-deployments-pipeline simflexcloud-ecs-blue-green-deployments-pipeline/master-sin/EcsBlueGreenDeploymentsStack simflexcloud-ecs-blue-green-deployments-pipeline/master-sin/simflexcloud-ecs-blue-green-deployments-build-image
Deploy
simflexcloud-ecs-blue-green-deployments-pipeline
it will create the repository ofcdk-infra
. Note: replaceCDK_DEFAULT_ACCOUNT
andCDK_DEFAULT_REGION
incdk-infra/src/shared/constants.ts
with expected ones.cdk deploy simflexcloud-ecs-blue-green-deployments-pipeline
Add the remote Git repository to
cdk-infra
(Note: Replace thepriv-acc
with yours)git remote add origin ssh://priv-acc/v1/repos/ecs-blue-green-deployments-infra
Create branch
master
and push source code to the repo, it will trigger CDK pipeline to create all stacks which also include the repository and pipeline forapp-proj
After the pipeline completed successfully, go to
app-proj
directory and add Git remote repository, then create the branchestestgreen
andtestblue
and push them to codecommitgit remote add origin ssh://priv-acc/v1/repos/simflexcloud-ecs-blue-green-deployments
๐ Process flow
1. Build project
Use AWS CodeBuild to create Docker images and store them in Amazon ECR. This process is powered by codepipeline to handle CICD. We need to build two image tags which are
testgreen
based on branchtestgreen
andtestblue
based on branchtestblue
. Any commits from these branches will trigger pipelines to execute build projects based on thebuildspec.yml
andDockerfile
.
2. Create ECS cluster
Create an Amazon ECS cluster using EC2 as container instance. The EC2 instance is attached an IAM role which includes
AmazonEC2ContainerServiceforEC2Role
policy for the ECS agent to connect to ECS cluster.Task definitions are required to run Docker containers in Amazon ECS. They tell the services which Docker images to use for the container instances, what kind of resources to allocate, network specifics, and other details. We create two task definitions which are Blue and Green. In the task, we define image tag, task size, container port, Task execution role, etc.
With ECS, we can easily deploy and manage containerized applications at scale, while benefiting from features such as automatic scaling, load balancing, and automatic service discovery. It resembles Auto Scaling in that it keeps a specified number of instances, but unlike Auto Scaling, it doesn't adjust the number of instances in response to CloudWatch alarms or other Auto Scaling mechanisms. By utilizing a load balancer, it is possible to maintain a specified amount of resources while ensuring a singular application reference point. As such, we generate two distinct services, one for the blue application and the other for the green application. Only one service is active (has desire count greater than zero) at a time.
Due to desired tasks 2, the service creates 2 containers on the EC2 instance and expose the public port for ALB target group, mapping to container port 8081.
3. Test the blue green deployments
Test the blue service by calling the
/api
request with ALB DNSNow we switch deployment to green service by updating
desiredCount
fromblue
to 0 and fromgreen
to 2 then deploy.We see targetgroup add new target port and draining the old one
๐ Cleanup
To cleanup all resoures in this project, we first need to delete the ECR image as they were not created by CDK and prevent CDK to destroy the ECR repository.
Go to cloudformation and delete stacks.
๐ Conclusion
Now that you know how to launch tasks into your Amazon ECS cluster using CDK pipeline
The approach of a blue-green deployment involves utilizing two identical production environments as a means of reducing downtime. Various cutover strategies may be employed, but typically only one of the environments should be actively serving production traffic.
Subscribe to my newsletter
Read articles from Vu Dao directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Vu Dao
Vu Dao
๐ AWSome Devops | AWS Community Builder | AWS SA || โ๏ธ CloudOpz โ๏ธ