AWS: Deploying a FastAPI App on EC2 in Minutes

-

AWS is a well-liked cloud provider that permits the deployment and scaling of huge applications. Mastering a minimum of one cloud platform is a necessary skill for software engineers and data scientists. Running an application locally isn’t enough to make it usable in production — it should be deployed on a server to turn into accessible to finish users.

On this tutorial, we’ll walk through an example of deploying a FastAPI application. While the instance focuses on core EC2 networking concepts, the principles are broadly applicable to other forms of applications as well.

Please note that this tutorial doesn’t cover best practices for AWS usage. As a substitute, the goal is to present readers a hands-on introduction to application deployment using EC2 instances.

# 01. Instance creation

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

Select the corresponding instance type. On this tutorial, we’ll launch a quite simple server with minimal technical requirements, so  must be sufficient for our needs.

For its containers, AWS uses SSH authentication. When making a recent instance, it’s mandatory to create a brand new key pair that can allow us to log in from the local machine using the SSH protocol. Click on .

Assign a reputation to the brand new key. We won’t dive into the possible options here, so we’ll select RSA as the important thing pair type and .pem because the private key file format.

To avoid wasting time, in our demonstration application we won’t worry about security. For the network settings, tick all of the checkboxes corresponding to SSH, HTTP, and HTTPS traffic.

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

After the instance is created, a  file will probably be downloaded to your local machine. This file incorporates the private key that enables SSH authentication. As an excellent practice, store this file in a protected location because AWS doesn’t provide a option to recuperate it whether it is lost.

By opening the EC2 dashboard, you’ll notice that the created instance has an associated IP address. This IP is shown under the label . For instance, within the image below, it’s . Once we deploy our application, it would be accessible from a browser using this IP address.

# 02. SSH connection

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

Within the instance menu, click  and choose  from the highest bar.

Open the local terminal and, using the screenshot above as reference, copy and execute command #3 (chmod 400 ".pem") together with the command shown below the  label. Ensure your current terminal directory matches the placement where the  key was downloaded within the previous step.

Through the SSH connection, the terminal might prompt whether to proceed. If it does, type .

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

# 03. Environment configuration

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

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

To redirect traffic to our application, we’d like to create an Nginx configuration file. This file must be placed within the directory /etc/nginx/sites-enabled/ and may have any custom name. We are going to add the next configuration to it:

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

Mainly, we’re specifying that any external request sent to the EC2 instance’s IP address on the default port 80 must be redirected via a proxy to the appliance running contained in the EC2 container on the address http://127.0.0.1:8000. As a reminder, that is the default HTTP address and port assigned by FastAPI.

To use these changes, we’d like to restart Nginx:

sudo service nginx restart

If we’ve a FastAPI server that we would love to launch, the easiest method could be to publish it on GitHub after which 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 mandatory Python requirements (assuming that the cloned repository incorporates a  file):

pip3 install -r requirements.txt

Run the server:

python3 -m uvicorn :app

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

Ensure to make use of the HTTP (not HTTPS) protocol. For instance: http://16.16.202.153. The firewall might block your connection, but you must proceed to open the net page. Add /docs after the URL to open Fast API Swagger.

Exercise

In the event you would love to run a FastAPI example, you’ll be able to create an easy repository consisting of only a  file and a .

from fastapi import FastAPI

app = FastAPI()

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

fastapi
uvicorn

Uploading files

In the event you attempt to upload a file to a server and receive a 413 status with the error message , it is probably going because Nginx has a limit on the utmost file size that could be uploaded. To resolve this issue, go to the Nginx configuration file and specify the utmost allowed file size by utilizing the  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, don’t forget to restart Nginx.

Conclusion

In this text, we’ve learned find out how to quickly create a running EC2 instance using a FastAPI server for example. Although we didn’t follow the perfect deployment and security practices, the primary goal of the article was to offer minimal information for beginners to launch their first server on AWS.

The following logical step within the AWS study roadmap could be creating multiple EC2 instances and connecting them to one another.

Connect with me

ASK ANA

What are your thoughts on this topic?
Let us know in the comments below.

0 0 votes
Article Rating
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments

Share this article

Recent posts

0
Would love your thoughts, please comment.x
()
x