DEV Community

Cover image for Deploy fast API to EC2
On-cloud7
On-cloud7

Posted on

Deploy fast API to EC2

FastAPI is a high-performance web framework for building APIs in Python. It's known for being fast, easy to learn, and efficient to code with. Here are some key points about FastAPI:

Performance: FastAPI is one of the fastest Python frameworks available, comparable to Node.js and Go in terms of speed.

Developer Friendly: FastAPI is designed to make development faster and reduce errors. It achieves this through features like automatic data validation and documentation.

Pythonic: FastAPI leverages standard Python-type hints for data validation, serialization, and deserialization, making it a natural fit for Python developers.

RESTful API focus: While FastAPI can be used for more general web development, it excels at building RESTful APIs, a common architecture for web services.

Implementation:

Here are the steps on how to deploy Fast Api on an EC2 instance in AWS:

  1. Create an EC2 Instance:
  2. Navigate to the EC2 service: Click on "Services" and select "EC2"

Image description

  1. Launch a new instance: Click on "Launch Instances"

Image description

Image description

  1. Choose an AMI: Select a suitable Amazon Machine Image (AMI).

Image description

  1. Choosing an instance type: Scroll down to select an EC2 instance type. An instance type is a particular configuration of CPU, memory (RAM), storage, and network capacity. AWS has a huge selection of instance types that cover many different workloads. Some are geared toward memory-intensive workloads, such as databases and caches, while others are aimed at compute-heavy workloads, such as image processing or video encoding. Amazon EC2 allows you to run 750 hours per month of a t2.micro instance under the AWS Free Tier. Select the t2.micro instance.

Image description

  1. Configuring an SSH key: You will see a details page on how to configure a key pair for your instance. You will use the key pair to SSH into your instance, which will give you the ability to run commands on your server. a.Open the key pair (login) section and choose Create new key pair for your instance. b. You will return to the Key pair (login) Section and you will find that the Key has been selected.

1.Configuring a security group and launching your instance: You need to configure a security group before launching your instance. Security groups are networking rules that describe the kind of network traffic that is allowed to your EC2 instance. You want to allow traffic to your instance:

•SSH traffic from all IP addresses so you can use the SSH protocol to log in to your EC2 instance and configure Fast API.

•HTTPS traffic from all IP addresses so that users can view your Fast API site Secured.

•HTTP traffic from all IP addresses so that users can view your Fast API site.

To configure this, select Allow SSH traffic from Anywhere and select Allow HTTPS & HTTP traffic from the Internet.

Image description

Launch: It is now time to launch your EC2 instance. a. Choose the Launch instance button to create your EC2 instance. b. Wait for instance to launch.
You have successfully launched your EC2 instance. Click on Open address to view the website.

Image description

Connect to Your EC2 Instance: •Use SSH to connect: Use a terminal or an SSH client like PuTTY to connect to your EC2 instance using its public IP address and the key pair you created earlier.

All the commands that are executed :

  1. sudo apt-get update
  2. sudo apt install -y python3-pip nginx 3.sudo vim /etc/nginx/sites-enabled/fastapi_nginx

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

  1. sudo service nginx restart
  2. git clone https://github.com/pixegami/fastapi-tutorial.git
  3. pip install -r requirements.txt
  4. python3 -m uvicorn main:app

INFO: Started server process [3324]
INFO: Waiting for application startup.
INFO: Application startup complete.
INFO: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)

Image description

Top comments (0)