Deploy fast API to EC2
Tags: fastapi, python, aws, ec2
Published: 2024-04-13 04:41:14

**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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z1c7zz3d3bcbzrblys55.png)

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

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vfnfkqcuxn9mgxh7v43w.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lwh3bosqshw77cpi0ab5.png)

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

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/707z94vca3h6ayr9zmso.png)

5. 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zqqtf1mm8v5m61j5xbnu.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vcpy1srcmuchx08wk10e.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gqotsxcvjnrfp7zu5mf1.png)

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;
        }
}        


 5. sudo service nginx restart
 6. git clone https://github.com/pixegami/fastapi-tutorial.git
 7. pip install -r requirements.txt
 8. 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0ograviwhwlj7cq3gqb5.png)