Logo

dev-resources.site

for different kinds of informations.

๐Ÿงฉ Step-by-Step EC2 Deployment: FastAPI, MongoDB, and NGINX Setup

Published at
1/7/2025
Categories
aws
mrzaizai2k
fastapi
docker
Author
mrzaizai2k
Categories
4 categories in total
aws
open
mrzaizai2k
open
fastapi
open
docker
open
Author
10 person written this
mrzaizai2k
open
๐Ÿงฉ Step-by-Step EC2 Deployment: FastAPI, MongoDB, and NGINX Setup

Key Takeaways

By the end of this guide, you will:

  • Understand how to set up an EC2 instance on AWS.
  • Deploy a machine learning app using Docker.
  • Configure IP and domain settings for your app.

This post is designed to be concise and to the point for clarity.

Project Overview

I will be deploying my Multilanguage Invoice OCR app onto AWS EC2. This app provides a powerful solution for extracting text and data from invoices in multiple languages using OCR (Optical Character Recognition). It consists of multiple services, including:

  • Frontend: A web-based user interface built with NGINX for easy access and interaction.

  • Backend: The core logic implemented using FastAPI for handling requests and processing data.

  • Database: MongoDB for storing processed invoice data and user information.

  • OCR Engine: The app utilizes PaddleOCR, a deep learning-based OCR library for multilingual text extraction.

The goal is to deploy this app with all its components containerized using Docker and running on an AWS EC2 instance, ensuring scalability, security, and ease of management.


Setting Up an EC2 Instance

What is an EC2 Instance?

An EC2 instance is a virtual server in the AWS cloud. Learn more about EC2 instances here.

Ways to Deploy a Website on AWS

  • Static websites: Use S3 buckets (for blogs, simple sites).
  • Dynamic websites: Use EC2 instances (like this project). EC2 can be applied to host scalable websites.
  • Serverless apps: Use AWS Lambda for lightweight apps.

Explore other methods here.

Steps to Configure an EC2 Instance

  1. Follow this guide for creating an EC2 instance.
  2. Key Configurations for the App:
    • Security Group: Allow access from all IPs (enable ports 80 and 443).
    • Spot Instance: Use for cost savings (optional).
    • Request Type: Set to persistent (restarts instance after failure).
    • Interrupt Behavior: Choose Stop.
    • Operating System: Ubuntu 22.04.
    • Instance Type: Choose t3.xlarge (not t2, due to Paddle error).
    • Memory: Allocate 16GB.

Launch the Instance

  1. Click Launch instance.
  2. Wait 1-2 minutes for initialization.
  3. Connect to the instance following the guide linked above.

Installing Docker on EC2

Docker is essential for containerizing and running the app. Follow this setup guide or use the steps below:

# Clone the repository
git clone https://github.com/mrzaizai2k/multilanguage_invoice_ocr.git
cd multilanguage_invoice_ocr
chmod +x ./scripts/setup-docker.sh
./scripts/setup-docker.sh
Enter fullscreen mode Exit fullscreen mode

Verify Installation

After running the setup, disconnect and reconnect to your instance. Check if Docker was installed correctly:

docker --version
docker-compose --version
Enter fullscreen mode Exit fullscreen mode

Running the App

This app involves:

  • Web service: NGINX
  • Database: MongoDB
  • Core: FastAPI

Prepare the Configuration

Create an .env file and configure it:

touch .env
nano .env
Enter fullscreen mode Exit fullscreen mode

Paste the following into .env:

OPENAI_API_KEY=
EMAIL_USER=
EMAIL_PASSWORD=
SECRET_KEY=
ALGORITHM=HS256
ACCESS_TOKEN_EXPIRE_MINUTES=30
CLIENT_MAX_BODY_SIZE=5M
SERVER_IP= # Use public IP or domain (e.g., xyz.com)
DEBUG=True # Set to True for development purposes
Enter fullscreen mode Exit fullscreen mode

Launch the App

Run the app using Docker Compose:

docker-compose up -d
Enter fullscreen mode Exit fullscreen mode

This may take 30 minutes to an hour. Verify the services are running:

docker ps -a
docker logs
Enter fullscreen mode Exit fullscreen mode

You can now access the app using the public IP.

Image description


Setting Up HTTPS

Allocate an Elastic IP

Elastic IP ensures your IP doesn't change after instance restarts. Learn more here.

Steps:

  1. Allocate an Elastic IP.
  2. Assign it to your running EC2 instance.

Buy and Link a Domain

For a domain, I recommend Hostinger. Follow this guide to map the domain to your Elastic IP.

Configure HTTPS

Follow the setup guide for enabling HTTPS using Certbot.


Conclusion

This was a quick yet comprehensive guide on deploying a FastAPI-based app on AWS EC2. If you need further help, check out my other posts:

Let me know if you have questions in the comments below!

fastapi Article's
30 articles in total
Favicon
Protect Your APIs from Abuse with FastAPI and Redis
Favicon
Making a Todo API with FastAPI and MongoDB
Favicon
The Core of FastAPI: A Deep Dive into Starlette ๐ŸŒŸ๐ŸŒŸ๐ŸŒŸ
Favicon
Python FastAPI quickstart in uv
Favicon
Automating Word Document Creation with Python and FastAPI (Using python-docx-template)
Favicon
๐ŸŒ๐Ÿ’ฑ New Project: Currency Exchange API
Favicon
Getting Started with FastAPI
Favicon
[HANDS ON] Service Discovery in Prometheus
Favicon
Python's Unstoppable Rise, Dominating The Modern Backend Environment
Favicon
Boost Your App Performance by 10x with Redis Caching!
Favicon
Supercharge Your API Performance with Asynchronous Programming in FastAPI
Favicon
Mastering Real-Time AI: A Developerโ€™s Guide to Building Streaming LLMs with FastAPI and Transformers
Favicon
Integrating LangChain with FastAPI for Asynchronous Streaming
Favicon
Serverless FastAPI Development: Building Player FC API on AWS
Favicon
FastAPI + Uvicorn = Blazing Speed: The Tech Behind the Hype
Favicon
Why is My Multi-Threaded API Still Slow?
Favicon
๐Ÿงฉ Step-by-Step EC2 Deployment: FastAPI, MongoDB, and NGINX Setup
Favicon
๐Ÿš€ Building a User Management API with FastAPI and SQLite
Favicon
Simplify Authentication with FastAPI!
Favicon
Fastapi
Favicon
The Secret Behind FastAPIโ€™s Speed
Favicon
Maximize Your FastAPI Efficiency: Blazingly Fast Implementation of Caching and Locking with py-cachify
Favicon
Understanding REST APIs: A Beginnerโ€™s Guide
Favicon
Mastering Python Async IO with FastAPI
Favicon
Is Flask Dead? Is FastAPI the Future?
Favicon
WSGI vs ASGI: The Crucial Decision Shaping Your Web Appโ€™s Future in 2025
Favicon
Fina Categorization API made publicly free
Favicon
Self-Correcting AI Agents: How to Build AI That Learns From Its Mistakes
Favicon
How to Build Smarter AI Agents with Dynamic Tooling
Favicon
๐Ÿš€ Validating User Input with FastAPI: An Example with Custom Validators

Featured ones: