Building a CI/CD Pipeline with GitHub Actions, Docker, and AWS ECS with Fargate

Listen to this Post

In this article, Suman Gaire demonstrates how to set up a CI/CD pipeline using GitHub Actions, Docker, and AWS ECS with Fargate. The example focuses on deploying a FastAPI-based application. Below are the verified commands and codes to implement this setup:

Step 1: Set Up GitHub Actions Workflow

Create a `.github/workflows/deploy.yml` file in your repository:

name: CI/CD Pipeline

on:
push:
branches:
- main

jobs:
build-and-deploy:
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v2

<ul>
<li>name: Set up Docker Buildx
uses: docker/setup-buildx-action@v1</p></li>
<li><p>name: Log in to Amazon ECR
id: login-ecr
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: us-east-1</p></li>
<li><p>name: Build, tag, and push image to Amazon ECR
id: build-image
env:
ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
ECR_REPOSITORY: my-repo
IMAGE_TAG: latest
run: |
docker build -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG .
docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG</p></li>
<li><p>name: Deploy to AWS ECS
uses: aws-actions/amazon-ecs-deploy-task-definition@v1
with:
task-definition: task-definition.json
service: my-service
cluster: my-cluster
wait-for-service-stability: true

Step 2: Create an ECS Task Definition

Save the following as `task-definition.json`:

{
"family": "my-task",
"networkMode": "awsvpc",
"executionRoleArn": "arn:aws:iam::123456789012:role/ecsTaskExecutionRole",
"containerDefinitions": [
{
"name": "my-container",
"image": "123456789012.dkr.ecr.us-east-1.amazonaws.com/my-repo:latest",
"essential": true,
"portMappings": [
{
"containerPort": 80,
"hostPort": 80
}
]
}
],
"requiresCompatibilities": [
"FARGATE"
],
"cpu": "256",
"memory": "512"
}

Step 3: Deploy to AWS ECS

Run the following AWS CLI commands to create the ECS cluster and service:

aws ecs create-cluster --cluster-name my-cluster

aws ecs register-task-definition --cli-input-json file://task-definition.json

aws ecs create-service --cluster my-cluster --service-name my-service --task-definition my-task --desired-count 1 --launch-type FARGATE --network-configuration "awsvpcConfiguration={subnets=[subnet-0123456789abcdef0],securityGroups=[sg-0123456789abcdef0],assignPublicIp=ENABLED}"

What Undercode Say

Setting up a CI/CD pipeline with GitHub Actions, Docker, and AWS ECS with Fargate is a powerful way to automate your deployment process. This approach eliminates the need to manage Kubernetes or EC2 instances, making it ideal for developers who want a low-touch, serverless solution. By leveraging GitHub Actions, you can automate the build, test, and deployment phases, ensuring consistent and reliable releases. AWS ECS with Fargate provides a scalable and cost-effective platform for running containerized applications, as you only pay for the resources your containers use.

To further enhance your pipeline, consider integrating additional AWS services like CloudWatch for monitoring and Lambda for event-driven automation. For Linux users, commands like docker ps, docker logs, and `aws ecs describe-services` can help monitor and troubleshoot your deployments. Windows users can use PowerShell commands like `Get-ECSTask` and `Get-ECSService` for similar purposes.

For more advanced configurations, explore AWS documentation on ECS and Fargate, or refer to the GitHub Actions marketplace for additional plugins and integrations. This setup not only streamlines your workflow but also ensures that your applications are deployed efficiently and securely in a cloud-native environment.

For further reading, visit:

References:

Hackers Feeds, Undercode AIFeatured Image