Skip to content

Amazon ECS Farget with Blue-Green Deployments by CDK Typescript - Part 2

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

image

Abstract

Table Of Contents

Open Table Of Contents

πŸš€ Solution overview

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

  • The whole AWS resources are created using CDK pipeline except the pipeline itself.

  • The ECS cluster is placed in private subnet as well as the fargate service. We create ECS service with task definition that has desired count of 3 and use FARGATE as requiresCompatibilities

  • The ECS service is registered to ECS deployment controller with the type CODE_DEPLOY for handling blue-green deployment. It sticks the application load balancer to the replacement target group when deploying successfully.

  • A container image is built with codepipeline and codebuild which store images to ECR.

  • Here is the stacks relationship

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

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

  • 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-green deployments

πŸš€ Cleanup

πŸš€ Conclusion

  • Now that you know how to launch tasks into your Amazon ECS cluster using CDK pipeline with the 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: