How minhtran3124/aws-ecs-green-blue-deployment works
Main Components
• Infrastructure Repositories:
• cfn-infra: Contains CloudFormation templates, CodeBuild/CodePipeline scripts, and configurations for establishing foundational AWS infrastructure (IAM policies, CodeBuild, CodePipeline, notification services, etc.).
• cfn-service: Provides CloudFormation templates and scripts for creating the service-specific AWS resources – setting up VPC, ECS Cluster/Service, load balancers, target groups, and parameters required for blue/green deployment.
• Application Source:
• api: Holds the source code for the API service which will run as a container within the ECS Cluster.
• CI/CD Pipeline Components:
• CodeCommit: (Mentioned in the README) for source code management after cloning.
• CodeBuild: For validating CloudFormation templates and building Docker images.
• CodePipeline: For orchestrating the deployment process, including manual approval (review stage) and triggering CloudFormation stack updates.
• AWS Lambda: As a step in the review stage that swaps the target groups between blue and green deployments.
• AWS Resources:
• CloudFormation Stacks: For infrastructure creation.
• Amazon ECR: For storing Docker container images.
• Target Groups and Application Load Balancer: Facilitate traffic routing and the blue/green swap.
• SNS, S3, IAM, and other supporting services.
Relationships and Interactions
• Developer Workflow:
• Developers push code changes (for both API and infrastructure modifications) to their respective repositories.
• A pull request is automatically triggered that starts the CodeBuild validation process for CloudFormation templates.
• Upon validation, the process continues to CodePipeline, which manages both build (creating the Docker image and updating deployment parameters) and deployment phases.
• A manual approval (review stage) is required where an admin reviews the changes.
• CI/CD Flow:
• Build Stage: CodeBuild builds and pushes the Docker image (documenting container tags) then runs a deploy script (deploy.py) to prepare the artifact (build.json).
• Deploy Stage: CloudFormation templates (e.g., ecs-cluster.yml, ecs-service.yml) are executed with parameters from the build artifact.
• Review Stage: A manual “Review” action triggers a Lambda function that swaps target groups (blue/green) – enabling traffic cut-over.
• Infrastructure Provisioning:
• cfn-infra sets up foundational AWS resources like IAM, CodeBuild/CodePipeline configurations, and notifications.
• cfn-service sets up the actual environment where the API service runs (VPC, ECS Cluster/Service, Load Balancer, Security Groups).
Architectural Patterns and Design Principles
• Continuous Delivery and Automated Deployment:
• Infrastructure as Code:
• Role of CloudFormation templates in provisioning both base and service-specific resources.
• Modular Separation:
• Blue/Green Deployment:
• How two versions (blue and green) of the target groups are maintained and swapped using a Lambda function after admin approval.
• Developer & Source Code Management:
• Developer workstations pushing code to Git (CodeCommit) for both the API service and infrastructure repositories.
• CI/CD Pipeline:
• CodeBuild Stage 1: Docker image build and push to ECR.
• CodeBuild Stage 2: Script execution (deploy.py) updating build.json.
• CodePipeline: Orchestration expressing build, deploy, and manual review stages.
• CloudFormation Infrastructure:
• cfn-infra: IAM, CodeBuild/CodePipeline definitions.
• cfn-service: VPC, ECS Cluster, ECS Service, load balancer, target groups.
• Runtime Environment:
• API container hosted on ECS.
• Application Load Balancer routing traffic via blue and green target groups.
• Lambda function for swapping (triggered upon manual approval).