CICD using GitHub Actions and Deploy to AWS ECS

Islam Salah
3 min readMay 10, 2023

In this article, I will demonstrate how to create CICD workflow using GitHub Actions to build and push container image to AWS ECR then deploy it to ECS Cluster using Python Application.

It is a simple solution to demonstrate CICD workflow using GitHub actions. therefor, it’s quite powerful as it allows you to trigger needed stages Build, Push Container Image then deploy automatically to your AWS Infrastructure with any change in your code.

Prerequisite:

  • Basic GitHub Action Knowledge
  • AWS Account
  • AWS ECR and ECS knowledge

Get started

Step 1: Add your AWS credentials as Secrets in GitHub repo.

Create AWS IAM User then get AWS Access Key ID and AWS Secret Access Key, then navigate to GitHub settings repo and create two new Secrets AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY, create environment variable for your AWS_REGION. Configure a action to register AWS creds to be available in next actions as below:

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

Step 2: Build and push Container Image to AWS ECR

Create AWS ECR repo, then navigate to GitHub settings repo and create environment variable for MY_ECR_REPOSITORY and MY_ECR_REGISTERY. Configure two actions one to login to AWS ECR and one to build and push the image to ECR as below:

- 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 "::set-output name=image::$ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG"

Step 3: Download AWS ECS task definition and update it with new Image

Create ECS Cluster, Task Definition and then navigate to GitHub settings repo and create environment variable MY_ECS_TASK_DEFINITION, MY_CONTAINER_NAME, ECS_SERVICE and ECS_CLUSTER. Configure two actions one to download task definition and one to inject the new image to it as below:

- name: Download task definition
run: |
aws ecs describe-task-definition --task-definition ${{ vars.MY_ECS_TASK_DEFINITION }} \
--query taskDefinition > task-definition.json

- 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: task-definition.json
container-name: ${{ vars.MY_CONTAINER_NAME }}
image: ${{ steps.build-image.outputs.image }}

Step 4: Deploy AWS ECS task definition with updates.

Configure action to deploy task definition to AWS ECS Service as below:

- 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 }}
wait-for-service-stability: true

finally you can check the log of your GitHub action workflow as below screen to be able to debug any issue:

Here it is! You are done!

Please check my GitHub repo for the Application and for the GitHub Action Yaml files (GitHub Repo).
Thank you for taking the time to read my article. I hope you find it helpful.

--

--