HNG DevOps Stage - 2
Deploying a FastAPI Application with CI/CD Pipeline

Search for a command to run...
Deploying a FastAPI Application with CI/CD Pipeline

No comments yet. Be the first to comment.
Terraform is a powerful tool for defining infrastructure as code (IaC). When managing multiple environments like dev, staging, and prod, organizing your Terraform code effectively is key. In this article, I’ll share how I manage multiple environments...
Using the AWS Management console

Number Classification API

Task - NGINX configuration

As part of my HNG DevOps Internship, I was tasked with deploying a FastAPI application with a Continuous Integration (CI) and Continuous Deployment (CD) pipeline while ensuring the application is served using Nginx as a reverse proxy. This blog post will guide you step by step on how to complete this project.
In this task, we:
Implement a missing API endpoint.
Set up a CI pipeline to run tests using pytest.
Configure a CD pipeline to deploy on merging into main.
Dockerize the FastAPI application.
Serve the application using Nginx.
Navigate to the given template repository on GitHub.
Click on Fork to create a copy under your GitHub account.
Clone your forked repository to your local machine:
git clone https://github.com/your-username/fastapi-project.git
cd fastapi-project
NOTE: THIS REPO IS NO LONGER UP.
We need to add an endpoint to retrieve a book by its ID.
routes.pyOpen the routes.py file.
Add the following function:
from fastapi import FastAPI, HTTPException
from models import books # Assuming books is a predefined list or database object
app = FastAPI()
@app.get("/api/v1/books/{book_id}")
async def get_book(book_id: int):
book = next((book for book in books if book["id"] == book_id), None)
if not book:
raise HTTPException(status_code=404, detail="Book not found")
return book
Save the file and test it locally using uvicorn:
uvicorn main:app --reload
Open http://127.0.0.1:8000/docs to test the new endpoint.
A CI pipeline will run tests automatically.
.github/workflows/ci.ymlCreate a .github/workflows/ci.yml file with:
name: CI
on:
pull_request:
branches:
- main
jobs:
test:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v3
- name: Set up Python
uses: actions/setup-python@v3
with:
python-version: "3.9"
- name: Install dependencies
run: |
pip install -r requirements.txt
- name: Run tests
run: pytest
Commit and push this file.
Open a Pull Request and verify the test job runs successfully.
.github/workflows/deploy.ymlAdd a new file .github/workflows/deploy.yml:
name: Deploy
on:
push:
branches:
- main
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v3
- name: Build and Push Docker Image
run: |
docker build -t my-fastapi-app .
docker run -d -p 8000:8000 my-fastapi-app
Push changes and merge a PR to test deployment.
DockerfileFROM python:3.9-slim
WORKDIR /app
COPY requirements.txt ./
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]
Build and test locally:
docker build -t fastapi-app .
docker run -p 8000:8000 fastapi-app
nginx.confCreate nginx.conf:
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://127.0.0.1:8000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
}
Run Nginx with Docker:
docker run --name nginx -v $(pwd)/nginx.conf:/etc/nginx/nginx.conf:ro -p 80:80 nginx
Push the Docker image to a registry (e.g., Docker Hub, AWS ECR, GitHub Container Registry).
docker tag fastapi-app your-username/fastapi-app:latest
docker push your-username/fastapi-app:latest
Deploy to a cloud provider or a self-hosted server with Docker and Nginx configured.
This project strengthened my CI/CD automation, Dockerization, and Nginx deployment skills.
I successfully:
Implemented a missing API endpoint
Set up a deployment pipeline.
Configured Nginx as a reverse proxy.
With each project I complete at HNG I learn a lot and I look forward to the next ones
#DevOps #FastAPI #Docker #GitHubActions #CI/CD #Nginx #CloudComputing