CI/CD Pipeline

Review CI/CD CodePipeline

Select Services and go to CodePipeline under Developer Tools. Identify the Pipeline created for ECS Blue-green environments. Review the stages in the pipeline and notice the approval stage.

Below is a screenshot of CodePipeline once all CloudFormation templates are completed. You may check that by selecting Services -> CodePipeline -> Select the latest created pipeline.

codepipeline

Here is further explanation for each stages of Code Pipeline.

Build stage

  • During first phase, CodeBuild builds the docker container image and pushes to Amazon ECR.

  • During second phase, Codebuild executes scripts/deployer.py which executes the following scripted logic

    1. Retrieve artifact (build.json) from the previous phase (CodeBuild phase, which builds application container images)
    2. Check if the load balancer exists. Name of the ELB is fed through environment variable by the pipeline.
    3. Get tag key value of the target group, running on port 8080 and 80 with KeyName as “Identifier”. It will be either “Code1” or “Code2”
    4. Get Sha of the image id running on target group at port 8080 and 80
    5. Edit the build.json retrieved from step-1 and append the values retrieved in step3 and step4
    6. Save the modified build.json. This file is the output from codebuild project and fed as an input to the CloudFormation execution stage.This json file has the following schema { “Code1” : “CONTAINER_TAG1”, “Code2” : “CONTAINER_TAG2” } If the load balancer does not exists (as found in step-2), this would imply that the stack is executed for the first time, and the values of “CONTAINER_TAG1” and CONTAINER_TAG2” will be the same and default to the value retrieved from build.json in step-1

Deploy stage

CodePipeline executes templates/ecs-cluster.yaml. The CloudFormation input parameters with KeyName as “Code1” and “Code2” are overwritten with the values as written in the build.json, retrieved from the second phase of Build Stage.

Review/Approval stage

The pipeline offers manual “Review” button so that the approver can review code and Approve new release. Providing approvals at this stage will trigger the Lambda function (blue_green_flip.py) which swaps the Green Target Group to Live traffic. You can checkout sample app to see new release change. blue_green_flip.py has the following logic scripted

  1. Read Job Data from input json
  2. Read Job ID from input json
  3. Get parameters from input json
  4. Get Load balancer name from parameters
  5. Identify the TargetGroup running on this Load Balancer at port 80 and port 8080. Perform the TargetGroup Swap. Also swap the values of “IsProduction” tags.
  6. Send success or failure to CodePipeline