How to Deploy FastAPI on AWS EC2: Quick and Easy Steps!

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
I'm going to show you how to deploy or host your FastAPI 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 FastAPI 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 FastAPI 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 "FastAPI 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 FastAPI 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 FastAPI 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. Our goal is to put our FastAPI 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 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 ".pem" 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 where I've copied my ".pem" file and I'm going to run "changemod 400" on this file. 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. 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 NGINX 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 FastAPI module. And NGINX, in case you're not familiar, is something we can use. It's like a web server software that allows us to connect our FastAPI application to IP addresses over here so that when people visit these public addresses they actually get routed to our FastAPI 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 ".fastapi_nginx" which it will use to configure the server when we start it up. 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 FastAPI endpoint Which to the perspective of this instance, is going to be on localhost so HTTP/... 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. 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 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 FastAPI project into this host, 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/pixegami/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 "fastapi-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 FastAPI 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 FastAPI and "uvicorn". 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 FastAPI server. I'm just going to clear that and then type "python3" using the module "uvicorn" and we're going to run the "main.py" 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 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 "localhost" for the machine. But the NGINX configuration we set up earlier is actually designed to route the traffic from this server from these public IP addresses to this FastAPI application running locally in the machine. And if you open this address 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. 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 "/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. 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 CURL 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 FastAPI application now. So that's pretty much it for how you can deploy or host a FastAPI application on Amazon EC2 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 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. TGhat's pretty much it, I hope you were successful in deploying your FastAPI application. I hope you found this helpful and thank you for watching!
Info
Channel: pixegami
Views: 35,191
Rating: undefined out of 5
Keywords: fastapi, python, tutorial, aws, ec2, coding, learning, programming, api, tech, pixegami, fastapi tutorial, fastapi python tutorial, fastapi python, python fastapi, fastapi example, fastapi create project, python tutorial, fastapi rest api, fastapi aws, aws fastapi, deploy fastapi aws, host fastapi aws, rest api, fastapi tutorial python, fastapi ec2, ec2 fastapi, aws ec2 python, python rest api aws, aws rest api python, python server aws, fastapi server aws, fastapi server ec2
Id: SgSnz7kW-Ko
Channel Id: undefined
Length: 13min 47sec (827 seconds)
Published: Thu May 05 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.