Simple Serverless FastAPI with AWS Lambda

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
welcome to dead bear code i'm sean byer and in this video we're going to convert a traditional standing server python fast api application into a serverless lambda with aws using api gateway to handle routing all the requests to this lambda proxy to do this we're going to be using a python package called mangum it's pretty cool and it's really simple to set up it only takes about two lines of code i'll be posting a link to the blog version of this video in the video description so just in case i need to make any updates later you can go follow that link and go to the blog post which i'll be able to update over time a couple of requirements for this is you need to have python installed virtual m and an aws account first things first let's go ahead and set up our virtual environment using virtual m if you don't know what this is or how to install it on your computer you can go ahead and watch this video here i've also put a link in the description to that video so now we're ready to set up fast api so if you come over to the fast api site you'll notice this tutorial user guide here so we're just going to be referencing this so if you go ahead and open that up and take a look it's going to say it's all fast api adding this all flag is going to give it all the optional dependencies and then we're also going to need uvicorn which is going to be the server that's going to be running the actual application so let's go ahead and pip install fast api all great okay then we're going to go ahead and pip install uvicorn now if we go back to the documentation the next step is going to be to add our main.pi file and we're just going to copy this straight from here into our code editor project going to make a new file call it main dot pi and just go ahead and paste that in there then next all we need to do is run yupicorn main app dash dash reload and that's gonna start our fast api and we can check to see if that's running and local host and you can see that it is now working so if we go back to our code here you'll see that we're importing fast api creating the fast api instance and then this is our route basically so this is just the root slash route and it's just going to return this message hello world which is if you go into the browser that's what we're doing here another way you can test to see if this is working is by checking the built in documentation that fast api comes with so if you add slash docs at the end of the url you'll get this pre-built swagger documentation you can come here see your route you can try it out hit execute and you'll get the same thing it'll just give you a little bit more information here so let's go ahead and create another route to test this out and see if we can get that working as well so over in our main.pi we're going to add another route and we're going to call it slash users and this is just going to send back the this a similar message just saying get users go back to the swagger docs refresh the page we'll see now we see our users route we can try that one out as well and that looks to be working great so right now you can see we have a pretty basic app everything is in one file and that isn't super realistic for production level applications so what we want to do is go ahead and restructure our application so we can have it a little bit more organized and allow our app to scale as it grows in size and we add more complex business logic so again if we refer back to the fast api documentation and i i highly suggest you come check this out it's pretty in-depth and thorough they do a really good job and it's kind of interesting because they'll walk you through this like whole tutorial of it it's not just you know super dry typical docs so if you come back and you'll see over here it'll say bigger applications multiple files and we're basically just going to go based off of the suggestions here in the documentation so if i go back to our code and we are going to create a app folder and move our main.pi into the app folder and we're also going to add a init dot pi and what the init.pi file does is it tells python to treat these as a module that it can import from any of the other files in the projects and yeah you'll see kind of how this works in a little bit okay and then inside the app we're also going to create another directory and call it api and then another directory inside of that and we're going to call this api v1 and so this is just so if you have any other drastic changes you can increment it up to v2 and then while still be able to maintain functionality for the previous versions and then we're going to go ahead and add init.pi files in both of those and then inside the api v1 we're going to do one more directory and we're going to call this endpoints and then you guessed it another init.pie now inside of our endpoints file we're going to create a users.pi and then we're going to move our users route into this file here so there's a few other things we need to do in order to get this to work so let's go ahead and set those up the first one is to import the api router so now instead of app we're going to want to put router and then we want to create another file called api dot pi and we're going to add in this snippet here and so this is where the modules come from we're basically importing users as a module and we're telling python to look into the endpoints folder and then we're telling fast api to include the user's endpoints on the router so that way we have access to them through the main.pi file and you'll notice this prefix and tags parameters here the prefix is so that if we put the slash users here now when we go back to the users.pi we can just change this to just the forward slash and then any route we put in the users.pi it's going to automatically have the slash users in front of it followed by our new route so in this case router dot get forward slash is it's just slash users and then say we were to create a new route that we wanted users and we wanted to call it slash new then it would look like this slash users slash new and so that just makes things a little bit cleaner a little bit easier to read and less code to write now the tags parameter you'll see in a second when we look at the swagger docs again and it'll show up there tagging all the routes that we have made on the users.pi file it looks like i accidentally put my api dot pi file in the endpoints folder and i actually want it in my api directory now since we updated our router we need to go back to the main.pi and update it here as well and have it include the router that we just created we want to import the router and so we're just going to go from the root of the application so we want to go into the app folder then go into the api folder then the api v1 and get the api and then from the api file we just want to import the router and we're going to call it api router finally we need to tell our fast api application to include our new router by adding an app.include router passing in the router and then this prefix is going to just add slash api version 1 onto the beginning of every route and instead of main app we're going to say app.main and that looks like it's working and now we have our users endpoint here's our tag users showing up here and that looks like it's working great now we have our fast api app set up so the next thing is going to be converting it to a lambda so the first thing we need to do is install mangum and then update our app to have a handler that the lambda will be able to call and in order to do this we are going to come back to our terminal and we're going to run pip install mangum first we want to import mangum so from mangum import mangum capital m and then we want to add our handler and we are going to wrap the app in mangum and then let's just go ahead and double check that our application's still working locally and everything looks good now our fast api is ready to be invoked via lambda and so now we need to provision our aws resources to be able to handle these requests so the first thing we need to do is package our fast api lambda into a zip file that we can upload to s3 the dependencies have to be accessible at the root level and the lambda is going to call this handler here it's not going to know to look in the env then go into lib python 3.7 site packages to get all these dependencies right so when we package this zip file we're going to want to take all the dependencies out and put them in the root of the directory of the zip file that we're creating we're going to cd into the site packages folder and then we're going to zip up everything in the site packages and put it back out into our the root of our project and to do that we're going to run zip dash r9 and then follow the path back to the root of our project and now that's done and then we're going to go ahead and go back to the root of our project now you'll see we have a function.zip so now we have the dependencies we also need the rest of our application so let's go ahead and zip up the rest of that and put add it into the function.zip and to do that we're going to run zip dash g the path to the location of our zip file dash r and app which is the name of the folder in our project that we want to add to the zip file now that our lambda function is packaged and ready to go let's go ahead and go to s3 and we're going to click create bucket and we're going to call this server list fast api go ahead and create it click on your new s3 bucket and click upload and we're going to go and add our zip file and click upload suite that's ready to go next we need to make our lambda so now let's go over to lambda which can be found under compute and lambda and you'll see we don't have any lambdas yet so let's go ahead and we're going to create function you want to take note that whatever region you're in you create the api gateway and the lambda in the same region the s3 buckets are global so you'll notice that if we go back to s3 instead of showing the region up here it says global so just for the lambda and the api gateway make sure that they're those are created in the same region let's go back to create function and we're just going to author this one from scratch and call it serverless fast api lambda and we want this python 3.7 and this will default to create a new role with basic lambda permissions and this is what we want so go ahead and leave that and click create function our lambda is now created and it's going to give us a default lambda here in the code and what we want to do is come over to actions and we're going to upload a file from s3 and so now we need to get this s3 link url in order to have our lambda upload it from the s3 bucket we just created so if we go back to s3 click on our serverless fast api bucket and click this check box this window is going to pop up and you can click copy path and then you can come back and paste that into this box here next we need to make sure that we update the handler because by default it's going to be pointing to lambda function dot lambda underscore handler and ours isn't that so let's go ahead and update that to what ours is which should be app.main.handler and this is saying find the app folder in the root of our project and then find the main dot pi file and then grab the handler that's in the main.pi file and click save now we can test to make sure that our fast api lambda is working correctly by running this test it's going to prompt us to create a new test instead of hello world we're going to search for api gateway aws proxy because that's what we're going to be implementing on the api gateway and then we're going to go ahead and give it a name we need to update two things in order for this test to work properly it's this path saying path to resource and ours is just going to be the route and then we're going to check instead of a post we're going to just do a get so now that we have those two updated create our test and we have our test selected if we click test again it's going to run our fast api lambda is working correctly and that is great last but not least we need to set up api gateway and if you go under networking and content delivery you'll find api gateway and it's going to give you a few options of the type of apis you can create today we're going to be using the rest api and we don't want the private this will make it to where your api can only be accessed within a vpc within your aws account and so we want this to be able to be accessed outside of a vpc so let's click build and when you create an api it's going to prompt you to be if you want to import an existing api from swagger documentation we're not going to do this today we're going to go ahead and just create our own api so click new api and give it a name i'm going to call this one serverless fast api and we're just going to leave this as regional create api the first thing we need to create is our root proxy method and so this will handle any requests to the root url so we're going to click actions create method and here we're going to select any because we want all requests no matter if they're a get a post a patch whatever we're going to forward all requests to the lambda proxy so hit the check mark next it's going to want to prompt you to set up at your integration point this is where api gateway is going to do with the request that comes in so we want to send it to a lambda function but we want to use the proxy integration so we're going to check this then we want to tell it which lambda function that we want to send it to and that's going to be the one we just created which is our serverless fast api lambda and go ahead and click save and this is just prompting you saying that it's going to automatically give api gateway permission to invoke that lambda function so go ahead and click ok next we need to handle any of the other sub routes so if we wanted like slash users or slash posts you know we want to be able to handle those requests as well and so we're going to create another resource so this time click the actions menu come click create resource and then it's going to be the same thing we want to configure the proxy so we're going to check this box here and this is going to automatically populate the resource name and the resource path so we can go ahead and leave that as is click create resource and then again we're going to tell it which lambda function that we want to forward the request to so again we're going to forward these to the same lambda and go ahead and leave everything else as default and click save same thing permissions and you'll see now we have this api gateway set up to where it's taking in any requests and the integration request is sending it to this lambda proxy so the last thing we need to do is deploy the api so it's publicly accessible so if you click the actions menu again come down to deploy api it's going to ask you what stage we don't have one yet so let's create one just going to call this one dev and click deploy now it's going to take you to the stage dashboard and show you the url to your api gateway so if we go ahead and click on that what do you know we have hello world here and it is working and by default api gateway is going to tack on the name of the stage to your url so this is technically the root proxy that we are hitting and so if we add slash api slash v1 users now we're getting our get users endpoint that we also set up so that's it we've successfully created our api gateway and we're routing all our requests to our fast api lambda proxy this was a way how i figured out how to get this to work if you have any other cool ideas or tips go ahead and let me know in the comments if you like this video and you found it useful do me a favor and leave a like and subscribe to this channel for more coding tutorials
Info
Channel: DeadbearCode
Views: 13,145
Rating: undefined out of 5
Keywords: developer, coding, software engineering, dead bear code, deadbearcode, fastapi, serverless, serverless fastapi, lambda, aws lambda, fastapi lambda
Id: 6fE31084Uks
Channel Id: undefined
Length: 17min 4sec (1024 seconds)
Published: Sun Sep 06 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.