Deploying A Fastapi Application On Aws Ec2 With Ci Cd Using Github
In this blog post, we will walk through the process of deploying a FastAPI application on an AWS EC2 instance with Nginx as a reverse proxy, while leveraging GitHub Actions for Continuous Integration and... This will ensure that every change pushed to the main branch is automatically deployed to the server. By the end of this tutorial, you will have a fully functional, continuously deployed FastAPI application running on AWS. We will be working with a FastAPI Book API and following these key steps: The application already provides endpoints for creating, updating, and deleting books, but we need to add an endpoint to retrieve a book by its ID. Modify api/routes/books.py by adding the missing endpoint:
This post documents my experience building an automated CI/CD pipeline for a FastAPI application. The goal was simple: push code to GitHub, and it automatically deploys to AWS EC2. What seemed straightforward turned into a multi-hour debugging session that taught me valuable lessons about Docker, async/sync database drivers, and SSH authentication. The pipeline architecture follows a standard CI/CD flow: The application uses two containers orchestrated by Docker Compose: Key Design Choice: The depends_on with service_healthy ensures the database is fully ready before the application starts.
This prevents connection errors during startup. My initial docker-compose.yaml only defined the application service. I assumed the database would be handled separately, but the application expected a container named db on the same Docker network. This repository showcases how to integrate FastAPI, Docker, and Poetry to build a production-ready Machine Learning Operations (MLOps) project. The project is designed to deploy machine learning models via a FastAPI microservice and automate the deployment process using GitHub Actions for a seamless CI/CD pipeline, deploying the containerized app to AWS EC2. This project is based on the tutorial provided by The Halftime Code, which walks through the entire process from project setup to deployment on AWS.
Train a sample machine learning model (RandomForest on Iris dataset) and save it to the models directory: Access the API at http://127.0.0.1:8000. You can test the prediction endpoint using curl: Tired of manually deploying your applications? What if you could push code to GitHub, run tests automatically, and deploy updates to your live server—without lifting a finger? That’s exactly what I did with my FastAPI project using CI/CD.
I’ll walk you through how I set up a Continuous Integration (CI) pipeline to automatically test my code... Continuous Deployment (CD) pipeline to push updates to an AWS EC2 instance. This guide explains how to set up a CI/CD pipeline to automatically deploy a Dockerized FastAPI app (or any containerized app) to an EC2 instance using GitHub Actions. Create a new SSH key pair (locally or in CI): You can have multiple lines in authorized_keys. Then log out and log back in (or reboot):
Go to your GitHub repo → Settings → Secrets and variables → Actions, and add: In this post, I’ll share my journey of deploying a FastAPI application to AWS EC2 with full HTTPS support. This was part of a seminar assignment where the goal was to integrate a MySQL RDS database, automate deployment using GitHub Actions, and secure the server using Nginx and Certbot. The journey wasn’t smooth—I encountered rate limits, missing configurations, and environment variable hell. Here is how I solved them. The system follows a standard CI/CD and containerized deployment pattern:
We use docker-compose.yaml to define our services. A key part is setting up Nginx and Certbot to share volumes for the ACME challenge. To automate SSL certificate issuance and renewal, we set up a shared volume between Nginx and Certbot. Deploying a FastAPI application with Nginx on AWS EC2 can be challenging for beginners. This guide will walk you through setting up an EC2 instance, installing necessary dependencies, configuring Nginx as a reverse proxy, and setting up a CI/CD pipeline with GitHub Actions. Before you begin, ensure you have the following:
Run the following commands on the instance: Test if Nginx is serving the FastAPI app: Go to your repository settings on GitHub and add the following secrets under Settings -> Secrets and Variables -> Actions:
People Also Search
- Deploying a FastAPI Application on AWS EC2 with CI/CD Using GitHub ...
- Building a CI/CD Pipeline for FastAPI Application with GitHub Actions ...
- Deploy a FastAPI application using Docker and GitHub Actions for CI/CD ...
- GitHub - ivesfurtado/mlops-fastapi-microservice: MLOps project using ...
- Deploying a FastAPI App with CI/CD: GitHub Actions, Docker, Nginx & AWS EC2
- CI/CD for Deploying Docker Application on EC2 with GitHub Actions
- Deploying a FastAPI Application with CI/CD: A DevOps Journey
- Building a Secure HTTPS CI/CD Pipeline for FastAPI on AWS EC2
- Deploying a FastAPI Application on AWS EC2 with Nginx and CI/CD
- How I Deployed My First FastAPI App on AWS EC2 (Step-by-Step for ...
In This Blog Post, We Will Walk Through The Process
In this blog post, we will walk through the process of deploying a FastAPI application on an AWS EC2 instance with Nginx as a reverse proxy, while leveraging GitHub Actions for Continuous Integration and... This will ensure that every change pushed to the main branch is automatically deployed to the server. By the end of this tutorial, you will have a fully functional, continuously deployed FastAP...
This Post Documents My Experience Building An Automated CI/CD Pipeline
This post documents my experience building an automated CI/CD pipeline for a FastAPI application. The goal was simple: push code to GitHub, and it automatically deploys to AWS EC2. What seemed straightforward turned into a multi-hour debugging session that taught me valuable lessons about Docker, async/sync database drivers, and SSH authentication. The pipeline architecture follows a standard CI/C...
This Prevents Connection Errors During Startup. My Initial Docker-compose.yaml Only
This prevents connection errors during startup. My initial docker-compose.yaml only defined the application service. I assumed the database would be handled separately, but the application expected a container named db on the same Docker network. This repository showcases how to integrate FastAPI, Docker, and Poetry to build a production-ready Machine Learning Operations (MLOps) project. The proje...
Train A Sample Machine Learning Model (RandomForest On Iris Dataset)
Train a sample machine learning model (RandomForest on Iris dataset) and save it to the models directory: Access the API at http://127.0.0.1:8000. You can test the prediction endpoint using curl: Tired of manually deploying your applications? What if you could push code to GitHub, run tests automatically, and deploy updates to your live server—without lifting a finger? That’s exactly what I did wi...
I’ll Walk You Through How I Set Up A Continuous
I’ll walk you through how I set up a Continuous Integration (CI) pipeline to automatically test my code... Continuous Deployment (CD) pipeline to push updates to an AWS EC2 instance. This guide explains how to set up a CI/CD pipeline to automatically deploy a Dockerized FastAPI app (or any containerized app) to an EC2 instance using GitHub Actions. Create a new SSH key pair (locally or in CI): You...