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:
- Create an EC2 Instance:
- Navigate to the EC2 service: Click on "Services" and select "EC2"
- Launch a new instance: Click on "Launch Instances"
- Choose an AMI: Select a suitable Amazon Machine Image (AMI).
- 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.
- 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.
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.
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 :
- sudo apt-get update
- 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;
}
}
- sudo service nginx restart
- git clone https://github.com/pixegami/fastapi-tutorial.git
- pip install -r requirements.txt
- 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)
Top comments (0)