Deploy FastAPI on AWS Lambda | In 9 MINUTES

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey friends and in this video we are going to go over how to deploy a fast API application on AWS lambda's step by step alright so once you open up your Visual Studio code the very first thing we need to do is install fast API uvicorn and Mangum and Mangum is going to be the Handler that we're going to be using on our AWS lambdas so we can successfully deploy this application so let's start by saying pip install fast API uvicorn and Mangum now once we install these dependencies we need to create a requirements.txt file and we can do this by saying pip freeze greater than sign and then typing in requirements.txt when you do this you'll see that it's going to create a requirements.txt file and now when you open this up you're going to see a bunch of dependencies that come automatically with fast API and all we really need is a fast API in this man gum everything else can be completely erased and that's because we are going to grab all the dependencies that we've already installed and stick it on our fast API lambdas now the next thing we need to do is go ahead and just create our main.pi file which is our main fast API application file where we can say from Fast API import fast API and then we want to say fun from Mangum import Mangum we then want to say app equals fast API we then need to create a Handler so we can say Handler equals man gum where we can pass in our fast API app and then we want to say at app.get where inside I'm just going to have a empty slash where we are going to create a new python function of async Def hello where we just return a dictionary of message and I'm going to say hello from coding with Ruby all right so this is everything we need to run a fast API application now just to make sure everything works I'm going to say uvicorn main colon app dash dash reload we can see that everything is working as intended and if we open this up we can see that we get our hello API that when executed we get a message of hello from coding with Roby so everything is perfect from there now the next thing that we need to do to get this ready is we need to type a couple commands to get this ready to be deployed on AWS lambdas now I have them on another screen just so I don't mess up but what we need to do is jump into our terminal and type pip 3 install Dash T dependencies Dash R requirements dot text now when you click this we're going to get a new dependencies directory that has all of the dependencies that we installed into our fast API application and we need to bundle this with our main.pi file so we can successfully add it to a AWS Lambda so then the Lambda can take over everything when running the application so after typing in that command of creating this dependencies package we need to come here and also type in this new command of parentheses CD dependencies so we're going to be going into our dependencies directory we want to zip and create a new AWS Lambda artifact.zip file Dash R so we're going to include the requirements and then Dot when you do this you will see that we now have a AWS Lambda artifact zip file in our directory now for the very last command we need to say zip AWS Lambda artifact.zip Dash U main.pi what this is doing is we are creating a new AWS Lambda artifact where we're adding the dash u means update we are adding and updating the zip file with our main.pi file so let's go ahead and add that and we can see that it's added our main.pi file to our AWS Lambda artifact now what we need to do is head over to your AWS console if you don't already have an AWS account you need to go ahead and create this but this Lambda is going to be totally free if you are brand new to AWS using the free tier so let's go ahead and type into the search bar Lambda we want to create a function and here we can name it whatever we want so I'm going to name this fast API coding with Ruby we want to change the runtime environment from node.js to python we can leave the architecture at this [Music] and now let's go down to our advanced settings and let's enable the function URL so use function URLs to assign https endpoints to our Lambda functions and then where it says auth type for this example go ahead and change your auth type to none this will add a new rule but what we're essentially saying is anyone can access your AWS Lambda now the reason I'm doing this is so we don't have to authenticate and use authorization to make sure that this Lambda is successfully working so I'm going to say none so anyone has access to this Lambda and then I'm going to say create function now one powerful thing about AWS is it creates these serverless functions extremely fast this should only take a couple seconds to maybe a few minutes alright so once you create a AWS Lambda it'll give you a URL immediately so we can say open up a new tab where we get a hello from Lambda now what this essentially is saying is automatically in our Lambda when you create a new AWS Lambda you get this Json that just returns a 200 and a body of hello from Landa from Lambda so that's exactly what it's printing right here so if you scroll a little bit down you'll see the file so now what we want to do is upload our fast API application to be at AWS Lambda so let's go ahead and just say upload from we want to say a dot zip file click upload and what we want to go to now is our directory where we just built the application and click our AWS Lambda artifact once you upload this go ahead and click save and this will have all of the dependencies that we bundled together using those three commands so our lambdo will successfully be able to work so the very first thing we can see is that it says success but if we go back to our Lambda and we refresh we're going to get an internal server error now the reason we're getting an internal server error is due to our Handler so remember we downloaded Mangum to control our fast API Handler but we're getting a default Lambda function dot Lambda Handler so what we need to do here is go ahead and click edit and this is on our home page I'm just scrolling down click edit under runtime settings and instead of Lambda function dot Lambda Handler we need to specify where our Handler lives within our fast API application so we can say main because main is our main file and our Handler are man gun was inside main so we can say main dot Handler now what this is saying is inside our main file we have our Handler variable which is controlling our app so if we go ahead and just save this wait for the updating to happen it's successfully updated and now if we refresh the page we're going to get message hello from coding with Roby on a https route using AWS lambdas now this is unbelievable how fast you can do this and I hope you enjoyed this video on how to deploy fast API on AWS lambdos and I will see you in the next video
Info
Channel: Eric Roby
Views: 15,206
Rating: undefined out of 5
Keywords: aws, aws lambda, aws lambda tutorial, aws lambda function, what is aws lambda, aws lambda example, aws lambda python, cloud computing, aws lambda for beginners, fastapi aws lambda, fastapi tutorial, serverless, fastapi aws, fastapi lambda, fastapi, python fastapi, fastapi deployment, programming, fastapi project, fast api
Id: 7-CvGFJNE_o
Channel Id: undefined
Length: 9min 2sec (542 seconds)
Published: Sun Jul 09 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.