# minhtran3124/aws-ecs-green-blue-deployment architecture

> Architecture diagram of the GitHub repository minhtran3124/aws-ecs-green-blue-deployment, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-03). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/minhtran3124/aws-ecs-green-blue-deployment
- Repository: https://github.com/minhtran3124/aws-ecs-green-blue-deployment

## Overview

##### 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).

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Developer & Source Control
    subgraph "Developer & Source Control"
        Dev("Developer Workstation"):::developers
        Repo("Source Code Repository (CodeCommit)"):::developers
    end

    %% CI/CD Pipeline
    subgraph "CI/CD Pipeline"
        CP("CodePipeline (Orchestration)"):::cicd
        CB("CodeBuild (Docker Build & Artifact Generation)"):::cicd
    end

    %% Infrastructure Provisioning
    subgraph "Infrastructure Provisioning"
        infraA["cfn-infra\n(Base AWS Resources)"]:::infra
        infraB["cfn-service\n(Service-Specific Resources)"]:::infra
    end

    %% cfn-infra Templates
    subgraph "cfn-infra Templates"
        CF1("CF: codebuild.yml"):::infra
        CF2("CF: codepipeline.yml"):::infra
        CF3("CF: iam.yml"):::infra
        CF4("CF: repository.yml"):::infra
        CF5("CF: s3.yml"):::infra
    end

    %% cfn-service Templates
    subgraph "cfn-service Templates"
        SF1("CF: ecs-cluster.yml"):::infra
        SF2("CF: ecs-service.yml"):::infra
        SF3("CF: loadbalancer.yml"):::infra
        SF4("CF: security-groups.yml"):::infra
        SF5("CF: vpc.yml"):::infra
        SF6("Master: master.yml"):::infra
        SF7("Parameters: parameters.json"):::infra
    end

    %% Deployment Script and Lambda for swap
    Deploy("Deploy Script (deploy.py)"):::infra
    Lambda("Lambda Function (Blue/Green Swap)"):::lambda

    %% Runtime Environment
    subgraph "Runtime Environment"
        ECR("Amazon ECR"):::app
        ECS("ECS Cluster & Service"):::app
        API("API Service Container"):::app
        ALB("Application Load Balancer"):::app
        Blue("Blue Target Group"):::app
        Green("Green Target Group"):::app
    end

    %% Connections
    Dev -->|"pushcode"| Repo
    Repo -->|"triggers"| CP
    CP -->|"initiatebuild"| CB
    CB -->|"buildDockerImage"| ECR
    CB -->|"generateArtifact(build.json)"| Deploy
    Deploy -->|"triggerInfraUpdate"| infraA
    Deploy -->|"triggerInfraUpdate"| infraB
    CP -->|"manualApproval"| Lambda
    Lambda -->|"swaps"| Blue
    Lambda -->|"swaps"| Green
    infraA -->|"provisions"| CF1
    infraA --> CF2
    infraA --> CF3
    infraA --> CF4
    infraA --> CF5
    infraB -->|"provisions"| SF1
    infraB --> SF2
    infraB --> SF3
    infraB --> SF4
    infraB --> SF5
    infraB --> SF6
    infraB --> SF7
    infraB -->|"provisions"| ECS
    infraB -->|"provisions"| ALB
    ALB -->|"routesTo"| Blue
    ALB -->|"routesTo"| Green
    ECS -->|"runs"| API
    infraA -->|"defines"| CP
    infraA -->|"defines"| CB

    %% Click Events
    click infraA "https://github.com/minhtran3124/aws-ecs-green-blue-deployment/tree/main/cfn-infra"
    click CF1 "https://github.com/minhtran3124/aws-ecs-green-blue-deployment/blob/main/cfn-infra/templates/codebuild.yml"
    click CF2 "https://github.com/minhtran3124/aws-ecs-green-blue-deployment/blob/main/cfn-infra/templates/codepipeline.yml"
    click CF3 "https://github.com/minhtran3124/aws-ecs-green-blue-deployment/blob/main/cfn-infra/templates/iam.yml"
    click CF4 "https://github.com/minhtran3124/aws-ecs-green-blue-deployment/blob/main/cfn-infra/templates/repository.yml"
    click CF5 "https://github.com/minhtran3124/aws-ecs-green-blue-deployment/blob/main/cfn-infra/templates/s3.yml"

    click infraB "https://github.com/minhtran3124/aws-ecs-green-blue-deployment/tree/main/cfn-service"
    click SF1 "https://github.com/minhtran3124/aws-ecs-green-blue-deployment/blob/main/cfn-service/templates/ecs-cluster.yml"
    click SF2 "https://github.com/minhtran3124/aws-ecs-green-blue-deployment/blob/main/cfn-service/templates/ecs-service.yml"
    click SF3 "https://github.com/minhtran3124/aws-ecs-green-blue-deployment/blob/main/cfn-service/templates/loadbalancer.yml"
    click SF4 "https://github.com/minhtran3124/aws-ecs-green-blue-deployment/blob/main/cfn-service/templates/security-groups.yml"
    click SF5 "https://github.com/minhtran3124/aws-ecs-green-blue-deployment/blob/main/cfn-service/templates/vpc.yml"
    click SF6 "https://github.com/minhtran3124/aws-ecs-green-blue-deployment/blob/main/cfn-service/master.yml"
    click SF7 "https://github.com/minhtran3124/aws-ecs-green-blue-deployment/blob/main/cfn-service/parameters.json"

    click CB "https://github.com/minhtran3124/aws-ecs-green-blue-deployment/blob/main/cfn-infra/templates/codebuild.yml"
    click CP "https://github.com/minhtran3124/aws-ecs-green-blue-deployment/blob/main/cfn-infra/templates/codepipeline.yml"
    click Deploy "https://github.com/minhtran3124/aws-ecs-green-blue-deployment/blob/main/cfn-service/scripts/deploy.py"
    click Lambda "https://github.com/minhtran3124/aws-ecs-green-blue-deployment/blob/main/cfn-service/scripts/blue_green_flip.py"
    click API "https://github.com/minhtran3124/aws-ecs-green-blue-deployment/tree/main/api"

    %% Styles
    classDef developers fill:#bbdefb,stroke:#0d47a1,stroke-width:2px;
    classDef cicd fill:#c8e6c9,stroke:#2e7d32,stroke-width:2px;
    classDef infra fill:#ffe082,stroke:#ff6f00,stroke-width:2px;
    classDef app fill:#ffccbc,stroke:#d84315,stroke-width:2px;
    classDef lambda fill:#d1c4e9,stroke:#512da8,stroke-width:2px;
```
