https://medium.com/@vanyamyshkin/deploy-python-fastapi-for-free-on-aws-ec2-050b46744366
Log into your AWS account and create an EC2 instance (t2.micro), using the latest stable
Ubuntu Linux AMI.
SSH into the instance and run these commands to update the software repository and install our dependencies.
sudo apt-get update
sudo apt install -y python3-pip nginxClone the FastAPI server app (or create your main.py in Python).
git clone https://github.com/pixegami/fastapi-tutorial.gitAdd the FastAPI configuration to NGINX's folder. Create a file called fastapi_nginx (like the one in this repository).
sudo vim /etc/nginx/sites-enabled/fastapi_nginxAnd put this config into the file (replace the IP address with your EC2 instance's public IP):
server {
listen 80;
server_name <YOUR_EC2_IP>;
location / {
proxy_pass http://127.0.0.1:8000;
}
}
Start NGINX.
sudo service nginx restartStart FastAPI.
cd fastapi-tutorial
python3 -m uvicorn main:appUpdate EC2 security-group settings for your instance to allow HTTP traffic to port 80.
Now when you visit your public IP of the instance, you should be able to access your API.