{"id":24625348,"url":"https://github.com/shreyxnsh/deploy-fastapi-aws-ec2","last_synced_at":"2026-05-06T02:39:13.266Z","repository":{"id":273753011,"uuid":"920769152","full_name":"shreyxnsh/deploy-fastapi-aws-ec2","owner":"shreyxnsh","description":"List of cmds and instructions to deploy your FastAPI project to AWS EC2 instance using nginx","archived":false,"fork":false,"pushed_at":"2025-01-22T18:34:27.000Z","size":2,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-01-22T19:36:44.863Z","etag":null,"topics":["aws","aws-ec2","devops","fastapi","nginx"],"latest_commit_sha":null,"homepage":"","language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/shreyxnsh.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2025-01-22T18:34:13.000Z","updated_at":"2025-01-22T18:35:06.000Z","dependencies_parsed_at":"2025-01-22T19:36:47.569Z","dependency_job_id":"be1114b5-a8f1-4a88-912a-7d5583e0032c","html_url":"https://github.com/shreyxnsh/deploy-fastapi-aws-ec2","commit_stats":null,"previous_names":["shreyxnsh/deploy-fastapi-aws-ec2"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shreyxnsh%2Fdeploy-fastapi-aws-ec2","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shreyxnsh%2Fdeploy-fastapi-aws-ec2/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shreyxnsh%2Fdeploy-fastapi-aws-ec2/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shreyxnsh%2Fdeploy-fastapi-aws-ec2/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/shreyxnsh","download_url":"https://codeload.github.com/shreyxnsh/deploy-fastapi-aws-ec2/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244394468,"owners_count":20445634,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["aws","aws-ec2","devops","fastapi","nginx"],"created_at":"2025-01-25T04:33:05.493Z","updated_at":"2026-05-06T02:39:08.232Z","avatar_url":"https://github.com/shreyxnsh.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"# Host FastAPI to AWS EC2\n\nThis is a simple example FastAPI application that pretends to be a bookstore.\n\n# Deployment\n\nLog into your AWS account and create an EC2 instance (`t2.micro`), using the latest stable\nUbuntu Linux AMI.\n\n[SSH into the instance](https://aws.amazon.com/blogs/compute/new-using-amazon-ec2-instance-connect-for-ssh-access-to-your-ec2-instances/) and run these commands to update the software repository and install\nour dependencies.\n\n```bash\nsudo apt-get update\nsudo apt install -y python3-pip nginx\n```\n\nClone the FastAPI server app (or create your `main.py` in Python).\n\n```bash\ngit clone https://github.com/{your-repo}.git\n```\n\nAdd the FastAPI configuration to NGINX's folder. Create a file called `fastapi_nginx` (like the one in this repository).\n\n```bash\nsudo vim /etc/nginx/sites-enabled/fastapi_nginx\n```\n\nAnd put this config into the file (replace the IP address with your EC2 instance's public IP):\n\n```\nserver {\n    listen 80;   \n    server_name \u003cYOUR_EC2_IP\u003e;    \n    location / {        \n        proxy_pass http://127.0.0.1:8000;    \n    }\n}\n```\n\n\nSave the configuration\n\n```\nCtrl + C --\u003e :wq\n```\n\n\nStart NGINX.\n\n```bash\nsudo service nginx restart\n```\n\nGo to project folder\n\n```bash\ncd fastapi-tutorial\n```\n\nCreate venv and install packages\n\n```bash\npython3 -m venv .venv\nsource .venv/bin/activate\npython3 -m pip install -r requirements.txt\n```\n\nStart the server\n\n```bash\nuvicorn app.main:app --reload --host 0.0.0.0 --port 8000\n```\n\nUpdate EC2 security-group settings for your instance to allow HTTP traffic to port 80.\n\nNow when you visit your public IP of the instance, you should be able to access your API.\n\n# Automate server start and stop using instance states\n\nBy using these commands your FastAPI application will turn on automatically whenever the instance is started and stops whenever the instance is stopped - Automation 101\n\nCreate a service\n\n```bash\nsudo nano /etc/systemd/system/fastapi.service\n```\n\nAdd these configurations: \n\n```bash\n[Unit]\nDescription=FastAPI Application\nAfter=network.target\n\n[Service]\nUser=ubuntu\nWorkingDirectory=/home/ubuntu/v1-fast-api\nExecStart=/usr/bin/python3 -m uvicorn app.main:app --host 0.0.0.0 --port 8000\nRestart=always\n\n[Install]\nWantedBy=multi-user.target\n\n```\n\nReload the systemd daemon\n\n```bash\nsudo systemctl daemon-reload\n```\n\nStart the service immediately\n\n```bash\nsudo systemctl start fastapi.service\n```\n\nEnable the service to start on boot\n\n```bash\nsudo systemctl enable fastapi.service\n```\n\nCheck the status\n\n```bash\nsudo systemctl status fastapi.service\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fshreyxnsh%2Fdeploy-fastapi-aws-ec2","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fshreyxnsh%2Fdeploy-fastapi-aws-ec2","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fshreyxnsh%2Fdeploy-fastapi-aws-ec2/lists"}