Startec

Startec

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

Mai 17, às 15:25

·

5 min de leitura

·

1 leituras

Abstract Continue the previous 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...
Amazon ECS Farget with Blue-Green Deployments by CDK Typescript - Part 2

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

Abstract

Table Of Contents


🚀 Solution overview

  • 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

🚀 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

Enter fullscreen mode Exit fullscreen mode

  • 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

  • Create an Amazon ECS cluster using fargate.

3. Application load balancer

  • We have two rules:

    • Port 80: the main rule

    • Port 8080: testing rule

  • The ALB is currently stuck to the target group of green

4. CodeDeploy application and deployment group

  • A CodeDeploy deployment group that orchestrates ECS blue-green deployments.

🚀 Test the blue-green deployments

  • 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 a new change

  • The deploy stage creates codedeploy deployment ID to perform the deployment process and handle the 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 following 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

🚀 Cleanup

  • To cleanup all resources 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 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:



Continue lendo

Tech Crunch

Deal Dive: Why this startup chose to sell itself over raising a Series A
Heroes Jobs was recently acquired after realizing its company was better off not alone Not all startups are built for a billion-dollar exit — or to grow as a stand-alone company at all. The...

Hoje, às 16:00

DEV

Mobile App ideas
What interesting, useful and simple mobile apps can be made? What do you think? Maybe there are problems that can be solved with this app? Share your ideas. Of course, todo list and weather apps are a great...

Hoje, às 14:51

AI | TechCrunch

This week in AI: AI heavyweights try to tip the regulatory scales
Keeping up with an industry as fast-moving as AI is a tall order. So until an AI can do it for you, here’s a handy roundup of the last week’s stories in the world of machine learning, along with notable...

Hoje, às 14:35

DEV

The Evolution and Impact of MLOps: understanding MLOps
Machine Learning Operations, or MLOps, is the missing bridge between machine learning, data science, and data engineering. It has emerged as the link that unifies these functions more seamlessly than ever...

Hoje, às 14:00

HackerNoon

279 Stories To Learn About Programming | HackerNoon
May 27th 2023 New Story51m by @learn Too Long; Didn't ReadPeople Mentionedprogramming#programming#learn#[email protected] LearnReceive Stories from @learnGet free API security automated scan in...

Hoje, às 13:56

HackerNoon

162 Stories To Learn About Programming Languages | HackerNoon
May 27th 2023 New Story25m by @learn Too Long; Didn't ReadPeople Mentionedprogramming#programming-languages#[email protected] LearnReceive Stories from @learnGet free API security automated scan in...

Hoje, às 13:56

HackerNoon

117 Stories To Learn About Programming Tips | HackerNoon
May 27th 2023 New Story17m by @learn Too Long; Didn't ReadPeople Mentionedscience#programming-tips#learn#[email protected] LearnReceive Stories from @learnGet free API security automated scan in...

Hoje, às 13:56

DEV

How to use Mapbox in Next.js
Table of content Mapbox Setting up the Mapbox key Next.js Dependencies Adding key to Next.js configuration Initializing the map Adding styles Adding the map controls Loading the data Adding the...

Hoje, às 13:15

DEV

Add MUI v5 Theme Switcher in NextJs/React
Hello guys today i will show you how to add MUI Theme Switcher in your React App. Prerequisite of this tutorial is to have intermediate knowledge of React and MUI (formally known as material-UI) Lets...

Hoje, às 13:03

Showmetech

50 filmes que todo empreendedor deve assistir
Índice Fyre Festival – Fiasco no Caribe (2019)Como Ser Warren Buffett (2017)Fome de Poder (2016)Cães de Guerra (2016)Generation Startup (2016)Steve Jobs (2015)Joy: O Nome do Sucesso (2015)A Grande Aposta...

Hoje, às 13:00