How to Deploy FastAPI on AWS EC2: Quick and Easy Steps!
Key Takeaways
The video demonstrates how to deploy a Python FastAPI application on an AWS EC2 instance using Ubuntu, configuring Nginx and Uvicorn, and accessing the API via HTTP protocol and Swagger documentation.
Full Transcript
i'm going to show you how to deploy or host your fast api python application on aws so that your api is accessible from the public internet we'll be deploying this api project which we created as part of the previous video tutorial if you didn't have the chance to work on that then don't worry because i've prepared a github link in the video description which you can just clone directly during this tutorial and of course if you have a different github link to your own fast api application then you can use that too so to get started you first need to log into your aws console so sign up for an account if you don't have one and log in and you should see something like this now there's a couple of different ways we can host a fast api application we can just host it using ec2 which is a virtual server so that's just going to be a computer that's running in the cloud 24 7 and then we'll pay by the hour but we can turn it on and off whenever we want that's what we're going to be doing today however we can also use aws lambda which is a serverless way to host the function that's pretty interesting as well but not something i'll cover today since we're using ec2 just go ahead and click on that service and that should bring you to your ec2 dashboard which looks something like this so we'll go up to this orange button over here and click launch instances and we get to configure a new instance to launch and here an instance is pretty much a virtual server so we'll get to put in a name and i'll just put fast api server and then we'll have to choose the machine image which is sort of like what os we want to launch it with for this one i'm going to use an ubuntu image and i'm going to use the latest long term supported version and for the instance type if you've just created a new account then i recommend you use t2 micro because it will be free otherwise you can check the pricing here on demand linux is just over one cent usd per hour and there's a couple of other cheaper ones as well like this nano one here but for this one i'm just going to leave it default at t2 micro which is good enough next there'll be an option to create a key pair login and this is going to be a file that you can use to ssh into your instance so that you can access the terminal in there and do things on it so we're going to want to do that and here the name can be anything i'm going to call it fast api key and once you've created the key you should see it appear in your downloads folder for the network settings we're going to want to allow ssh traffic from anywhere because we want to be able to connect to this instance or this host from our terminal and we will also want to allow http access from the internet we might allow https as well even though this fast api application we're doing will only support http at this point and that's pretty much it just double check your settings to make sure everything looks like this and once you are happy with that then you can click launch instance once your instance has launched you can go back to the ec2 dashboard and see it running like this if it's not running yet then it might still be pending so you just have to wait a little while and if you click on the instance id you can see the summary for this instance including its public ip address which is how we can access it publicly on the internet also it's public ipv4 dns which is sort of like a domain name that we can use to access it if we don't want to use the ip address directly and if i open this address you should see that it can't connect that's because even though the instance is running there's literally nothing on the instance right now it's just an empty ubuntu computer and the ip address doesn't work either and our goal is to put our fast api application onto this instance so that when we access these endpoints we can actually use the api so now the next step is to connect to our instance if you click on this connect button up here then it will take you to a page with a few different options normally you can use instance ec2 instance connect which if you press that it will bring up a window where you have the console accessible right here but i don't think this works here because this is an ubuntu image that doesn't have the ec2 instance connect support so we'll go back to this and then click ssh client if you're using mac or linux then these instructions should be fine otherwise i suggest you looking in the comments below for how to ssh from windows so remember that ssh pen file that we created earlier we're going to need to use that find that private key file and then change the permissions so that it's not publicly viewable and then we'll connect to the instance using this command so let's go ahead and do that now i'm now here in a directory uh where i've copied my pem file and i'm going to run changemod400 on this file and now i'm just going to copy this ssh command over from the example here which has already been pre-filled with our pem file name and the ec2 instance address so if i just run the command it will ask me a question i'll answer yes and now i'm connected to the instance so this terminal now represents the ubuntu os inside this machine here so the first thing i'm going to want to do in this machine is to update and install my dependencies so i'm going to start with sudo apt-get update and this will update all the repositories so we have access to all the latest software i'm going to make this terminal a bit bigger as well and then the next step is to sudo apt install and then i'm going to pass a flag so that it presses yes for all the questions and then i'm going to want to install python 3 pip and engine x like that so just a bit of background on the things we installed the ubuntu image doesn't actually come with pip so we had to install python pip so that we can use that to install our fast api uh module and engine x in case you're not familiar is something we can use uh it's like a web server software that allows us to connect our fast api application to ip addresses over here so that when people visit these public addresses they actually get routed to our fast api server and port running on this instance for nginx to be able to do that we are going to have to create a configuration file inside a predefined location so that when we start the nginx software it knows how to route the traffic so we're going to do that by creating a new file inside this directory so this is the directory that already exists with nginx by default and we're just going to go ahead and create a new file called fast api nginx which it will use to configure the server when we start it up so now we're going to have to fill out our nginx configuration file that's going to look like this we're going to be listening on port 80 which is the default http port so that's where our traffic is going to come in we're also going to need a server name which is just going to be this ip address here the public ipv4 address so just go ahead and copy that and then paste that in here and then to tell nginx where to wrap the traffic we're going to do location which is the route and here we're going to put in proxy pass and then the address of our fast api endpoint which to the perspective of this instance is going to be on localhost so http slash then this is the static address for the local host and by default it's port 8000 so it looks like that and we'll just close off our curly brackets and save that file and by the way you don't have to understand everything that this file is doing this is just something you have to configure once but if you do want to learn more about nginx and how it works uh you can go to the official documentation and read more about it there so i'm just going to save this and quit and now i'm going to restart my nginx server so that that configuration takes effect so just type sudo service nginx restart and you're not going to see any kind of output or anything here because it's going to be running in the background but if something was wrong in your configuration then you might get an error here so i suggest you just go back or go to the github and then double check that the configuration is correct now that the nginx configuration is finished i'm going to clone my fast api project into this post so let's go back to the project github and go to code and then clone and then there should be an https option so just copy that then go back to your terminal here and then type git clone and paste that github link github.com pixagonway fastapi tutorial.git or whatever your repository that you want to use is once you run that it should be cloning it into a directory called fast api tutorial which i can see is there so let's go into that directory and check that all our files are there so you can see this main py file is the fast api file that i want to run and it's this file here that we created as part of that previous video tutorial before we can run this we actually need to install the requirements so if i go into my requirements file there's just fast api and uv corn i don't know why the line is formatted like that but to install that we have to type pip3 install and then r requirements.txt so once the installation is finished successfully we should be able to start our fast api server i'm just going to clear that and then type python3 using the module uvicorn and we're going to run the main file and the application is just called app so now this looks familiar to when we were doing it on our local machine except that this uh this app is actually running in this amazon server so it's running on this localhost obviously if we click this we're not going to see it because this is our localhost is not running it and this is the local host for the machine but the nginx configuration we set up earlier is actually designed throughout the traffic from this server so these public ip addresses to this fast api application running locally in the machine and if you open this address uh here it's actually going to be a problem loading the page unable to connect and that's because by default it tries to take us to the https route which is on port 443 by default but we only exposed our api to port 80. so we can access that by actually just using the regular http protocol we just have to delete the s here and if we refresh that again we're going to see that our api can actually be called from this address so this is just the root path api that just returns this welcome message here and we can actually use all of these as well for example if i try list books i'll see it that returns an empty list that's because i haven't added any books to this yet but if i go ahead to the docs section which is just slash docs on the root path i'll be able to see the swagger documentation of all the api and then i can interact with it here so for example i can click this to add the book and i can try it out and maybe add a new book and then if i execute that through the web ui you can see the response and the crow request to interact with this api so that operation was successful and then if i go back to my api and list books again i will now be able to see that the new book i've added is there and this is a public ip address so you can use this from any computer or even your phone you'll be able to access this fast api application now so that's pretty much it for how you can deploy or host a fast api application on amazon ez2 instances before you wrap up though you should go back to your console and actually terminate this instance otherwise you might be charged for it running even if it's in the free tier you might forget about it and then eventually it might start billing you to your account so once you're done with the tutorial and you won't need the instance anymore just go ahead and click this check box or right click this instance and then hit terminate instance this will shut it down and eventually once you see the instance state become terminated like this other one i have here then you can be certain that the instance is no longer used and you would no longer be charged for it but yeah that's pretty much it i hope you were successful in deploying your fast api application uh i hope you found this helpful and thank you for watching
Original Description
In this video, I'll show you how to deploy a Python FastAPI application to an AWS EC2 instance, so that your API becomes publicly accessible on the internet.
👉 Previous FastAPI Tutorial: https://www.youtube.com/watch?v=34cqrIp5ANg
👉 Code: https://github.com/pixegami/fastapi-tutorial
👉 SSH to EC2 from Windows: https://stackoverflow.com/questions/5264945/ssh-to-ec2-linux-instance-from-windows
👉 AWS: https://aws.amazon.com/
👉 FastAPI: https://fastapi.tiangolo.com/
👉 FastAPI on AWS Lambda: https://youtu.be/RGIM4JfsSk0
00:00 Introduction
00:30 Launch an EC2 instance
04:06 Connect to your EC2 instance
05:35 Install pip and NGINX
06:20 Route API traffic with NGINX
09:06 Run the FastAPI server
11:39 Testing the API
13:01 Terminate the EC2 instance
#fastapi #aws #pixegami
🎨 Background vector created by GarryKillian
https://www.freepik.com/vectors/background
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from pixegami · pixegami · 15 of 60
1
2
3
4
5
6
7
8
9
10
11
12
13
14
▶
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
How to Build an AWS Lambda Function in Python in Just 7 Minutes!
pixegami
AWS CDK Tutorial: Deploy a Python Lambda Function using AWS
pixegami
I used GPT-3 to Write Poetry • Is AI the Future of Creative Writing?
pixegami
Create NFT Generative Art with Python! (Full Tutorial)
pixegami
Build an AI-driven SaaS Application: FULLSTACK Tutorial with Python, React, and AWS
pixegami
NextJS and TailwindCSS: How to Build a Portfolio Site from Scratch
pixegami
Python Web Scraping Tutorial • Step by Step Beginner's Guide
pixegami
Build Wordle in Python • Word Game Python Project for Beginners
pixegami
How to create 1000+ unique NFT-style images (like Cryptopunk) | Python Tutorial
pixegami
Top 10 Python Modules 2022
pixegami
How to Send SMS Text Messages with Python & Twilio - Quick and Simple!
pixegami
How To Write Unit Tests in Python • Pytest Tutorial
pixegami
How to Style Your React Landing Page with Tailwind CSS
pixegami
FastAPI Python Tutorial - Learn How to Build a REST API
pixegami
How to Deploy FastAPI on AWS EC2: Quick and Easy Steps!
pixegami
PyScript • How to run Python in a browser
pixegami
My Custom Ubuntu Linux Terminal with Themes and Plug-ins 💻
pixegami
Deploy FastAPI on AWS Lambda ⚡ Serverless hosting!
pixegami
NextJS Firebase Auth Tutorial • How to Authenticate Users for Your App
pixegami
AWS Lambda Python functions with a database (DynamoDB)
pixegami
How To Build a CRUD (TO-DO) App on AWS using FastAPI and Python
pixegami
How to Make a Discord Bot with Python
pixegami
How To Use GitHub Copilot (with Python Examples)
pixegami
PyTest • REST API Integration Testing with Python
pixegami
Python Beginner Project: Build a Caesar Cipher Encryption App
pixegami
Decorators in Python: How to Write Your Own Custom Decorators
pixegami
NextJS 13 Tutorial: Create a Static Blog from Markdown Files
pixegami
Exploring ChatGPT for Coding and Business ✨ 8 Real Examples!
pixegami
How I Would Learn Python (if I had to start over) • A Roadmap for 2023
pixegami
Build an AI Pokemon Generator with Python and Midjourney
pixegami
Why You Should Learn Python in 2023 (as your first programming language)
pixegami
ChatGPI API in Python ✨ How to Build a Custom AI Chat App
pixegami
Learn Python • #1 Installation and Setup • Get Started With Python!
pixegami
Learn Python • #2 Variables and Data Types • Python's Building Blocks
pixegami
Learn Python • #3 Operators • Add, Subtract and More...
pixegami
Learn Python • #4 Conditions • If / Else Statements
pixegami
Learn Python • #5 Lists • Storing Collections of Data
pixegami
Learn Python • #6 Loops • How to Repeat Code Execution
pixegami
Learn Python • #7 Dictionaries • The Most Useful Data Structure?
pixegami
Learn Python • #8 Tuples and Sets • More Ways To Store Data!
pixegami
Learn Python • #9 Functions • Python's Most Important Concept?
pixegami
Learn Python • #10 User Input • 4 Ways To Get Input From Your User
pixegami
Learn Python • #11 Classes • Create and Use Classes in Python
pixegami
Learn Python • #12 Final Project • Build an Expense Tracking App!
pixegami
Stripe & Firebase Tutorial • Add Payments To Your NextJS App
pixegami
How To Use GitHub Actions • Automate Your AWS Deployments
pixegami
How to Run a Python Docker Image on AWS Lambda
pixegami
My MacOS Terminal Setup for HIGH Productivity
pixegami
Host a Python Discord Bot on AWS Lambda (Free and Easy)
pixegami
Python FastAPI Tutorial: Build a REST API in 15 Minutes
pixegami
Pydantic Tutorial • Solving Python's Biggest Problem
pixegami
How to Get Started with AWS • Crash Course
pixegami
Python Requests Tutorial: HTTP Requests and Web Scraping
pixegami
Amazon Bedrock Tutorial: Generative AI on AWS
pixegami
How to Publish a Python Package to PyPI (pip)
pixegami
Langchain: The BEST Library For Building AI Apps In Python?
pixegami
RAG + Langchain Python Project: Easy AI/Chat For Your Docs
pixegami
Python Dataclasses: Here's 7 Ways It Will Improve Your Code
pixegami
Build a Custom AI RPG Game with OpenAI GPTs
pixegami
Create a Custom AI Assistant + API in 10 Mins
pixegami
More on: AI Systems Design
View skill →Related AI Lessons
⚡
⚡
⚡
⚡
Docker Explained: From “What Even Is This” to Deploying a Full-Stack App
Medium · DevOps
I Used to Pay for Cloud Servers. Then I Found a Way to Run One Free, 24/7
Medium · AI
KEDA 2026: Event-Driven Autoscaling Patterns That Shrank Our AWS Bill by 40%
Medium · DevOps
AWS CloudFormation and CDK Explained: Infrastructure as Code on AWS
Medium · DevOps
Chapters (8)
Introduction
0:30
Launch an EC2 instance
4:06
Connect to your EC2 instance
5:35
Install pip and NGINX
6:20
Route API traffic with NGINX
9:06
Run the FastAPI server
11:39
Testing the API
13:01
Terminate the EC2 instance
🎓
Tutor Explanation
DeepCamp AI