AWS Builder Center

Deploying a Python FastAPI Application on AWS EC2

Learn how to deploy a production-ready Python FastAPI application on an Amazon EC2 instance. This tutorial covers launching an EC2 server, configuring the environment, running FastAPI with Uvicorn, and exposing the application through the internet.

Introduction

FastAPI is a modern Python framework for building high-performance APIs. AWS EC2 provides flexible virtual servers where you can deploy and run applications with full control over the underlying environment.
In this tutorial, we will deploy a simple FastAPI application on an EC2 instance.

Architecture

1
2
3
4
5
6
7
8
9
10
11
User
|
v
Internet
|
v
AWS EC2
|
+--> FastAPI
|
+--> Uvicorn

Prerequisites

You need:
  • An AWS account
  • Basic Python knowledge
  • An SSH client
  • AWS EC2 access

Step 1: Create an EC2 Instance

Open the AWS Console and navigate to EC2 → Launch Instance.
Use the following configuration:
1
2
3
Name: fastapi-server
AMI: Ubuntu Server 24.04 LTS
Instance type: t3.micro
Create or select an SSH key pair and launch the instance.

Step 2: Configure the Security Group

Allow the following inbound traffic:
1
2
3
SSH TCP 22 Your IP
HTTP TCP 80 0.0.0.0/0
Custom TCP 8000 0.0.0.0/0
Port 8000 is useful for testing the FastAPI application directly.
For production, it is better to place the application behind a reverse proxy such as Nginx and expose only ports 80 and 443.

Step 3: Connect to EC2

From your terminal:
1
ssh -i my-key.pem ubuntu@YOUR_EC2_PUBLIC_IP
Update the server:
1
2
sudo apt update
sudo apt upgrade -y
Install Python:
1
sudo apt install python3 python3-pip python3-venv -y

Step 4: Create the FastAPI Application

Create a project:
1
2
mkdir fastapi-app
cd fastapi-app
Create a virtual environment:
1
2
python3 -m venv .venv
source .venv/bin/activate
Install dependencies:
1
pip install fastapi uvicorn
Create main.py:
1
from fastapi import FastAPIapp = FastAPI()@app.get("/")def root(): return {"message": "FastAPI running on AWS EC2"}@app.get("/health")def health(): return {"status": "healthy"}

Step 5: Start the Application

Run:
1
uvicorn main:app --host 0.0.0.0 --port 8000
The application should now be accessible at:
1
http://YOUR_EC2_PUBLIC_IP:8000
Open the /docs endpoint to access FastAPI's interactive Swagger documentation:
1
http://YOUR_EC2_PUBLIC_IP:8000/docs

Step 6: Run FastAPI as a Service

Running Uvicorn directly in the terminal is not suitable for production because the application stops when the process terminates.
Create a systemd service:
1
sudo nano /etc/systemd/system/fastapi.service
Add:
1
2
3
4
5
6
7
8
9
10
11
12
[Unit]
Description=FastAPI Application
After=network.target

[Service]
User=ubuntu
WorkingDirectory=/home/ubuntu/fastapi-app
ExecStart=/home/ubuntu/fastapi-app/.venv/bin/uvicorn main:app --host 0.0.0.0 --port 8000
Restart=always

[Install]
WantedBy=multi-user.target
Enable and start it:
1
2
3
sudo systemctl daemon-reload
sudo systemctl enable fastapi
sudo systemctl start fastapi
Check the status:
1
sudo systemctl status fastapi

Step 7: Test the Deployment

Use:
1
curl http://YOUR_EC2_PUBLIC_IP:8000/health
Expected response:
1
2
3
{
"status": "healthy"
}

Production Improvements

A real production deployment should go beyond the basic setup.
Consider adding:
  • Nginx as a reverse proxy
  • HTTPS using Let's Encrypt
  • AWS CloudWatch for monitoring and logs
  • IAM roles instead of storing AWS credentials
  • Auto Scaling for high availability
  • Application Load Balancer for distributed traffic
  • CI/CD through GitHub Actions
  • Infrastructure as Code using Terraform or AWS CDK

Conclusion

Deploying FastAPI on EC2 provides complete control over the application environment while leveraging AWS infrastructure.
For small applications and prototypes, a single EC2 instance can be sufficient. As traffic and reliability requirements increase, the architecture can evolve toward Load Balancer → Auto Scaling → multiple EC2 instances → managed AWS services.
Any opinions in this article are those of the individual author and may not reflect the opinions of AWS.
Enjoyed reading this content? Let the author know!

Your likes, comments, shares, and saves help creators reach more builders.

Loading recommendations

Loading article