Skip to content

This project automates the deployment of a Python application to AWS Elastic Container Service (ECS) using GitHub Actions, Docker, and Terraform.

Notifications You must be signed in to change notification settings

NotHarshhaa/tf-ecr-ecs-gh-deploy

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

43 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸš€ CI/CD Pipeline: Deploying a Python App to AWS ECS using GitHub Actions & Terraform

GitHub Actions to AWS ECS


πŸ“Œ Overview

This project automates the deployment of a Python application to AWS Elastic Container Service (ECS) using GitHub Actions, Docker, and Terraform.

By the end of this guide, you'll have a fully automated CI/CD pipeline that:
βœ… Builds a Docker image of the Python app
βœ… Pushes the image to AWS Elastic Container Registry (ECR)
βœ… Deploys the containerized app to ECS (Fargate)
βœ… Uses Terraform to provision and manage AWS infrastructure


🎯 Project Goals

βœ… Develop a Python-based web application
βœ… Containerize the app using Docker and push it to AWS ECR
βœ… Deploy the app to AWS ECS using Terraform
βœ… Automate testing, building, and deployment using GitHub Actions


πŸ“‚ Detailed Project Structure

TF-ECR-ECS-GH-Deploy/
│── .github/
β”‚   └── workflows/        # GitHub Actions workflows
β”‚       β”œβ”€β”€ apply.yml     # Workflow to apply Terraform changes
β”‚       β”œβ”€β”€ destroy.yml   # Workflow to destroy Terraform resources
β”‚       β”œβ”€β”€ workflow.yml  # Main CI/CD workflow
β”‚
│── terraform/            # Terraform configurations for AWS resources
β”‚
│── .dockerignore         # Exclude unnecessary files from Docker builds
│── app.py                # Python application (Flask/Django/FastAPI)
│── Dockerfile            # Docker configuration for building the application
│── README.md             # Project documentation
│── requirements.txt      # Python dependencies for the application

πŸ”₯ Prerequisites

Ensure the following tools are installed:

Tool Version Purpose
AWS CLI Latest Manage AWS services
Terraform Latest Infrastructure as Code
Docker Latest Build and manage containers
GitHub Actions N/A Automate CI/CD
Python 3 Latest Run the application

πŸ“Œ Configure AWS CLI with your credentials:

aws configure

πŸ” GitHub Secrets Configuration

Store sensitive AWS credentials in GitHub:

1️⃣ Navigate to Settings β†’ Secrets and Variables β†’ Actions
2️⃣ Click New repository secret
3️⃣ Add the following secrets:

Secret Name Value
AWS_ACCESS_KEY_ID Your AWS Access Key
AWS_SECRET_ACCESS_KEY Your AWS Secret Key
AWS_REGION AWS Region (e.g., us-east-1)
ECR_REPOSITORY Your AWS ECR Repository Name
ECS_CLUSTER_NAME Your ECS Cluster Name
ECS_SERVICE_NAME Your ECS Service Name

πŸ“ Terraform Configuration

Terraform provisions AWS infrastructure, including:

βœ… VPC, Subnets, Security Groups
βœ… AWS ECR (Container registry)
βœ… AWS ECS Cluster & Fargate Service
βœ… IAM Roles & Policies

Example Terraform configuration:

provider "aws" {
  region = "us-east-1"
}

resource "aws_ecr_repository" "app" {
  name = "my-python-app"
}

resource "aws_ecs_cluster" "main" {
  name = "my-cluster"
}

πŸ“Œ Run Terraform commands to apply changes:

terraform init
terraform apply -auto-approve

πŸ“œ GitHub Actions CI/CD Workflow

πŸ“Œ Workflow File Location:

  • .github/workflows/workflow.yml

πŸ”„ Workflow Overview

βœ… Triggers

  • Runs on push or pull_request events to main branch

βœ… Job 1: Build & Push Docker Image

  • Builds the Docker image and pushes it to AWS ECR

βœ… Job 2: Deploy to AWS ECS

  • Updates the ECS Service to use the new container

πŸ”„ Step-by-Step Breakdown of CI/CD Pipeline

1️⃣ Checkout Repository

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

2️⃣ Configure AWS Credentials

- 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: ${{ secrets.AWS_REGION }}

3️⃣ Login to AWS ECR

- name: Login to AWS ECR
  run: |
    aws ecr get-login-password --region ${{ secrets.AWS_REGION }} | docker login --username AWS --password-stdin ${{ secrets.ECR_REPOSITORY }}

4️⃣ Build & Push Docker Image

- name: Build, Tag, and Push Image to ECR
  run: |
    docker build -t ${{ secrets.ECR_REPOSITORY }}:latest .
    docker tag ${{ secrets.ECR_REPOSITORY }}:latest ${{ secrets.ECR_REPOSITORY }}:latest
    docker push ${{ secrets.ECR_REPOSITORY }}:latest

5️⃣ Deploy to AWS ECS

- name: Deploy to AWS ECS
  run: |
    aws ecs update-service --cluster ${{ secrets.ECS_CLUSTER_NAME }} --service ${{ secrets.ECS_SERVICE_NAME }} --force-new-deployment

πŸ“Š Monitoring Deployment

πŸ“Œ How to check deployment progress:

1️⃣ GitHub Actions Logs: Navigate to Actions tab
2️⃣ AWS ECS Console: Check service & tasks status
3️⃣ AWS CloudWatch Logs: View application logs


βœ… Final Testing

Once deployment is successful, test the application:

curl http://your-ecs-service-url

🏁 Conclusion

πŸŽ‰ You’ve successfully deployed a Python app to AWS ECS using GitHub Actions & Terraform!

βœ… Fully automated CI/CD pipeline
βœ… Scalable & secure AWS infrastructure
βœ… Seamless GitHub Actions integration


⭐ Hit the Star!

If you find this repository helpful and plan to use it for learning, please consider giving it a star ⭐. Your support motivates me to keep improving and adding more valuable content! πŸš€


πŸ› οΈ Author & Community

This project is crafted with passion by Harshhaa πŸ’‘.

I’d love to hear your feedback! Feel free to open an issue, suggest improvements, or just drop by for a discussion. Let’s build a strong DevOps community together!


πŸ“§ Let's Connect!

Stay connected and explore more DevOps content with me:

LinkedIn GitHub Telegram Dev.to Hashnode


πŸ“’ Stay Updated!

Want to stay up to date with the latest DevOps trends, best practices, and project updates? Follow me on my blogs and social channels!

Follow Me

About

This project automates the deployment of a Python application to AWS Elastic Container Service (ECS) using GitHub Actions, Docker, and Terraform.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published