CD for a Dockerized application using Github Actions, Amazon ECR and Amazon ECS

Introduction

Containerized applications are becoming more popular due to the consistency they provide accross different machines and portability to different cloud providers. It is becoming increasingly valuable to build applications with dockerization in mind. With that comes the need to streamline deployment of these containerized applications.

Pre-Requisites

Step 1 (Setting up your application & AWS):

NOTE: It’s important that your ECR repository and ECS cluster are defined on the same AWS region otherwise you may have to sign into aws twice throughout the workflow

Step 2 (Setting up the workflow – triggers):

on:
  push:
    branches:
      - production

Step 2 (Setting up the workflow triggers):

Create a new job

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

    steps:

Step 3 (Checking out the repo)

    - name: Checkout
            uses: actions/checkout@v4

Step 4 (Signing into AWS)

- name: Configure AWS credentials
        uses: aws-actions/configure-aws-credentials@0e613a0980cbf65ed5b322eb7a1e075d28913a83
        with:
          aws-access-key-id: $
          aws-secret-access-key: $
          aws-region: <YOUR_AWS_REGION>
- name: Login to Amazon ECR
        id: login-ecr
        uses: aws-actions/amazon-ecr-login@62f4f872db3836360b72999f4b87f1ff13310f3a

Step 5 (Building/tagging your image and pushing it to your ECR repo)

- name: Build, tag, and push image to Amazon ECR
        id: build-image
        run: |
          docker build -t <ECR_REGISTRY>/<ECR_REPOSITORY>:latest <DOCKERFILE_FOLDER_PATH>
          docker push <ECR_REGISTRY>/<ECR_REPOSITORY>:latest

Step 6 (Deploying the application)

- name: Deploy Amazon ECS task definition
        uses: aws-actions/amazon-ecs-deploy-task-definition@df9643053eda01f169e64a0e60233aacca83799a
        with:
          task-definition: <PATH_TO_TASK_DEFINITION>
          service: <ECS_SERVICE>
          cluster: <ECS_CLUSTER>
          wait-for-service-stability: true

Conclusion

Congrationations, you have reached the end of this tutorial! After a push to your production branch you should be able to sit back and watch your newest workflow deploy your app for you!

Reference

This guide is inspired by this github actions guide Deploying to amazon elastic container service