Machine Learning

AWS: Moving Fastapi app on EC2 in minutes

AWS is a famous cloud supplier that makes the submission and investing large programs. Knowing at least one cloud platform is an important skill for the software engineer and data scientist. Running a local system is not enough to make it useful in production – it must be sent to the server to access the last users.

In this lesson, we will travel by example to use the Fastapi app. While for example, the focus on network network communications are, the goals are more effective in other forms of applications.

Please note that the lesson does not cover the good of the AWS usage. Instead, the aim is to provide students in the introduction of an introduction to the submission of the application using EC2 conditions.

# 01. Example Creation

Navigate to the EC2 dashboard in the AWS service menu and select create a new example. This will open the page where we can describe parameters.

Choose the type of associated example. In this lesson, we will launch a very simple server for small technical needs, so t3.no It should be enough for our needs.

In its bowls, AWS uses SSH authentication. When you create a new example, it is necessary to create a new couple that will allow us to enter a local machine using the Shs Protocol. Click on Create a new key pair of key.

Give a name to the new key. We will not enter options that may occur here, so we will choose RSA as a type of pair key once.

Saving time, in our exhibition app will not be safe. In network settings, note all associated test boxes associated with SSH, HTTP, and HTTPS traffic.

It's good! By clicking LAUNCH statusAWS will form a new example.

After an example is made, a .em The file will be downloaded on your local machine. This file contains a private key that allows the SHS verification. As a good practice, keep this file in a safe place because the AWS does not give way to return if it is lost.

By opening the EC2 dashboard, you will see that the created example has an IP address associated with it. This IP is shown underneath the label “Public IPv4” address. For example, in the picture below, of course “16.16.202.153”. As soon as we move our system, it will be available from the browser using this IP address.

# 02. SSH connection

AWS offer many ways to make authenticity. To our, we will use SSH method.

In the status menu, click Encounter and select SSH Client from upper bar.

Open local terminal and, using the screenshot above as a reference, Copy and release the command # 3 (chmod 400 ".pem") and the command shown below “Example” label. Make sure your terminal guide is in place with a place where .em The key was followed in the previous step.

During SSH connection, terminal may arouse to continue. If possible, type “Yes”.

During this time, we have successfully connected to the local signal status in EC2. Any instructions included in terminal will now be made directly to the EC2 container.

# 03. Environmental configuration

After connecting in a position from the local grain, the next step is to renew the package manager and insert the Python and the NIGNX.

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

Redirect traffic to our app, we need to create a ninx configuration file. This file should be placed in a directory /etc/nginx/sites-enabled/ and can any of any custom name. We will add the following configuration to:

server {
  listen 80;
  server_name ;
  location / {
    proxy_pass 
  }
}

Basically, we specify that any foreign request sent to the EC2 address in Port Port 80 should be redirected by proxy applicable within the EC2 Container at the address http://127.0.0.1:8000. As a Memorial, this is the default HTPP address and port provided by the Fastapi.

To use these changes, we need to restart the NGX:

sudo service nginx restart

If we have a Fastapi server we would like to open, the easiest way would be to publish in GitHub and place the warehouse in EC2.

git clone  
cd 

Create and activate visual nature:

python3 -m venv venv
source venv/bin/activate

Enter the required Python requirements (which assumes the enclosure 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 for example.

Confirm using HTTP (not HTTPS) protocol. For example: http://16.16.202.153. Firewall can block your contact, but you must continue to open a Web page. Add /docs After the URL to open the Fast API Swagger.

Exercise

If you would like to use a Surapi example, you can create a simple place that includes a main. File and A Requirements.txt.

main.

from fastapi import FastAPI

app = FastAPI()

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

Requirements.txt

fastapi
uvicorn

To upload files

If you try to upload a file to the server and get the status of 413 with an error message “Error: Ask the business very big”It is possible because niginx is limited to the size of a high file that can be loaded. To solve this problem, go to the ninx configuration file and specify the file size allowed through A_max_body_body_bed Directory (to set up 0 shows not limited to installation file size):

server {
  listen 80;
  server_name ;
  location / {
    proxy_pass 
    client_max_body_size 0;
  }
}

After changing the configuration file, don't forget to restart the NGX.

Store

In this article, we have learned how we can promote effective EC2 example using a Fastapi server as an example. Although we did not follow the best habits of transport and safety, the main purpose of the article was to provide the small details of the beginners to launch their first server to AWS.

The next reasonable step in the AWs Study Roadmap will create many EC2 situations and connect them.

All photos unless noted in another way is a writer.

Connect with me

Source link

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button