An end-to-end DevOps project implementing automated testing, Docker containerization, container image publishing, and continuous deployment of a FastAPI application to AWS EC2 using GitHub Actions.
CI/CD FastAPI AWS Deployment is a practical DevOps project designed to demonstrate how a Python web application can move from source code to a running cloud environment through an automated CI/CD pipeline.
Instead of manually copying application files to an AWS server, the project automates the complete deployment lifecycle:
Developer
β
β git push
βΌ
GitHub Repository
β
βΌ
GitHub Actions
β
βββ Install dependencies
βββ Run automated tests
βββ Build Docker image
βββ Push image
β
βΌ
GitHub Container Registry
β
β docker pull
βΌ
AWS EC2
β
βΌ
Docker Engine
β
βΌ
FastAPI + Uvicorn
β
βΌ
GET /health
β
βΌ
HTTP 200 Healthy
The project demonstrates the fundamental DevOps lifecycle:
Source Control β Continuous Integration β Containerization β Artifact Registry β Continuous Deployment β Cloud Infrastructure β Application Verification
flowchart LR
Dev["π¨βπ» Developer"]
GitHub["GitHub Repository"]
Actions["GitHub Actions"]
Tests["pytest<br/>Automated Tests"]
Build["Docker Build"]
GHCR["GitHub Container Registry<br/>GHCR"]
EC2["AWS EC2<br/>Amazon Linux 2023"]
Docker["Docker Engine"]
FastAPI["FastAPI<br/>Uvicorn"]
Health["GET /health"]
Verified["β
Deployment Verified"]
Dev -->|"git push main"| GitHub
GitHub --> Actions
Actions --> Tests
Tests -->|"Tests pass"| Build
Build --> GHCR
Actions -->|"SSH deployment"| EC2
GHCR -->|"docker pull"| EC2
EC2 --> Docker
Docker --> FastAPI
FastAPI --> Health
Health -->|"HTTP 200"| Verified
The deployment process consists of the following stages:
A change is committed and pushed to the main branch.
git add .
git commit -m "Update application"
git push origin mainThis triggers the GitHub Actions workflow.
GitHub Actions provisions an Ubuntu runner and:
- checks out the repository
- installs Python 3.12
- installs project dependencies
- executes the test suite
GitHub
β
GitHub Actions
β
Checkout
β
Python 3.12
β
Dependencies
β
pytest
If the tests fail, the deployment does not proceed.
After successful tests, Docker builds the application image using the project's Dockerfile.
docker build -t cicd-fastapi .The image packages:
- Python runtime
- application dependencies
- FastAPI application
- Uvicorn server
This creates a consistent artifact that can run in different environments.
GitHub Actions authenticates to GitHub Container Registry using the built-in GITHUB_TOKEN.
The image is published as:
ghcr.io/pmman-sudo/cicd-fastapi-aws:main
GHCR therefore acts as the centralized container artifact repository.
Once the image has been published, the deployment job connects to the AWS EC2 instance using SSH.
The private key is retrieved from GitHub Secrets rather than being stored in the repository.
The EC2 server executes:
docker pull ghcr.io/pmman-sudo/cicd-fastapi-aws:mainThe server therefore deploys the same container image that was produced by CI.
The existing application container is removed:
docker rm -f cicd-fastapi || trueA new container is started:
docker run -d \
--name cicd-fastapi \
--restart unless-stopped \
-p 8000:8000 \
ghcr.io/pmman-sudo/cicd-fastapi-aws:mainThe port mapping is:
EC2 port 8000
β
βΌ
Container port 8000
β
βΌ
FastAPI / Uvicorn
The deployment script waits for the application to start and performs:
curl -f http://localhost:8000/healthA successful response:
{
"status": "healthy"
}confirms that the newly deployed application is responding.
The workflow is divided into two logical stages.
flowchart TD
A["Git Push / Pull Request"] --> B["GitHub Actions"]
B --> C["Checkout Repository"]
C --> D["Setup Python 3.12"]
D --> E["Install Dependencies"]
E --> F["Run pytest"]
F -->|"Tests pass"| G["Login to GHCR"]
G --> H["Generate Docker Metadata"]
H --> I["Build Docker Image"]
I --> J["Push Image to GHCR"]
J --> K["Deploy Job"]
K --> L["Start SSH Agent"]
L --> M["Connect to EC2"]
M --> N["Pull Docker Image"]
N --> O["Remove Previous Container"]
O --> P["Start New Container"]
P --> Q["Health Check"]
Q --> R["β
Deployment Successful"]
Every push or pull request targeting main triggers the CI workflow.
The CI stage performs:
- Repository checkout
- Python environment setup
- Dependency installation
- Automated testing
- Docker image creation
- Container image publishing
The important principle is:
Code should not be deployed if the automated tests fail.
This is enforced through the job dependency:
deploy:
needs: test-and-buildTherefore:
Tests PASS
β
Build
β
Publish
β
Deploy
but:
Tests FAIL
β
Pipeline stops
β
Deployment does not occur
The deployment stage automatically updates the application running on EC2.
The deployment process:
GitHub Actions
β
β SSH
βΌ
AWS EC2
β
βββ docker pull
β
βββ docker rm
β
βββ docker run
β
βββ curl /health
This eliminates the need to manually SSH into the server and deploy every new version.
The application is containerized using Docker.
The Dockerfile uses a lightweight Python image:
FROM python:3.12-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY app ./app
EXPOSE 8000
CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8000"]A slim image provides the Python runtime while reducing unnecessary operating-system packages and image size compared with a full Python image.
The Dockerfile performs:
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txtbefore:
COPY app ./appThis improves Docker layer caching.
If application source code changes but dependencies remain unchanged, Docker can reuse the dependency installation layer.
Uvicorn is started with:
--host 0.0.0.0This allows the application to accept connections through the container's network interface rather than being restricted to the container's loopback interface.
The project also includes:
docker-compose.yml
This provides a convenient way to define and run the application locally using Docker Compose.
The project therefore demonstrates both:
- direct Docker execution
- Docker Compose configuration
The .dockerignore file prevents unnecessary or sensitive files from being included in the Docker build context.
Example:
venv/
**__pycache__/
.pytest_cache/
.git/
.gitignore
.env
tests/
This helps:
- reduce build context size
- improve build performance
- prevent unnecessary files from entering the image
- reduce the risk of accidentally including local secrets
The project uses pytest for automated testing.
Tests are executed locally with:
pytestand automatically within GitHub Actions.
This ensures that application changes are tested before deployment.
The deployment environment consists of:
| Component | Purpose |
|---|---|
| AWS EC2 | Hosts the application |
| Amazon Linux 2023 | Server operating system |
| Docker Engine | Runs the application container |
| Security Group | Controls inbound/outbound traffic |
| SSH | Secure administrative/deployment access |
| Port 8000 | FastAPI application endpoint |
The EC2 instance runs the container:
cicd-fastapi
with:
EC2 :8000 β Docker :8000 β FastAPI
Deployment credentials are stored in GitHub repository secrets rather than committed to source control.
The deployment uses:
EC2_HOST
EC2_USER
EC2_SSH_KEY
The private SSH key is supplied to GitHub Actions through:
${{ secrets.EC2_SSH_KEY }}This prevents the private key from being exposed in the repository.
β Private key in Git repository
β Private key inside Docker image
β Private key inside source code
β
Private key stored in GitHub Secrets
The project incorporates several foundational security practices:
- SSH private key stored in GitHub Secrets
.envexcluded from Docker build context.gitexcluded from Docker build context- automated tests executed before deployment
- SSH host verification
- EC2 security-group controlled access
- Docker container isolation
- deployment performed through an authenticated SSH connection
However, this project should be considered a production-style portfolio deployment, rather than a fully hardened production architecture.
A real production environment would require additional controls.
Future hardening could include:
- HTTPS/TLS
- AWS Application Load Balancer
- restricted Security Group rules
- AWS IAM roles
- immutable container image digests
- container vulnerability scanning
- secret rotation
- centralized logging
- CloudWatch monitoring
- intrusion detection
- automated rollback
- deployment approvals
- private subnets
- bastion host or SSM-based administration
- WAF protection
cicd-fastapi-aws/
β
βββ .github/
β βββ workflows/
β βββ ci-cd.yml
β
βββ app/
β βββ main.py
β
βββ tests/
β βββ ...
β
βββ .dockerignore
βββ Dockerfile
βββ docker-compose.yml
βββ requirements.txt
βββ README.md
- Python 3.12
- FastAPI
- Uvicorn
- pytest
- Docker
- Docker Compose
- GitHub Actions
- GitHub Container Registry
- Amazon Web Services
- Amazon EC2
- Amazon Linux 2023
- SSH
- Docker
- Git
- GitHub
Clone the repository:
git clone https://github.com/pmman-sudo/cicd-fastapi-aws.git
cd cicd-fastapi-awsCreate a virtual environment:
python -m venv venvActivate it on Windows:
.\venv\Scripts\Activate.ps1Install dependencies:
pip install -r requirements.txtRun the application:
uvicorn app.main:app --reloadThe API should then be available at:
http://localhost:8000
Build the image:
docker build -t cicd-fastapi .Run the container:
docker run --rm -p 8000:8000 cicd-fastapiVerify the application:
curl http://localhost:8000/healthExpected:
{
"status": "healthy"
}The published image is available through GHCR:
ghcr.io/pmman-sudo/cicd-fastapi-aws:main
Pull it:
docker pull ghcr.io/pmman-sudo/cicd-fastapi-aws:mainRun it:
docker run --rm -p 8000:8000 \
ghcr.io/pmman-sudo/cicd-fastapi-aws:mainThe completed implementation successfully demonstrated:
- FastAPI application startup
- Docker image creation
- Docker container execution
/healthendpoint validation
- GitHub Actions workflow execution
- Automated pytest execution
- Docker image creation within CI
- GHCR authentication
- Container image publication
- Deployment job execution
- EC2 instance configuration
- Amazon Linux 2023 administration
- Docker Engine installation
- SSH authentication
- GHCR image retrieval
- Docker container deployment
- Application health verification
The deployed application successfully returned:
{
"status": "healthy"
}with HTTP status:
200 OK
During development, several practical DevOps problems were encountered and resolved.
A local container initially failed because port 8000 was already occupied.
The issue was resolved by identifying the existing container and using an alternative host port where necessary:
docker run --rm -p 8001:8000 ...This reinforced the distinction between:
HOST PORT : CONTAINER PORT
The EC2 deployment involved troubleshooting:
- incorrect SSH usernames
- SSH key paths
- Windows PowerShell path handling
- public/private key authentication
- EC2 connectivity
- Security Group access
The final deployment successfully authenticated to the EC2 instance.
The EC2 instance initially did not have Docker available.
Docker was installed, started, enabled as a system service, and configured so the ec2-user could interact with Docker without requiring sudo for normal container operations.
The deployment was validated end-to-end by checking:
docker psthen:
docker logs cicd-fastapiand finally:
curl http://localhost:8000/healthThis confirmed that the container was running and the application was responding correctly.
This project demonstrates practical experience with:
- Git workflows
- GitHub repository management
- GitHub Actions
- CI/CD pipeline design
- automated testing
- Docker image creation
- Docker container management
- Docker Compose
- GitHub Container Registry
- AWS EC2
- Amazon Linux
- Linux administration
- SSH authentication
- GitHub Secrets
- deployment automation
- application health checks
- cloud deployment
- DevOps troubleshooting
More importantly, it demonstrates the ability to connect these technologies into a working deployment system rather than learning each tool in isolation.
Through this project, the following concepts were applied practically:
Automatically validate changes before they reach the deployment stage.
Automatically deploy successful builds to a cloud environment.
Separate:
Source Code
β
Build
β
Artifact
β
Deployment Environment
Instead of copying source code directly to EC2, the pipeline deploys the built Docker image.
Source Code
β
Docker Build
β
Container Image
β
GHCR
β
EC2
This provides greater consistency between build and deployment environments.
The next evolution of this project could introduce:
Terraform for automated AWS infrastructure provisioning.
AWS Application Load Balancer with TLS certificates.
CloudWatch metrics, logs, and alerts.
Docker image vulnerability scanning using tools such as Trivy or Docker Scout.
Replace the mutable:
:main
tag with versioned or commit-based tags.
For example:
:v1.0.0
or:
:commit-sha
Automatically restore the previous container image if deployment health checks fail.
Introduce:
- rolling deployments
- blue/green deployments
- deployment environments
- approval gates
Provision EC2, networking, security groups, and related infrastructure using Terraform.
Eventually migrate the application to Kubernetes.
This project progressed from a locally developed FastAPI application to a cloud-hosted, containerized application with an automated CI/CD pipeline.
The final workflow is:
Developer
β
Git
β
GitHub
β
GitHub Actions
β
pytest
β
Docker Build
β
GHCR
β
SSH
β
AWS EC2
β
Docker
β
FastAPI
β
Health Check
β
β
Deployment Verified
The key achievement is not simply deploying FastAPI to AWS.
It is demonstrating the ability to design and operate an automated software delivery pipeline connecting source control, testing, containerization, artifact management, cloud infrastructure, deployment automation, and post-deployment verification.
Paul Iyen
DevOps β’ Cloud β’ Cybersecurity
This project is part of a practical DevOps/Cloud engineering portfolio focused on building production-oriented infrastructure, automation, CI/CD pipelines, and cloud deployments.
CI/CD FastAPI AWS Deployment
Built an end-to-end CI/CD pipeline that automatically tests a FastAPI application, containerizes it with Docker, publishes the image to GitHub Container Registry, and deploys the application to AWS EC2 through SSH, with automated post-deployment health verification.
Core technologies: Python β’ FastAPI β’ pytest β’ Docker β’ GitHub Actions β’ GHCR β’ AWS EC2 β’ Linux β’ SSH β’ Git