Skip to content

Hands-on Amazon ECS for Blue-Green Deployments With CDK Typescript - Part 1

Posted on:May 10, 2023Β atΒ 03:57 PM

image

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

Open Table Of Contents

πŸš€ Solution overview

<img src=https://raw.githubusercontent.com/vumdao/cdk-ecs-blue-green-deployments/master/images/architect.png width=1100>

  • 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 infrastructure cdk-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 run cdk 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 of cdk-infra. Note: replace CDK_DEFAULT_ACCOUNT and CDK_DEFAULT_REGION in cdk-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 the priv-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 for app-proj

    • After the pipeline completed successfully, go to app-proj directory and add Git remote repository, then create the branches testgreen and testblue and push them to codecommit

      git remote add origin ssh://priv-acc/v1/repos/simflexcloud-ecs-blue-green-deployments

πŸš€ Process flow

1. Build project

2. Create ECS cluster

3. Test the blue green deployments

πŸš€ Cleanup

πŸš€ 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.

:stars: Blog Β· Github Β· stackoverflow Β· Linkedin Β· Group Β· Page Β· Twitter :stars:


{% user vumdao %}

{% github vumdao/vumdao no-readme %}