DEV Community

Cover image for 🧩 Step-by-Step EC2 Deployment: FastAPI, MongoDB, and NGINX Setup
Mai Chi Bao
Mai Chi Bao

Posted on

🧩 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!

Top comments (1)

Collapse
 
mrzaizai2k profile image
Mai Chi Bao

🚀 "I followed your steps, and my app is live! Do you have recommendations for monitoring and scaling EC2 instances?"