AWS: Deploying a FastAPI App on EC2 in Minutes

From zero to EC2: easy steps to launching an AWS Instance The post AWS: Deploying a FastAPI App on EC2 in Minutes appeared first on Towards Data Science.

Apr 25, 2025 - 02:10
 0
AWS: Deploying a FastAPI App on EC2 in Minutes

Introduction

AWS is a popular cloud provider that enables the deployment and scaling of large applications. Mastering at least one cloud platform is an essential skill for software engineers and data scientists. Running an application locally is not enough to make it usable in production — it must be deployed on a server to become accessible to end users.

In this tutorial, we will walk through an example of deploying a FastAPI application. While the example focuses on core EC2 networking concepts, the principles are broadly applicable to other types of applications as well.

Please note that this tutorial does not cover best practices for AWS usage. Instead, the goal is to give readers a hands-on introduction to application deployment using EC2 instances.

# 01. Instance creation

Navigate to the Ec2 dashboard in the AWS service menu and choose to create a new instance. This will open a page where we can define instance parameters.

Select the corresponding instance type. In this tutorial, we will launch a very simple server with minimal technical requirements, so t3.nano should be sufficient for our needs.

For its containers, AWS uses SSH authentication. When creating a new instance, it is necessary to create a new key pair that will allow us to log in from the local machine using the SSH protocol. Click on Create new key pair.

Assign a name to the new key. We will not dive into the possible options here, so we will choose RSA as the key pair type and .pem as the private key file format.

To save time, in our demonstration application we will not worry about security. For the network settings, tick all the checkboxes corresponding to SSH, HTTP, and HTTPS traffic.

Great! By clicking Launch instance, AWS will create a new instance.

After the instance is created, a .pem file will be downloaded to your local machine. This file contains the private key that allows SSH authentication. As a good practice, store this file in a safe location because AWS does not provide a way to recover it if it is lost.

By opening the EC2 dashboard, you will notice that the created instance has an associated IP address. This IP is shown under the label “Public IPv4 address”. For example, in the image below, it is “16.16.202.153”. Once we deploy our application, it will be accessible from a browser using this IP address.

# 02. SSH connection

AWS offers several ways to perform authentication. In our case, we will use the SSH mechanism.

In the instance menu, click Connect and select SSH client from the top bar.

Open the local terminal and, using the screenshot above as reference, copy and execute command #3 (chmod 400 ".pem") along with the command shown below the “Example” label. Make sure your current terminal directory matches the location where the .pem key was downloaded in the previous step.

During the SSH connection, the terminal might prompt whether to proceed. If it does, type “yes”.

At this point, we are successfully connected from the local terminal to the EC2 instance. Any commands entered into the terminal will now be executed directly in the EC2 container.

# 03. Environment configuration

After connecting to the instance from the local terminal, the next step is to update the package manager and install Python along with Nginx.

sudo apt-get update
sudo apt install -y python3-pip nginx

To redirect traffic to our application, we need to create an Nginx configuration file. This file should be placed in the directory /etc/nginx/sites-enabled/ and can have any custom name. We will add the following configuration to it:

server {
  listen 80;
  server_name ;
  location / {
    proxy_pass http://127.0.0.1:8000;
  }
}

Basically, we are specifying that any external request sent to the EC2 instance’s IP address on the default port 80 should be redirected via a proxy to the application running inside the EC2 container at the address http://127.0.0.1:8000. As a reminder, this is the default HTTP address and port assigned by FastAPI.

To apply these changes, we need to restart Nginx:

sudo service nginx restart

If we have a FastAPI server that we would like to launch, the simplest way would be to publish it on GitHub and then clone the repository onto the EC2 instance.

git clone  
cd 

Create and activate a virtual environment:

python3 -m venv venv
source venv/bin/activate

Install the necessary Python requirements (assuming that the cloned repository contains a requirements.txt file):

pip3 install -r requirements.txt

Run the server:

python3 -m uvicorn :app

Open the browser and enter the IP address of the instance.

Make sure to use the HTTP (not HTTPS) protocol. For example: http://16.16.202.153. The firewall might block your connection, but you should proceed to open the web page. Add /docs after the URL to open Fast API Swagger.

Exercise

If you would like to run a FastAPI example, you can create a simple repository consisting of just a main.py file and a requirements.txt.

main.py

from fastapi import FastAPI

app = FastAPI()

@app.get("/")
def read_root():
    return {"message": "Hello, World!"}

requirements.txt

fastapi
uvicorn

Uploading files

If you try to upload a file to a server and receive a 413 status with the error message “Error: Request Entity Too Large”, it is likely because Nginx has a limit on the maximum file size that can be uploaded. To resolve this issue, go to the Nginx configuration file and specify the maximum allowed file size by using the client_max_body_size directive (setting it to 0 indicates no limits on input file sizes):

server {
  listen 80;
  server_name ;
  location / {
    proxy_pass http://127.0.0.1:8000;
    client_max_body_size 0;
  }
}

After changing the configuration file, do not forget to restart Nginx.

Conclusion

In this article, we have learned how to quickly create a running EC2 instance using a FastAPI server as an example. Although we did not follow the best deployment and security practices, the main goal of the article was to provide minimal information for beginners to launch their first server on AWS.

The next logical step in the AWS study roadmap would be creating multiple EC2 instances and connecting them to each other.

All images unless otherwise noted are by the author.

Connect with me