Deploying your first FastAPI application on Amazon EC2
Hi there, in this article, we are going to learn how to deploy a fastAPI application to Amazon EC2 Ubuntu server with NGINX.
Amazon EC2 (Elastic Compute Cloud) is a web service provided by Amazon Web Services that offers resizable virtual servers in the cloud. It allows developers, businesses, and organizations to run applications without needing to invest in physical hardware, providing flexibility, scalability, and cost-effectiveness.
NGINX is a high-performance web server, reverse proxy, and load balancer designed to efficiently handle client requests, serve static content, and secure applications. When hosting FastAPI on EC2, NGINX is used to act as a reverse proxy, forwarding client requests to the FastAPI app while improving security by hiding the backend server. It also provides load balancing to distribute traffic across multiple instances, SSL/TLS termination for secure HTTPS communication, efficient static content delivery to reduce backend load, and enhanced performance by handling more concurrent connections than Uvicorn alone, making the application more scalable and robust.
STEP 1: Sign in your AWS Account
STEP 2: Launch an EC2 Instance
- Select the ubuntu server, and any corresponding AMI(Preferably the default)
Select the desired instance type(t3.micro is recommended for development environments)
click “create new key pair”
- click create pair
Network Settings
Make sure the “Allow HTTP traffic” and “Allow HTTPs traffic” are ticked to allow you to access the app from the internet if needed.
then go ahead to launch the instance.
STEP 3: Connect to the EC2 Instance
Now your instance is up and running, select the instance and click “connect”.
Navigate to the “SSH Client” tab
Now copy the string, under “Example”, its the one you are going to use for connecting to the server through ssh.
Open a terminal of your choice
Navigate to the directory where you saved your key pair file
paste the code you copied above and click enter.
STEP 4: Configure the nginx and port redirection
- Navigate to the nginx predefined location to create an nginx configuraton file.
sudo vim /etc/nginx/sites-enabled/fastapi_nginx
- Put the following code in the above opened file
server {
listen 80;
server_name 13.245.235.61;
location / {
proxy_pass http://127.0.0.1:8000;
}
}
replace the server_name value with your instance’s public IP
Kindly note the above configuration is only allowing for HTTP connections, if you want to add HTTPs, add the port number 443.
Now restart the nginx server
sudo service nginx restart
STEP 5: Clonning your fastapi app into the server
From github, clone your repository into the server.
If you dont have any fastapi repository available, you can use the following for testing
git clone https://github.com/judahtc/test-fastapi-ec2.git
- navigate to the created code folder
cd test-fastapi-ec2
STEP 6: Installing NGINX and python libraries
sudo apt install -y python3-pip nginx python3.12-venv
- Creating a python virtual environment
python3 -m venv venv
- Activate the virtual environment
source venv/bin/activate
- install fastapi directly or from your requirements file
pip install fastapi[standard]
OR
pip install -r requirements.txt
Now start your uvicorn server
uvicorn main:app --port 8000
Now you are good to go, Paste your public IP into any web browser and your rest endpoints will appear.