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
β
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
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
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
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 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
π Workflow File Location:
.github/workflows/workflow.yml
- Runs on
push
orpull_request
events tomain
branch
- Builds the Docker image and pushes it to AWS ECR
- Updates the ECS Service to use the new container
- name: Checkout code
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: ${{ secrets.AWS_REGION }}
- name: Login to AWS ECR
run: |
aws ecr get-login-password --region ${{ secrets.AWS_REGION }} | docker login --username AWS --password-stdin ${{ secrets.ECR_REPOSITORY }}
- 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
- name: Deploy to AWS ECS
run: |
aws ecs update-service --cluster ${{ secrets.ECS_CLUSTER_NAME }} --service ${{ secrets.ECS_SERVICE_NAME }} --force-new-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
Once deployment is successful, test the application:
curl http://your-ecs-service-url
π 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
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! π
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!
Stay connected and explore more DevOps content with me:
Want to stay up to date with the latest DevOps trends, best practices, and project updates? Follow me on my blogs and social channels!