AWS Deployment's - vimlesh-verma16/Notes GitHub Wiki

Deploy FastAPI on AWS with nginx

Link to deployment Video: https://www.youtube.com/watch?v=SgSnz7kW-Ko

create main.py 
create venv name(env)
pip install -r requirement.txt

Link to AWS Steps:
create instance
ubuntu image
key_pair:

select create key_pair
name fastapi and download the .pem file

Security Groups

image

Launch instance

Click on connect -> select ssh client
image

✅ Steps to Connect to AWS EC2 Using PowerShell on Windows 10

C:\WINDOWS\system32> Move-Item -Path "C:\Users\YourName\Downloads\fastapi_key.pem" -Destination "C:\Users\YourName\.ssh\"
PS C:\WINDOWS\system32> icacls C:\Users\Vimlesh\.ssh\fastapi_key.pem /inheritance:r
processed file: C:\Users\Vimlesh\.ssh\fastapi_key.pem
Successfully processed 1 files; Failed processing 0 files
PS C:\WINDOWS\system32> $env:UserName
Vimlesh
PS C:\WINDOWS\system32> icacls C:\Users\Vimlesh\.ssh\fastapi_key.pem /grant:r "Vimlesh:R"
processed file: C:\Users\Vimlesh\.ssh\fastapi_key.pem
Successfully processed 1 files; Failed processing 0 files
PS C:\WINDOWS\system32> ssh -i C:\Users\Vimlesh\.ssh\fastapi_key.pem [email protected]
>>Welcome to Ubuntu 24.04.1 LTS (GNU/Linux 6.8.0-1021-aws x86_64)
ubuntu@ip-172-31-90-199:~$ sudo apt-get update
ubuntu@ip-172-31-90-199:~$ sudo apt install -y python3 nginx
ubuntu@ip-172-31-90-199:~$ sudo vim /etc/nginx/sites-enabled/fastapi_nginx
server {
        listen 80;
        server_name 52.90.78.51;
        location / {
                  proxy_pass http://127.0.0.1:8000;

                   }
 }
ubuntu@ip-172-31-90-199:~$ sudo nano /etc/nginx/sites-enabled/fastapi_nginx 
ubuntu@ip-172-31-90-199:~$ sudo service nginx restart

Clone code from Github to EC2 server in nginx

ubuntu@ip-172-31-90-199:~$ git clone https://github.com/vimlesh-verma16/Instagram_clone_FastAPI.git
ubuntu@ip-172-31-90-199:~$ cd Instagram_clone_FastAPI
ubuntu@ip-172-31-90-199:~/Instagram_clone_FastAPI$ ls
Notes  README.md  __pycache__  db  main.py  requirement.txt
ubuntu@ip-172-31-90-199:~/Instagram_clone_FastAPI$ sudo apt install python3.12-venv -y
ubuntu@ip-172-31-90-199:~/Instagram_clone_FastAPI$ python3 -m venv env
ubuntu@ip-172-31-90-199:~/Instagram_clone_FastAPI$ source env/bin/activate
(env) ubuntu@ip-172-31-90-199:~/Instagram_clone_FastAPI$ pip install -r requirements.txt
(env) ubuntu@ip-172-31-90-199:~/Instagram_clone_FastAPI$ python3 -m uvicorn main:app

After doing all this steps.
Now, open http://54.21.32.23 (your EC2 Public IPv4) in a browser.
❌ DO NOT use https://, as Nginx is listening on port 80 (HTTP).

Successfully deployed the FastAPI on EC2 !!!!!


Details info of above steps with errors for future references.

Move Your .pem Key to a Secure Location

Move your key to your user directory (optional but recommended).
image

Set Correct File Permissions for the Key

Windows doesn’t support chmod 400, but you can restrict access using:
image

Connect to the EC2 Instance

Replace Public-DNS with your actual AWS instance address.

image

Now verify with the ip address
image