- Continue the previsous post Hands-on Amazon ECS for Blue-Green Deployments With CDK Typescript which uses EC2 to host the ECS container service and manually operate blue-green deployments. In this blog post, I use AWS fargate as container of ECS service and codedeploy to operate blue-green deployment automatically.
-
The whole AWS resources are created using CDK pipleine except the pipeline itself.
-
The ECS cluster is placed in private subnet as well as the fargate service. We create ECS service with task defination has desired count 3 and use FARGATE as
requiresCompatibilities
-
The ECS service is registered to ECS deployment controller with type
CODE_DEPLOY
for handling blue-green deployment. It sticks the application load balancer to replacement target group when deploying successfully. -
Container image is built with codepipeline and codebuild which store images to ECR.
-
Here is the stacks relationship
-
We have two Git repositories (codecommit) one for application project
app-project
directory and other 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
-
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
-
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.
2. Create ECS cluster
3. Application load balancer
4. CodeDeploy application and deployment group
-
Test the blue service by loading ALB DNS
-
Now we change the color to red in
app-proj/index.html
and push the commit to CodeCommit. It triggers the pipeline to build and then deploy new change -
The deploy stage creates codedeploy deployment ID to perform deployment process and handle Traffic shifting progress strategy with rule
LINEAR_10PERCENT_EVERY_1MINUTES
(CodeDeploy predefined deployment configuration that shifts 10 percent of traffic every minute until all traffic is shifted) -
ECS run new tasks with new image version on the ECS service
-
After the new tasks are in healthy state, the deployment starts rerouting production traffic to replacement task set gradually follow the rule
LINEAR_10PERCENT_EVERY_1MINUTES
-
Use port 8080 for testing and compare with current version
-
Complete the replacement and start terminating the original task set
-
ECS remove the tasks with old revision
-
The final result
-
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.
- Now that you know how to launch tasks into your Amazon ECS cluster using CDK pipeline with required type EC2 or Fargate.
- 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.
References: