GitHub Actions CI/CD Workflow for Automated Deployment to AWS ECS

I'm looking for a GitHub Actions workflow that automates the deployment of my application to AWS ECS whenever I push changes to the main branch. The workflow should build the Docker image, push it to AWS ECR, and update the ECS service with the new image.

1 Answers

✓ Best Answer

🚀 Automating AWS ECS Deployments with GitHub Actions

Here's a comprehensive GitHub Actions workflow that automates the deployment of your application to AWS ECS. This workflow builds a Docker image, pushes it to AWS ECR, and updates the ECS service.

Prerequisites

  • ✅ AWS credentials configured in GitHub Secrets (AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, AWS_REGION).
  • 🐳 Dockerfile in your repository.
  • ☁️ AWS ECS cluster and service already set up.
  • 📦 AWS ECR repository created.

Workflow YAML File (.github/workflows/deploy.yml)

name: Deploy to AWS ECS

on:
  push:
    branches: [main]

env:
  AWS_REGION: your-aws-region  # e.g., us-east-1
  ECR_REPOSITORY: your-ecr-repo  # e.g., my-app
  ECS_SERVICE: your-ecs-service  # e.g., my-app-service
  ECS_CLUSTER: your-ecs-cluster  # e.g., my-ecs-cluster
  TASK_DEFINITION: task-definition.json # Task definition file

jobs:
  deploy:
    name: Deploy
    runs-on: ubuntu-latest

    steps:
      - name: Checkout
        uses: actions/checkout@v3

      - name: Configure AWS Credentials
        uses: aws-actions/configure-aws-credentials@v2
        with:
          aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
          aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
          aws-region: ${{ env.AWS_REGION }}

      - name: Login to Amazon ECR
        id: login-ecr
        uses: aws-actions/amazon-ecr-login@v1

      - name: Build, tag, and push image to Amazon ECR
        id: build-image
        env:
          ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
          IMAGE_TAG: ${{ github.sha }}
        run: |
          docker build -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG .
          docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG
          echo "image=$ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG" >> $GITHUB_OUTPUT

      - name: Fill in the new image ID in the Amazon ECS task definition
        id: task-def
        uses: aws-actions/amazon-ecs-render-task-definition@v1
        with:
          task-definition: ${{ env.TASK_DEFINITION }}
          container-name: your-container-name # e.g., web
          image: ${{ steps.build-image.outputs.image }}

      - name: Deploy Amazon ECS task definition
        uses: aws-actions/amazon-ecs-deploy-task-definition@v1
        with:
          task-definition: ${{ steps.task-def.outputs.task-definition }}
          service: ${{ env.ECS_SERVICE }}
          cluster: ${{ env.ECS_CLUSTER }}

Explanation

  1. Name: Defines the name of the workflow.
  2. On: Triggers the workflow on pushes to the main branch.
  3. Env: Sets environment variables for AWS region, ECR repository, ECS service, ECS cluster, and Task Definition file.
  4. Jobs: Defines the deploy job.
  5. Checkout: Checks out the repository.
  6. Configure AWS Credentials: Configures AWS credentials using secrets.
  7. Login to Amazon ECR: Logs in to Amazon ECR.
  8. Build, tag, and push image to Amazon ECR: Builds the Docker image, tags it with the Git SHA, and pushes it to ECR.
  9. Fill in the new image ID in the Amazon ECS task definition: Updates the task definition with the new image ID.
  10. Deploy Amazon ECS task definition: Deploys the updated task definition to the ECS service.

Task Definition (task-definition.json)

{
  "family": "your-task-family",
  "containerDefinitions": [
    {
      "name": "your-container-name",
      "image": "your-ecr-repo:latest",
      "cpu": 256,
      "memory": 512,
      "portMappings": [
        {
          "containerPort": 80,
          "hostPort": 80
        }
      ],
      "essential": true
    }
  ],
  "requiresCompatibilities": [
    "FARGATE"
  ],
  "networkMode": "awsvpc",
  "memory": "512",
  "cpu": "256"
}

Important Notes

  • ✏️ Replace placeholder values (your-aws-region, your-ecr-repo, your-ecs-service, your-ecs-cluster, your-container-name, your-task-family) with your actual values.
  • 🔑 Ensure your AWS credentials have the necessary permissions to interact with ECR and ECS.
  • ⚠️ Adjust CPU and memory settings in the task definition based on your application's requirements.

Know the answer? Login to help.