[Python] Build a CRUD Serverless API with AWS Lambda, API Gateway and a DynamoDB from Scratch

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what's up everyone welcome back to another episode of aws tutorial and today's video is going to be a complimentary piece of an old video that i have where i talked about how to build a serverless api with a lambda function and an api gateway in that video i wrote lambda code in node.js and i got a lot of requests asking me to do a similar video in python because python is a very popular language and a lot of people use it for software development nowadays so i figured it might be a good idea to do a similar video in python because it will be beneficial to a lot of people in that old video i also talked about what a serverless api is and its advantages over non-serverless apis so if you haven't watched it i i'll include a link down below so you can check that out if you're interested but in today's video i'm just gonna focus on the implementation part of it so without further ado let's get to it all right so right now i'm on the homepage of the aws console and step number one of the process is to create a dynamodb table to host our data for the cloud api so i'm going to type in dynamo open that into a new tab and then i'm going to hit create table give it a name i'm just going to call it product inventory and then for the partition key i'm just going to use product id and then for the data type i'm just keep it as string but you can change its number if you want to and then for the settings i just use all the default settings all right so this is done and the table is active and now we can move on to step number two which is to create a lambda function for our api but before we can do that we need to create an item row so that the lambda function can use it to access our dynamodb table so i'm gonna type in iam click on rows and then create a new row we're gonna choose lambda because the lambda function is going to use it and now we're going to attach two policies to it one is the cloud watchful permission so that we can look at logs for the api requests and then the other one is dynamo full access because we wanted to access the dynamodb to do modifications and creations so i'm going to type in cloud watch full access check it and then we're going to type in dynamodb for access as well hit next and now you can see that we have two policies selected for this role and now i'm gonna give it a name i'm just call it serverless api raw hit create all right so that is done and now we can move on to create a lambda function for our api code so i'm going to type in lambda hit create function i'm going to select from scratch give it a name i'll just call it serverless api lambda and we're going to choose python and 3.9 is the most updated version that is available and then under permissions we're going to choose the row that we just created i believe it's that one and then hit create okay so it's done before we move on i'm gonna change two things in the setting so i'm gonna go to configurations and then under general configuration i'm gonna change the memory setting and the timeout setting because the default setting is only three seconds and 128 megabytes typically 128 is enough um i'm not sure what you're going to do with the api um so let's change it to 500 this should be more than sufficient and then one minute for the timeout hit save and now we're ready to move on to the next step which is to create the api gateway to connect everything so i'm going to type in api gateway okay so this is the home page for api gateway and you have four options right here we're gonna build the restful api so we're gonna choose this make sure that this is the one that you choose so that you can follow along so i'm gonna click build rest api hit ok i'm gonna choose new api give it a name i would call it serverless api keep it as original create okay so now we're gonna create some resources for our path that the api is gonna have so the first one is going to be health we're gonna check enable gateway course or api get recourse create and then go back to the root we are gonna create two more the first one is products where we do operations on a single product and then lastly we're gonna have a products path where we do operations for multiple products hit create and then under health we're gonna have a get method that is just for the user to check whether the api is healthy or not we're gonna choose lambda we're going to check use lambda proxy integration this is very important make sure you choose this option here and in here we're going to choose the lambda function that we just created earlier which is that then hit save hit okay and then next under products we're gonna have a get method where we just get a single product by an id we're going to do the same thing here lambda function use lambda proxy integration and then select the lambda function that we just created and then next we're going to have a post method under product where we want to create a single product or insert into the database the next one is the patch method where we want to modify a single item in our database and then finally we're going to have a delete item on the product that we want to delete a single item from our database and then finally under the products path we are going to have a get method where we want to get all the products in our database okay so this is done and now remember the last step that we have to do in order to make the api active is to deploy that and we're going to create a new stage we're just going to call it pride and hit deploy and now we have a new stage called pride and this is the end point for the api and one thing i want to mention is that right now the api is open to the world anyone can access it if they notice endpoint uil i have a separate video that talks about how to do authentications on an api in order to access it using api keys i'll include a link down below so if you're interested you can check that out but in this video i'm just not going to get into the details of it and now we are ready to move on to the final step which is to write the code for the api you can write the code in the lambda console if you want but i just prefer an ide for that because it's just easier for the syntax so i'm just going to write that in pycharm okay so right now i have pycharm open amt folder called tutorial in my laptop and step number one is to create a python file for our lambda function and the name has to be lambda underscore function dot pi because that's what we have configured as our handler so i'm going to do new python file lambda function and the handler function that's going to be called by the lambda function is called lambda handler so i'm going to define the function first that takes in an event object and a context object i do pass for now so this is basically the entry point uh for our lander functions whenever you hit it it's gonna get into this function to call other methods and now we're gonna import all the modules that we want what we need for the api the first one is both three and then we also need the json module and then we're gonna use the login module as well and in here we're just gonna define our logger let's go locking dot get logger and then we're gonna set the locker level we're just gonna do info and then next we're gonna define our dynamodb table the table name which is i think we call it product inventory let's double check go back to the pencil i can just copy this okay that's right and then we're going to define our dynamo clients we're going to get that from our border 3 that we defined earlier i'm going to do resource and then dynamodb and the next we're going to get the table and then we're going to define our methods the first one is called get method all caps and then we're going to define the paths we have the first one is the health okay and now we are ready to define our handler function we can remove this so the first thing we're going to do is we're going to log the request event to see how the request looks like and then we can extract the http method from our event object and then we can extract the path from our event object as well and now we're ready to handle all the different scenarios and the first scenario is that if the http method is equal to our get method and the path is equal to the health check path we are going to set our response to be 200 and let's define this method here so it's gonna take in a status code and then a body which is optional so we're gonna set it to be not none as optional so we're going to set the response that has a attribute of status code which is the status code that's passed in and then for the headers we're gonna have two attributes inside one is called content type application json and then the next one is pretty important if you plan to integrate that with the front end that has a different host name so this would allow cross region access and then after that we're going to check if the body is not none that means the user pass in a custom object we're going to set the response body to be the body and then finally we're gonna return the response so one more thing that we need to do with the body here is so since object that we get from dynamodb is in decimals which is not supported by the default json encoder so right here we have to define our custom encoder for decimal numbers so we're going to do that in a separate file so under our tutorial file tutorial folder we're going to create a new python folder called custom encoder and in here we're going to define a class just call it custom encoder but you can call it whatever you want that takes in a json encoder let me fix the import here and then we're going to define a default function that takes in an object and we're going to check if the object is an instance of a decimal and if it is we are going to return a float version of the decimal object and if not we can just return a default value of the object which is the original and that is our custom encoder and now we are ready to use it let's let's import that and then we're going to use it in our function here because i'm in code okay that is it so we finished one scenario so the second scenario is that if the http method is a get method and the path is a product path let's fix the name here that means we want to get a single item so we're going to set the response to be equal to get product we're going to define this method later it meant we're going to take the product id from our query string parameters then we're going to pass in a attribute called product id and then the next scenario is when the http method is equal to get and the path is equal to the product's path that means the user wants to get all the products so we're gonna call the get products method that gets all the items from our database we're gonna define it later as well and then next we're gonna have a post method so when the http method equal to post and the path is equal to product path that means the user wants to create a new item and insert it into our table so we're going to call the save product method and we're going to extract the request body from the event object so we're going to do a json dot loads event body and then next if http method is equal to a patch method and the path is equal to the product path that means the user wants to modify an item in our database so we're going to call the modify product method that is going to take in a product id update key and update value but let's extract our request body first and we're gonna extract that from our event object and then next we're gonna have a delete method and again we're gonna request the body from the event object and then we're going to call a delete method that we're going to define later that's going to take in the product id from our request body and then finally if it's none of the above it will just respond a 404 back to the user so we're going to set the response to be just 404 not found or something like that and then finally we are going to return the response back to the api gateway okay that is the skeleton of it and now we just need to define all these functions to get our products or product from our dynamodb table but for the interest of time i'm just going to speed up the video and then i'll explain it at the end [Music] oh [Music] why are you keeping me [Music] why are you me me [Music] okay so i just finished writing all the functions and you don't see any errors here anymore um so there are a few things i want to bring up here so the first thing is when we try to do the error handling so you need to do your custom error handling right here just lock it out in my lambda and then the second thing is when we try to get all the products from our dynamodb table we're using a while loop here and the reason for that is if your table is too big there's a limit on how many items a single operation of the scan can get and if it's not able to get all of them it's going to return an attribute of last evaluated key in the response and we can just check that in our response to see if it's finished or not and if it doesn't finish we are just going to keep querying it and append the new response into our results and then finally finally we're going to return everything oh that should be result not response and then the last thing that i want to mention is in all my methods here i just customize the return body as operation what operation it is and the message i have and then the item that i want to return back to the user but you can customize it the way you want okay so this is everything so i think now we can copy this whole thing and go back to the aws console lambda and paste it here and then the next thing that we need to do is we need to create a custom encoder file just like we do it here so i'm just gonna to right click and then new file pi and then inside here i will just copy and paste the whole thing and that should be it hit deploy okay and now i think we're ready to test this api out so i'm going to go back to api gateway copy this url that's our endpoint and then we're going to open postman and we're going to paste our url here but before we do any operations on it let's go back to the aws console to make sure that we don't have any items on our dynamodb first so click on the product inventory table and as you can see item count is zero and we don't have any items here and now let's go back to postman and do a health check on it first so health and then it's get method hit send all right we get back at 200 which means our api is up and running and now let's insert some item into our dynamodb table so we're going to use a post method and then products and then hit body raw json so it needs to have a product id which is a string i just do 001 let's do color red and then another attribute let's do price let's do 100. it's sent okay we say that we gotta back a 200 and it says it's successful now let's go back to our dynamodb hit refresh oh i think they changed the ui so i think we have to go to items here on the left side and we're gonna click here all right so now we have one item product item here which matches with what we have submitted here and now let's create a couple more and now if we go back to the dynamodb table hit refresh we should be able to see three items here on the table and yes we get all three and now let's just get a single item let's get the second one which is a get method product and i believe we say that we're going to do a query parameter product id value as this and we get back the second item and if we enter a product id that's not on the table let's say one zero zero four it's going to return a4 l4 not found and that's our message and now let's try to modify one of the items uh let's say that we are going to modify the second item we don't need this so in our request body we're gonna say update key let's say we're gonna modify the price and then we're gonna update it to 1000 and i believe it's a patch method yep that's right uh we have it updated to be 1000 and let's go back here right now it's 200 but if i do refresh it's 1000 let's see what else do we want uh delete let's check out delete method and i believe we just need to pass in let's delete the first one it's successful and that one got deleted and let's do refresh and that is gone and then finally we are going to get all the items on our table which is a get method and it's a products method and we don't need that and it should return both of the items back to me in a list internal server error all right let's see what happened so we can go back to the lambda console hit monitor and then view logs in cloudwatch and let's open that and there should be the log let's see what happened key error item of course there is a typo here uh let's go back to the code get products and that should be items not item that is well hit deploy again and now let's test it one more time and now it's working um it's returning products as a list of items okay so this is it everyone i hope you have learned something and if you like this video i hope you can give it a thumbs up and i'll see you in the next video
Info
Channel: Felix Yu
Views: 45,167
Rating: undefined out of 5
Keywords: AWS, Serverless, API, Serverless API, Lambda, API Gateway, DynamoDB, Python, CRUD
Id: 9eHh946qTIk
Channel Id: undefined
Length: 26min 47sec (1607 seconds)
Published: Sun Dec 26 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.