CI/CD Pipeline

🎯 Mục tiêu Task 11: Thiết lập pipeline CI/CD cho backend NestJS monolithic: GitHub → Build → ECR → Deploy ECS

🔐 Security Best Practice: Trong production environment, nên tạo custom policies với principle of least privilege thay vì sử dụng managed policies có quyền rộng như FullAccess

📋 Prerequisites: Cần hoàn thành Task 8 (ECR), Task 9 (ECS Fargate) trước khi bắt đầu CI/CD setup.


1. Tạo S3 Artifact Bucket

1.1. Tạo S3 Bucket

AWS Console → S3:

  1. Click “Tạo bucket”
    • Tên bucket: vinashoes-artifacts-prod
    • Region: Asia Pacific (Singapore)

📝 CLI Alternative:

# Tạo S3 bucket bằng AWS CLI
aws s3api create-bucket \
    --bucket vinashoes-artifacts-prod \
    --region ap-southeast-1 \
    --create-bucket-configuration LocationConstraint=ap-southeast-1

# Enable versioning
aws s3api put-bucket-versioning \
    --bucket vinashoes-artifacts-prod \
    --versioning-configuration Status=Enabled
  1. Block Public Access:
    • ☑️ Block all public access (khuyến nghị)
    • ☑️ Enable Bucket Versioning
  1. Server-side Encryption:
    • ☑️ Amazon S3 managed keys (SSE-S3)

🔐 Lưu ý Bảo mật:

  • SSE-S3: Amazon quản lý encryption keys
  • SSE-KMS: Bạn có thể control access và audit key usage
  • SSE-C: Bạn provide và manage encryption keys
  • Cho CI/CD artifacts, SSE-S3 đủ an toàn và cost-effective

📝 CLI Reference:

# Set bucket encryption
aws s3api put-bucket-encryption \
    --bucket vinashoes-artifacts-prod \
    --server-side-encryption-configuration '{
        "Rules": [{
            "ApplyServerSideEncryptionByDefault": {
                "SSEAlgorithm": "AES256"
            }
        }]
    }'

# Verify bucket settings
aws s3api get-bucket-location --bucket vinashoes-artifacts-prod
aws s3api get-bucket-versioning --bucket vinashoes-artifacts-prod

💡 S3 Optimization: Enable S3 Intelligent Tiering để tự động optimize storage costs cho artifacts.


2. Tạo IAM Roles

2.1. CodeBuild Service Role

AWS Console → IAM → Roles:

  1. Tạo Role:
    • Vào IAM Console → Roles → Click “Tạo role”
    • Loại entity tin cậy: ☑️ AWS service

🎯 Mục đích IAM Role:

  • CodeBuild Role: Cần quyền pull code, build Docker, push ECR, write logs
  • CodePipeline Role: Cần quyền orchestrate các stages, trigger builds, deploy services
  • Principle of Least Privilege: Chỉ cấp permissions cần thiết cho từng service
  1. Chọn Service:
    • Use case: Tìm và chọn CodeBuild
    • Description: “Allows CodeBuild to call AWS services on your behalf”
    • Click “Next”
  1. Thêm Permissions:
    • Search và attach các policies:
    • ☑️ AWSCodeBuildDeveloperAccess
    • ☑️ AmazonEC2ContainerRegistryPowerUser
    • ☑️ AmazonS3FullAccess
    • Click “Next”
  1. Đặt tên, Xem lại và Tạo:
    • Tên role: VinaShoesCodeBuildRole
    • Mô tả: Role for CodeBuild to build Docker images and push to ECR
    • Click “Tạo role”

📝 CLI Alternative - Create CodeBuild Role:

# Tạo trust policy file
cat > codebuild-trust-policy.json << EOF
{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Principal": {
        "Service": "codebuild.amazonaws.com"
      },
      "Action": "sts:AssumeRole"
    }
  ]
}
EOF

# Tạo role
aws iam create-role \
    --role-name VinaShoesCodeBuildRole \
    --assume-role-policy-document file://codebuild-trust-policy.json

# Attach policies
aws iam attach-role-policy \
    --role-name VinaShoesCodeBuildRole \
    --policy-arn arn:aws:iam::aws:policy/AWSCodeBuildDeveloperAccess

aws iam attach-role-policy \
    --role-name VinaShoesCodeBuildRole \
    --policy-arn arn:aws:iam::aws:policy/AmazonEC2ContainerRegistryPowerUser

aws iam attach-role-policy \
    --role-name VinaShoesCodeBuildRole \
    --policy-arn arn:aws:iam::aws:policy/AmazonS3FullAccess

2.2. CodePipeline Service Role

  1. T?o Role:

    • Vào IAM Console → Roles → Click “Create role”
    • Trusted entity type: ☑️ AWS service
  2. Select Service:

    • AWS service đã được chọn
    • Scroll xuống và chọn “EC2” (sẽ edit trust policy sau)
    • Click “Next”
  1. Add Permissions:

    • Search và attach các policies:
    • ☑️ AWSCodePipelineFullAccess
    • ☑️ AmazonECS_FullAccess
    • ☑️ AmazonS3FullAccess
    • ☑️ AWSCodeBuildDeveloperAccess
    • Click “Next”

    📝 Lưu ý: Nếu không tìm thấy AWSCodePipelineFullAccess, có thể sử dụng:

    • AWSCodePipelineCustomActionAccess
    • AWSCodePipelineApproverAccess
    • Hoặc tạo custom policy sau

    Hoặc tạo Custom Policy:

    • Thay vì attach managed policies, click “Create policy”
    • Chọn JSON tab và paste policy sau:
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "codepipeline:*",
                "codebuild:BatchGetBuilds",
                "codebuild:StartBuild",
                "ecs:UpdateService",
                "ecs:DescribeServices",
                "ecs:DescribeTaskDefinition",
                "ecs:DescribeTasks",
                "ecs:ListTasks",
                "s3:GetObject",
                "s3:PutObject",
                "s3:ListBucket"
            ],
            "Resource": "*"
        }
    ]
}
  • Policy name: VinaShoesCodePipelineCustomPolicy
  • Click “Create policy” và quay lại attach vào role
  1. Name, Review, and Create:
    • Role name: VinaShoesCodePipelineRole
    • Description: Role for CodePipeline to orchestrate CI/CD workflow
    • Click “Create role”

  1. Update Trust Policy:
    • Sau khi tạo role thành công, quay lại danh sách roles (IAM → Roles)
    • Tìm và double-click vào role name VinaShoesCodePipelineCustomPolicy
    • Bạn sẽ vào trang role details như hình thứ 2
  • Hiện tại bạn đang ở tab “Permissions”
  • Click chuyển sang tab “Trust relationships” (tab thứ 2)
  • Click “Edit trust policy”
  • Trong JSON editor, thay thế toàn bộ nội dung hiện tại với:
{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Principal": {
        "Service": "codepipeline.amazonaws.com"
      },
      "Action": "sts:AssumeRole"
    }
  ]
}
  • Click “Update policy”
  • Quay lại tab “Trust relationships” để verify trusted entity đã thay đổi thành “codepipeline.amazonaws.com”

� CLI Alternative - Create CodePipeline Role:

# Tạo trust policy file
cat > codepipeline-trust-policy.json << EOF
{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Principal": {
        "Service": "codepipeline.amazonaws.com"
      },
      "Action": "sts:AssumeRole"
    }
  ]
}
EOF

# Tạo custom policy
cat > codepipeline-policy.json << EOF
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "codepipeline:*",
                "codebuild:BatchGetBuilds",
                "codebuild:StartBuild",
                "ecs:UpdateService",
                "ecs:DescribeServices",
                "s3:GetObject",
                "s3:PutObject",
                "s3:ListBucket"
            ],
            "Resource": "*"
        }
    ]
}
EOF

# Tạo role và policy
aws iam create-policy \
    --policy-name VinaShoesCodePipelineCustomPolicy \
    --policy-document file://codepipeline-policy.json

aws iam create-role \
    --role-name VinaShoesCodePipelineRole \
    --assume-role-policy-document file://codepipeline-trust-policy.json

# Attach policy
aws iam attach-role-policy \
    --role-name VinaShoesCodePipelineRole \
    --policy-arn arn:aws:iam::577638368374:policy/VinaShoesCodePipelineCustomPolicy

�🔍 Verify Roles Created:

  • Vào IAM Console → Roles
  • Search VinaShoes để xem 2 roles đã tạo
  • Verify trust relationships và permissions

🔐 Security Best Practice: Trong production environment, nên tạo custom policies với principle of least privilege thay vì sử dụng managed policies có quyền rộng như FullAccess.


3. Tạo CodeBuild Project

3.1. Tạo Build Project

AWS Console → Developer Tools → CodeBuild → Build projects → Create build project:

  1. Project Configuration:
    • Project name: vinashoes-backend-build
    • Project type: ☑️ Default project (Create a custom CodeBuild project)
    • Click Additional configuration để mở thêm options:
      • Description: Build NestJS backend to Docker image

🏗️ CodeBuild Project Naming:

  • Project name: Nên đặt theo pattern <service>-<environment>-build
  • Description: Mô tả rõ ràng purpose và technology stack
  • Tags: Thêm tags cho cost tracking và resource management

3.2. Source Configuration

  1. Source Settings:
    • Source provider: Chọn GitHub từ dropdown
    • Credential: Bạn sẽ thấy warning “You have not connected to GitHub. Manage account credentials.”
    • Click “Manage account credentials” để connect GitHub account
    • ☑️ Use override credentials for this project only (nếu cần)
  1. Repository & Branch:
    • Repository: https://github.com/your-username/vinashoes-backend
    • Branch: main
    • Git clone depth: 1 (recommended cho faster builds)

📝 GitHub Integration Best Practices:

  • Personal Access Token: Cần repo scope cho private repositories
  • OAuth Apps: Khuyến khích dùng cho organization-level access
  • GitHub Apps: Most secure, có fine-grained permissions
  • Webhook: Automatically trigger builds khi có push events

CLI Reference - Test GitHub Connection:

# Test repository access (cần GitHub CLI)
gh repo view uit-ntn/vina-shoes-server

# Clone repository locally để test
git clone https://github.com/uit-ntn/vina-shoes-server.git

3.3. Environment Configuration

  1. Provisioning Model:

    • ☑️ On-demand (Automatically provision build infrastructure in response to new builds)
  2. Environment Image:

    • ☑️ Managed image (Use an image managed by AWS CodeBuild)
  3. Compute:

    • ☑️ EC2 (Optimized for flexibility during action runs)
  4. Running Mode:

    • ☑️ Container (Running on Docker container)
  5. Operating System:

    • Amazon Linux (từ dropdown)
  6. Runtime(s):

    • Standard (từ dropdown)
  7. Image:

    • aws/codebuild/amazonlinux-x86_64-standard:5.0 (latest version)
  8. Image Version:

    • Always use the latest image for this runtime version

🖥️ Compute Environment Options:

  • Small (3 GB memory, 2 vCPUs): Suitable cho simple builds, testing
  • Medium (7 GB memory, 4 vCPUs): Recommended cho Docker builds như NestJS
  • Large (15 GB memory, 8 vCPUs): Cho complex builds, large applications
  • X-Large (145 GB memory, 72 vCPUs): Cho enterprise builds, parallel processing

Container Runtime:

  • Amazon Linux 2: Standard choice, good Docker support
  • Ubuntu: Alternative với different package ecosystem
  • Windows: Cho .NET applications
  1. Service Role:
    • ☑️ Existing service role
    • Role ARN: Select VinaShoesCodeBuildRole from dropdown

🔧 Privileged Mode: Cần enable privileged mode cho Docker builds. Điều này sẽ được config trong Additional configuration section.

⚠️ Security Implications:

  • Privileged mode cho phép container access host kernel features
  • Cần thiết cho Docker-in-Docker builds
  • Trong production, consider sử dụng AWS ECR build images thay vì Docker commands
  • Monitor privileged containers với AWS CloudTrail và GuardDuty

📝 CLI Alternative - Create CodeBuild Project:

# Tạo build project specification
cat > codebuild-project.json << EOF
{
  "name": "vinashoes-backend-build",
  "description": "Build NestJS backend to Docker image",
  "source": {
    "type": "GITHUB",
    "location": "https://github.com/uit-ntn/vina-shoes-server.git"
  },
  "artifacts": {
    "type": "NO_ARTIFACTS"
  },
  "environment": {
    "type": "LINUX_CONTAINER",
    "image": "aws/codebuild/amazonlinux2-x86_64-standard:3.0",
    "computeType": "BUILD_GENERAL1_MEDIUM",
    "privilegedMode": true
  },
  "serviceRole": "arn:aws:iam::577638368374:role/VinaShoesCodeBuildRole"
}
EOF

# Tạo project
aws codebuild create-project --cli-input-json file://codebuild-project.json

3.4. Buildspec Configuration

  1. Build Specifications:

    Option 1: Use a buildspec file (Recommended)

    • ☑️ Use a buildspec file (Store build commands in a YAML-formatted buildspec file)
    • Buildspec name: buildspec.yml (hoặc để trống để dùng default)

    Option 2: Insert build commands

    • ☑️ Insert build commands (Store build commands as build project configuration)
    • Build commands textbox: Copy và paste toàn bộ block commands sau:
# Build commands cho NestJS Docker build
echo "=== Pre-build phase ==="
echo "Logging in to Amazon ECR..."
aws ecr get-login-password --region ap-southeast-1 | docker login --username AWS --password-stdin 577638368374.dkr.ecr.ap-southeast-1.amazonaws.com

echo "=== Build phase ==="
echo "Build started on $(date)"
echo "Building the Docker image..."
docker build -t vinashoes/backend-service:latest .
docker tag vinashoes/backend-service:latest 577638368374.dkr.ecr.ap-southeast-1.amazonaws.com/vinashoes/backend-service:latest

echo "=== Post-build phase ==="
echo "Build completed on $(date)"
echo "Pushing the Docker images..."
docker push 577638368374.dkr.ecr.ap-southeast-1.amazonaws.com/vinashoes/backend-service:latest
echo "Writing image definitions file..."
printf '[{"name":"backend-container","imageUri":"%s"}]' 577638368374.dkr.ecr.ap-southeast-1.amazonaws.com/vinashoes/backend-service:latest > imagedefinitions.json

Username: AWS (cố định, luôn là “AWS” cho ECR) Password: Được generate tự động bởi command aws ecr get-login-password

  1. aws ecr get-login-password --region ap-southeast-1 → Generate temporary password
  2. | docker login --username AWS --password-stdin → Dùng password từ step 1
  3. Bạn KHÔNG cần nhập username/password thủ công
  4. AWS CLI tự động handle authentication dựa trên IAM role của CodeBuild

Điều kiện:

  • CodeBuild role phải có permission ecr:GetAuthorizationToken
  • Role đã được setup với ECR policies trong bước trước

📝 CLI Reference - ECR Commands:

# Get ECR login token
aws ecr get-login-password --region ap-southeast-1

# Login to ECR
aws ecr get-login-password --region ap-southeast-1 | \
  docker login --username AWS --password-stdin \
  577638368374.dkr.ecr.ap-southeast-1.amazonaws.com

# List ECR repositories
aws ecr describe-repositories --region ap-southeast-1

# Create ECR repository if not exists
aws ecr create-repository \
  --repository-name vinashoes/backend-service \
  --region ap-southeast-1

📋 Cách paste commands vào CodeBuild:

  1. Click radio button “Insert build commands”
  2. Scroll xuống sẽ thấy textbox “Build commands”
  3. Copy toàn bộ block commands ở trên (từ echo "=== Pre-build phase ===" đến cuối)
  4. Paste vào textbox
  5. Click “Switch to editor” nếu muốn dùng syntax highlighting

📝 Environment Variables cần thiết:

  • AWS_ACCOUNT_ID: 577638368374
  • IMAGE_TAG: latest hoặc commit hash
  • Có thể set trong Environment variables của CodeBuild project

🔧 Advanced Environment Variables:

AWS_DEFAULT_REGION=ap-southeast-1
AWS_ACCOUNT_ID=577638368374
IMAGE_REPO_NAME=vinashoes/backend-service
IMAGE_TAG=latest
DOCKER_BUILDKIT=1  # Enable Docker BuildKit for faster builds

⚠️ Source Warning: Nếu primary source là “No source”, cần cung cấp valid buildspec command. Đảm bảo đã config GitHub source ở bước trước.

🚨 Common BuildSpec Errors:

  • Missing docker login command → ECR authentication fails
  • Incorrect image tagging → Push fails
  • Missing privileged mode → Docker commands fail
  • Wrong ECR repository URI → Push to wrong location
  • Missing AWS CLI → ECR commands not found

3.5. Batch Configuration

  1. Batch Configuration:
    • Define batch configuration (optional)
    • Để unchecked cho single build execution

3.6. Artifacts Configuration

  1. Artifact 1 - Primary:
    • Type: No artifacts (từ dropdown)
    • Note: “You might choose no artifacts if you are running tests or pushing a Docker image to Amazon ECR”

💡 Why No Artifacts: Vì chúng ta push Docker image lên ECR, không cần store artifacts trong S3. ECS sẽ pull image trực tiếp từ ECR.

📋 Artifact Types Explained:

  • No artifacts: For Docker builds pushing to ECR
  • S3: For storing build outputs, test reports, documentation
  • CodePipeline: For passing files between pipeline stages
  1. Additional Configuration:
    • Click Additional configuration để access cache và encryption settings
    • Cache: Có thể enable để speed up builds
    • Encryption key: Để default

🚀 Performance Optimization - Caching:

# Trong buildspec.yml, thêm cache configuration
cache:
  paths:
    - '/root/.npm/**/*'          # npm cache
    - '/root/.cache/yarn/**/*'   # yarn cache  
    - 'node_modules/**/*'        # dependencies
    - '/var/lib/docker/**/*'     # Docker layer cache (nếu sử dụng)

📝 CLI - Enable Build Cache:

aws codebuild update-project \
  --name vinashoes-backend-build \
  --cache '{
    "type": "S3",
    "location": "vinashoes-artifacts-prod/build-cache"
  }'

3.7. Logs Configuration

  1. CloudWatch Logs:

    • ☑️ CloudWatch logs - optional
    • Group name: Leave empty (sẽ tự tạo /aws/codebuild/<project-name>)
    • Stream name prefix: Leave empty
  2. S3 Logs:

    • S3 logs - optional
    • Có thể enable để store logs trong S3 bucket

📊 Logging Best Practices:

  • CloudWatch Logs: Real-time monitoring, log querying với CloudWatch Insights
  • S3 Logs: Long-term storage, cost-effective cho archival
  • Log Retention: Set appropriate retention period để control costs
  • Log Groups: Organize logs theo project/environment

📝 CLI - Configure Logging:

# Create CloudWatch log group
aws logs create-log-group \
  --log-group-name /aws/codebuild/vinashoes-backend-build

# Set retention period (30 days)
aws logs put-retention-policy \
  --log-group-name /aws/codebuild/vinashoes-backend-build \
  --retention-in-days 30
  1. Create Build Project:
    • Review tất cả settings
    • Click “Create build project”

3.8. Test CodeBuild Project

  1. Manual Build Test:
    • Vào CodeBuild project vinashoes-backend-build
    • Click “Start build” để test
    • Monitor build logs và verify ECR push

� CLI - Start Build Test:

# Start a build manually
aws codebuild start-build \
  --project-name vinashoes-backend-build

# Get build status
aws codebuild batch-get-builds \
  --ids "build-id-from-previous-command"

# List recent builds
aws codebuild list-builds-for-project \
  --project-name vinashoes-backend-build \
  --sort-order DESCENDING

�🔧 Privileged Mode Required: Sau khi tạo project, cần enable Privileged mode:

  1. Vào CodeBuild project → EditEnvironment
  2. Scroll xuống Additional configuration
  3. ☑️ Privileged - Enable this flag if you want to build Docker images
  4. Click Update environment

3.9. Create buildspec.yml

Tạo file buildspec.yml trong root của backend repository:

version: 0.2

env:
  variables:
    AWS_DEFAULT_REGION: ap-southeast-1
    AWS_ACCOUNT_ID: "577638368374"
    IMAGE_REPO_NAME: vinashoes/backend-service
    IMAGE_TAG: latest

phases:
  pre_build:
    commands:
      - echo Logging in to Amazon ECR...
      - aws ecr get-login-password --region $AWS_DEFAULT_REGION | docker login --username AWS --password-stdin $AWS_ACCOUNT_ID.dkr.ecr.$AWS_DEFAULT_REGION.amazonaws.com
      - REPOSITORY_URI=$AWS_ACCOUNT_ID.dkr.ecr.$AWS_DEFAULT_REGION.amazonaws.com/$IMAGE_REPO_NAME
      - COMMIT_HASH=$(echo $CODEBUILD_RESOLVED_SOURCE_VERSION | cut -c 1-7)
      - IMAGE_TAG=${COMMIT_HASH:=latest}
      
  build:
    commands:
      - echo Build started on `date`
      - echo Building the Docker image...
      - docker build -t $IMAGE_REPO_NAME:$IMAGE_TAG .
      - docker tag $IMAGE_REPO_NAME:$IMAGE_TAG $REPOSITORY_URI:$IMAGE_TAG
      - docker tag $IMAGE_REPO_NAME:$IMAGE_TAG $REPOSITORY_URI:latest
      
  post_build:
    commands:
      - echo Build completed on `date`
      - echo Pushing the Docker images...
      - docker push $REPOSITORY_URI:$IMAGE_TAG
      - docker push $REPOSITORY_URI:latest
      - echo Writing image definitions file...
      - printf '[{"name":"backend-container","imageUri":"%s"}]' $REPOSITORY_URI:$IMAGE_TAG > imagedefinitions.json

artifacts:
  files:
    - imagedefinitions.json
    - '**/*'

💡 BuildSpec Best Practices:

  • Environment Variables: Account ID đã được set thành 577638368374
  • Commit Hash Tags: Sử dụng commit hash để track image versions
  • Privileged Mode: Cần enable trong CodeBuild environment để run Docker commands
  • ECR Permissions: Đảm bảo CodeBuild role có quyền push tới ECR repository

🚀 Advanced BuildSpec Features:

# Multi-stage builds với cache
pre_build:
  commands:
    - echo Build cache warming...
    - docker pull $REPOSITORY_URI:latest || true
    
build:
  commands:
    - docker build --cache-from $REPOSITORY_URI:latest -t $IMAGE_REPO_NAME:$IMAGE_TAG .
    
# Testing phase
post_build:
  commands:
    - echo Running tests...
    - docker run --rm $IMAGE_REPO_NAME:$IMAGE_TAG npm test
    - echo Pushing only if tests pass...
    - docker push $REPOSITORY_URI:$IMAGE_TAG

📝 CLI - Validate BuildSpec:

# Validate buildspec syntax
aws codebuild validate-project \
  --source '{
    "type": "GITHUB",
    "location": "https://github.com/uit-ntn/vina-shoes-server.git",
    "buildspec": "buildspec.yml"
  }'

🔧 Required Configuration: Sau khi tạo project, cần enable Privileged mode trong Environment settings:

  1. Vào CodeBuild project → Edit → Environment
  2. ☑️ Privileged - Enable this flag if you want to build Docker images

4. Tạo CodePipeline

4.1. Tạo Pipeline - Chọn Template

AWS Console → CodePipeline → Create pipeline:

  1. Choose Creation Option:
    • Category: Chọn ☑️ Deployment
    • Template: Chọn ☑️ Push to ECR
    • Description: “Build and push a new container image to Amazon ECR”
    • Click Next

💡 Template Benefits:

  • Push to ECR template tự động config build stage với ECR integration
  • Pre-configured với best practices cho container deployment
  • Giảm thiểu manual configuration errors

🔄 Template vs Manual Setup:

  • Template: Faster setup, CloudFormation managed, standardized configuration
  • Manual: Full control, custom stages, complex workflows
  • Hybrid: Start with template, customize sau khi tạo

📋 Available Templates:

  • Push to ECR: Container builds
  • Deploy to ECS: Container deployment
  • Deploy to S3: Static website deployment
  • Custom: Build your own pipeline

4.2. Choose Source (Step 2 of 3)

  1. Source Configuration:

    • Source provider: GitHub (via GitHub App) (từ dropdown)
    • Connection: Sử dụng existing connection hoặc click “Connect to GitHub”
  2. Repository Details:

    • Repository name: Nhập uit-ntn/vina-shoes-server
    • Default branch: Nhập main
    • Output artifact format: ☑️ CodePipeline default
    • Click Next

🔗 GitHub Connection Types:

  • GitHub App: Most secure, fine-grained permissions, organization-level
  • Personal Access Token: Simple setup, user-level access
  • OAuth: Legacy method, broader permissions

📝 CLI - List CodeStar Connections:

# List existing connections
aws codestar-connections list-connections

# Get connection details
aws codestar-connections get-connection \
  --connection-arn "arn:aws:codestar-connections:region:account:connection/connection-id"

4.3. Configure Template (Step 3 of 3)

Template sẽ tự động điền một số thông tin từ các bước trước:

  1. Template Details (Read-only):

    • ConnectionArn: ARN của CodeConnection đến GitHub
    • FullRepositoryId: uit-ntn/vina-shoes-server
    • BranchName: main
  2. Pipeline Configuration (Editable):

    CodePipelineName:

    • Thay đổi từ SimpleDockerService thành: vinashoes-backend-pipeline

    DockerBuildContext:

    • Để trống hoặc nhập . (build từ root directory)

    DockerFilePath:

    • Giữ default: ./Dockerfile

    ImageTag:

    • Giữ default: latest

    RetentionPolicy:

    • Chọn Delete từ dropdown

🏗️ Template Configuration Explained:

  • CodePipelineName: Tên pipeline, sẽ tạo CloudFormation stack cùng tên
  • DockerBuildContext: Directory context cho Docker build (. = root)
  • DockerFilePath: Path tới Dockerfile trong repository
  • ImageTag: Tag cho Docker image, có thể dùng variables như ${CODEBUILD_BUILD_NUMBER}
  • RetentionPolicy: Gì sẽ xảy ra với resources khi delete stack

🔧 Advanced Configuration Options:

# Có thể sử dụng trong template parameters
DockerBuildArgs: |
  --build-arg NODE_ENV=production
  --build-arg API_VERSION=v1
  
ImageTag: "${CODEBUILD_BUILD_NUMBER}-${CODEBUILD_RESOLVED_SOURCE_VERSION}"
  1. Preview & Create:
    • Click ► Preview để xem CloudFormation template (optional)
    • Click “Create pipeline from template”

4.4. Template Generated Resources

Template “Push to ECR” sẽ tự động tạo:

  • CodePipeline: 2-stage pipeline (Source + Build)
  • CodeBuild Project: Docker build environment với ECR integration
  • ECR Repository: Container registry cho Docker images
  • IAM Roles: Service roles cho pipeline và build
  • S3 Bucket: Artifact storage
  • CloudFormation Stack: Manage toàn bộ resources

💡 Template Workflow: Template “Push to ECR” tạo 2-stage pipeline:

  1. Source Stage: Pull code từ GitHub
  2. Build Stage: Build Docker image và push tới ECR

Khác với manual setup: Template không bao gồm deploy stage. Sau khi pipeline chạy thành công, bạn cần manually update ECS service để sử dụng image mới từ ECR.

🔄 ECS Deployment: Template này chỉ build và push image lên ECR. Để tự động deploy lên ECS, bạn cần:

  1. Option 1: Thêm deploy stage sau khi tạo pipeline
  2. Option 2: Sử dụng ECS service auto-deployment khi detect new image
  3. Option 3: Manual update ECS service task definition

📝 CLI - Add ECS Deploy Stage:

# Get current pipeline definition
aws codepipeline get-pipeline \
  --name vinashoes-backend-pipeline > pipeline.json

# Edit pipeline.json to add deploy stage, then update
aws codepipeline update-pipeline \
  --cli-input-json file://pipeline.json

# Alternative: Use ECS rolling update
aws ecs update-service \
  --cluster vinashoes-cluster \
  --service vinashoes-backend \
  --force-new-deployment

5. Xác minh Tạo Pipeline

5.1. Kiểm tra CloudFormation Stack

  1. CloudFormation Console:
    • Vào AWS Console → CloudFormation
    • Tìm stack với tên chứa vinashoes-backend-pipeline
    • Verify status = CREATE_COMPLETE

📝 CLI - Monitor CloudFormation:

# List stacks with pipeline name
aws cloudformation list-stacks \
  --stack-status-filter CREATE_COMPLETE \
  --query 'StackSummaries[?contains(StackName, `vinashoes-backend-pipeline`)]'

# Get stack details
aws cloudformation describe-stacks \
  --stack-name "stack-name-from-above"

# List stack resources
aws cloudformation list-stack-resources \
  --stack-name "stack-name-from-above"

5.2. Check Created Resources

  1. CodePipeline:
    • Vào CodePipeline console
    • Verify pipeline vinashoes-backend-pipeline exists
    • Check pipeline có 2 stages: Source + Build

📝 CLI - Verify Pipeline:

# Get pipeline details
aws codepipeline get-pipeline \
  --name vinashoes-backend-pipeline

# List pipelines
aws codepipeline list-pipelines

# Get pipeline execution history
aws codepipeline list-pipeline-executions \
  --pipeline-name vinashoes-backend-pipeline
  1. CodeBuild Project:
    • Vào CodeBuild console
    • Verify build project được tạo với tên auto-generated
    • Check environment configuration

📝 CLI - Verify Resources:

# List CodeBuild projects
aws codebuild list-projects

# Get project details
aws codebuild batch-get-projects \
  --names "project-name-from-template"

# List ECR repositories
aws ecr describe-repositories

# Test ECR access
aws ecr get-login-password --region ap-southeast-1

# List S3 buckets created by template
aws s3 ls | grep -i pipeline

🏗️ Template Resources Created:

  1. CodePipeline: Main orchestration pipeline
  2. CodeBuild Project: Docker build environment
  3. ECR Repository: Container image registry
  4. S3 Bucket: Artifact storage (auto-generated name)
  5. IAM Roles: Service roles cho pipeline và build (auto-generated)
  6. CloudWatch Log Groups: Build logs storage

📋 Resource Naming Pattern:

  • Pipeline: vinashoes-backend-pipeline
  • CodeBuild: {pipeline-name}-{random-suffix}
  • ECR: {pipeline-name} hoặc auto-generated
  • S3: codepipeline-{region}-{random-id}

6. Task 11 Hoàn Thành!

📋 Thành phần CI/CD đã triển khai

Thành phần Trạng thái Chi tiết
✅ S3 Artifacts ACTIVE vinashoes-artifacts-prod lưu trữ build logs
✅ IAM Roles CONFIGURED CodeBuild + CodePipeline roles
✅ CodeBuild ACTIVE Xây dựng & push Docker images
✅ CodePipeline ACTIVE Pipeline automation 3 giai đoạn
✅ ECS Integration WORKING Tự động deploy lên Fargate service

🚀 CI/CD Workflow Results

🔄 Quy trình Triển khai Tự động:

  1. Developer push code → GitHub repository
  2. Pipeline kích hoạt → Source stage pull changes
  3. CodeBuild thực thi → Xây dựng Docker image
  4. Image được push → ECR repository với commit hash tag
  5. ECS deploy → Cập nhật Fargate service
  6. Artifacts lưu trữ → S3 bucket cho traceability

Lợi ích đạt được:

  • Zero bước deployment thủ công
  • 📝 Full audit trail với S3 artifacts
  • 🔄 Automated rollbacks possible via ECS
  • 📊 Build monitoring via CloudWatch

🔧 Hướng dẫn Khắc phục Sự cố

Build Thất bại:

  • Kiểm tra quyền login ECR
  • Xác minh syntax Dockerfile
  • Xem lại commands buildspec.yml

Deploy Thất bại:

  • Xác minh ECS service tồn tại
  • Check task definition compatibility
  • Confirm security group settings

Pipeline Stuck:

  • Check IAM role permissions
  • Verify S3 bucket access
  • Review CodeBuild compute resources

💡 Tối ưu Production

Hiệu suất:

  • Thêm Docker layer caching trong buildspec
  • Triển khai parallel builds cho multiple services
  • Tối ưu build compute size

Bảo mật:

  • Thay thế managed policies với least-privilege custom policies
  • Add vulnerability scanning stage
  • Implement secrets management với AWS Secrets Manager

Giám sát:

  • Thêm CloudWatch alarms cho pipeline failures
  • Thiết lập SNS notifications cho deployment status
  • Triển khai blue/green deployment strategy

7. Dọn Dẹp Tài Nguyên

7.1. Xóa CodePipeline

Xóa pipeline và tất cả associated resources:

# Delete CodePipeline
aws codepipeline delete-pipeline --name vinashoes-backend-pipeline

# Delete CloudFormation stack (nếu được tạo từ template)
aws cloudformation delete-stack --stack-name vinashoes-backend-pipeline

7.2. Xóa CodeBuild Project

Xóa build project và associated resources:

# Delete CodeBuild project
aws codebuild delete-project --name vinashoes-backend-build

# Delete CloudWatch log group
aws logs delete-log-group --log-group-name "/aws/codebuild/vinashoes-backend-build"

7.3. Xóa ECR Repository

Xóa container images và repository:

# Delete ECR repository (sẽ xóa tất cả images)
aws ecr delete-repository \
  --repository-name vinashoes/backend-service \
  --force

# Hoặc xóa từng image cụ thể
aws ecr batch-delete-image \
  --repository-name vinashoes/backend-service \
  --image-ids imageTag=latest

7.4. Xóa S3 Artifacts Bucket

Xóa S3 bucket chứa build artifacts:

# Empty bucket trước khi xóa
aws s3 rm s3://vinashoes-artifacts-prod --recursive

# Delete bucket
aws s3 rb s3://vinashoes-artifacts-prod

7.5. Xóa IAM Roles

Xóa service roles đã tạo:

# Detach policies trước khi xóa
aws iam detach-role-policy \
  --role-name VinaShoesCodeBuildRole \
  --policy-arn arn:aws:iam::aws:policy/AWSCodeBuildDeveloperAccess

aws iam detach-role-policy \
  --role-name VinaShoesCodeBuildRole \
  --policy-arn arn:aws:iam::aws:policy/AmazonEC2ContainerRegistryPowerUser

aws iam detach-role-policy \
  --role-name VinaShoesCodeBuildRole \
  --policy-arn arn:aws:iam::aws:policy/AmazonS3FullAccess

# Xóa roles
aws iam delete-role --role-name VinaShoesCodeBuildRole
aws iam delete-role --role-name VinaShoesCodePipelineRole

7.6. Xóa CloudWatch Resources

Xóa log groups và metrics:

# Xóa CodeBuild log groups
aws logs delete-log-group --log-group-name "/aws/codebuild/vinashoes-backend-build"

# Xóa pipeline execution history (optional)
# Note: CloudWatch tự động xóa metrics sau 15 tháng

⚠️ Thứ Tự Dọn Dẹp CI/CD:

  1. Stop pipeline executions
  2. Delete CodePipeline
  3. Delete CodeBuild project
  4. Empty và delete S3 bucket
  5. Delete ECR repository
  6. Delete IAM roles
  7. Clean up CloudWatch logs

8. Phân Tích Chi Phí

8.1. Tổng Quan Giá CI/CD Services

Cấu trúc giá AWS CI/CD:

Thành Phần Dịch Vụ Miễn Phí Trả Phí Ước Tính Chi Phí
CodePipeline 1 pipeline free $1/pipeline/tháng $1/tháng
CodeBuild 100 build minutes $0.005/minute $10-50/tháng
ECR Storage 500MB free $0.10/GB/tháng $1-5/tháng
S3 Artifacts 5GB free $0.023/GB/tháng $1-3/tháng
CloudWatch Logs 5GB free $0.50/GB ingested $5-15/tháng

8.2. Chi Tiết Chi Phí Hàng Tháng

Ước tính chi phí cho CI/CD pipeline:

Chi Phí Cơ Bản CI/CD:
  CodePipeline: $1/tháng (1 pipeline)
  CodeBuild: $25/tháng (500 build minutes)
  ECR Storage: $2/tháng (20GB images)
  S3 Artifacts: $1/tháng (50GB artifacts)
  CloudWatch Logs: $8/tháng (16GB logs)
  
Build Frequency:
  Daily builds: 30 builds/tháng
  Average build time: 15 minutes
  Peak usage: 1000 minutes/tháng
  
Tổng Chi Phí Hàng Tháng: $37/tháng

8.3. Chiến Lược Tối Ưu Chi Phí

Giảm chi phí CI/CD:

Chiến Thuật Tối Ưu:
  1. Build Optimization:
     - Sử dụng build cache để giảm build time
     - Implement parallel builds cho multiple services
     - Optimize Docker layers để giảm image size
     
  2. Storage Management:
     - Set ECR lifecycle policies để xóa old images
     - Configure S3 lifecycle cho artifacts
     - Use CloudWatch log retention policies
     
  3. Pipeline Efficiency:
     - Combine multiple services vào single pipeline
     - Use conditional builds để skip unnecessary steps
     - Implement build queuing để optimize resource usage

8.4. Phân Tích ROI

Lợi Ích CI/CD Automation vs Chi Phí:

Loại Lợi Ích Giá Trị Tác Động Chi Phí
Developer Productivity Giảm 50% deployment time $20K+ mỗi developer/năm
Deployment Frequency Tăng từ weekly sang daily $50K+ faster time-to-market
Error Reduction Giảm 80% manual deployment errors $10K+ fewer outages
Rollback Speed Instant rollbacks vs hours $100K+ business continuity
Quality Assurance Automated testing integration $30K+ improved quality

Tính Toán ROI:

  • Chi Phí Hàng Năm: $444 (37/tháng × 12)
  • Lợi Ích Hàng Năm: $200K+ (productivity + quality + speed)
  • ROI: 45,000% (lợi ích ÷ chi phí)

8.5. Giám Sát Chi Phí

Theo dõi chi tiêu CI/CD:

# Kiểm tra chi phí CodeBuild
aws ce get-cost-and-usage \
  --time-period Start=2024-01-01,End=2024-01-31 \
  --granularity MONTHLY \
  --metrics BlendedCost \
  --group-by Type=DIMENSION,Key=SERVICE \
  --filter '{
    "Dimensions": {
      "Key": "SERVICE",
      "Values": ["AWS CodeBuild"]
    }
  }'

# Giám sát build usage
aws codebuild list-builds \
  --sort-order DESCENDING \
  --query 'builds[*].{id:id,buildNumber:buildNumber,duration:duration,endTime:endTime}'

# Check ECR storage usage
aws ecr describe-repositories \
  --query 'repositories[*].{repositoryName:repositoryName,imageCount:imageCount}'

# Monitor S3 artifact storage
aws s3 ls s3://vinashoes-artifacts-prod --recursive --summarize

💡 Thực Tiễn Quản Lý Chi Phí Tốt Nhất

Build Optimization:

  • Use CodeBuild reserved capacity cho high-volume projects
  • Implement build caching để reduce build times
  • Monitor build duration trends để identify bottlenecks

Storage Management:

  • Set ECR lifecycle policies: keep last 10 images per tag
  • Configure S3 lifecycle: transition old artifacts to Glacier
  • Use CloudWatch log retention: 30 days cho active, 7 days cho dev

Cost Monitoring:

  • Set billing alerts cho $50/tháng threshold
  • Monitor build minutes usage hàng tuần
  • Review pipeline execution frequency monthly

Scaling Considerations:

  • High-frequency deployments: Chi phí scale với build frequency
  • Large teams: Cost increases với parallel pipeline usage
  • Multi-environment: Separate pipelines cho dev/staging/prod

🚀 Production-Ready AWS Microservices Platform with Complete CI/CD! 🚀