Build a REST API (CRUD) with AWS Lambda, API Gateway & DynamoDB Using Python | Step-by-Step Guide

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey so we're going to be building a serverless API Lambda API Gateway and uh Dynamo TP so we'll have our user they'll make a request we'll use uh API Gateway the full crud and then we're going to have a Lambda here to process that and we're going to store some data inside of uh Dynamo TB and we'll have a a am rooll assigned to Lambda allowing us to use Dynamo DB and uh Cloud watch to be able to monitor this uh not too complex of uh architecture we're going to be building but very useful to be able to build your own serverless apis first thing we're going to be doing is building a new Dynamo DB table so let's go ahead and search Dynamo DB and there we go and go ahead and click create table so I'm going to make a table called the employee info partion key so so that's a primary key so we need to make something here so we'll make this employee ID sort key we not going to bother there going leave this as is and just go ahead and click create table okay now that we have our Dynamo DB uh created we have our table let's make a new Lambda right so I'm in Lambda and we're going to go ahead and click create function all right so we're going to give our Lambda function name uh for runtime we'll use Python and then for the default execution roll we're going to create a new Roll All right so we'll have this R serverless API demo which will add uh access full access to Dynamo DV and full access to cloudwatch all right so that's all we really need here you can go ahead and click create function all right so we have our Lambda function created if you go down to code to this section here click on configuration and go to permissions and you see we have a role name here so go ahead and click on that all right so it opens up that role and then we have the option here to add additional policies so click on ADD permission and then click attach policies so we're going to search the ones we need like Dynamo DB full access so click that and we'll get cloudwatch logs full access and then you can go ahead and click add permissions so our policies have been successfully attached okay next thing let's open up API Gateway all right for this tutorial we're going to be using a rest API so go ahead and click uh build click uh new API all right so we're going to be creating a new rest API call it server list demo description I'll leave it blank and then for endpoint we're going to leave this as Regional and then go ahead and click create API okay so now we're going to create some resources First Source we're going to name it's going to be status and then make sure you check off this uh course this one and then click create resource all right so we have our first uh first API resource and we're going to create a method under it all right so we're going to do a get method and then next thing we need to choose the Lambda so you'll see under here we'll see here we have our Lambda function API processing so go ahead and select that and then click create method all right we're going to create two more resources employee and we're going to create employes okay so for employees we're going to also make a get so go ahead to click employees and create method Lambda function going to do get once again we selected our Lambda and make sure you have Lambda proxy integration selected and then uh click create Method All right so for employee we're also going to create another uh G uh so the idea would be to be able to get one employee versus this getting all the employees and then this to check the status all right also under employee we're going to add patch to update so we may want to update an entry okay so we'll create patch and also enable Lambda proxy Integrations create that method um let's also create post okay create that method so we can enter in new employee entries and then last let us create delete click create method and create click create Method All right so for employee we have delete get patch and post and then for getting all the employees we just have only get under here and then for status we also only have get so next we need to deploy this API so you need to just click deploy API and then under stage just click new stage and then stage just put new stage and then put a stage name and then click deploy and then down here you're going to get a URL to invoke your API so make sure to copy that for later all right so we'll go back into our Lambda and open open up the code here we're using uh python runtime remember and we're going to build the code to process our API requests and send them to Dynamo DB all right and to do a quick test of our um if our API is actually working up to this point let's go ahead and copy this URL and if you enter in status like this and you press run you'll see that you'll get this output here hello from Lambda which is our just our lamba function running now of course we need to actually build out our Lambda function out to process that and send things to Dynamo DB pull things pull employees delete employees update employees Etc so let's work on that this is going to be the coding portion now going to deploy this new code I have all right so once you paste this code in make sure to configure test event and to just write test here or whatever and click save so we're leaving this as import Json we're going to import a Bodo 3 which helps uh interact between different AWS uh services and then we have uh variables for our three different um our three different API resources so status res employee and employees then we have our Lambda Handler where we put in our mate code here to try this um HTTP method and then event. HTTP method so the first part of this is we're checking if the method is uh get post patch or delete and uh what you can see is basically we have this U if else logic and we're checking one the method and two we're checking the path so for example if um the method is get and the path is status then we'll know to run this uh status response and we'll say the service is operational if the um method is get and the path is the employee path then we'll know to we'll know to get the employee uh information from that Dynamo DB table and we have this employee ID um function that will run and uh same thing for get employees and we have this function that will run if it's get and uh the employees um resource path if it's a post then we'll save some we'll save the Json information into our um employee table and if it's a patch will update uh so on and if it's none of them then we just have a 404 error not found and uh and then we have the accept just printing an error as well and we return our response so that's the basic idea of this main uh Lambda Handler below it we have several functions so we have this function for getting the employee this one for getting the employees from our Dynamo DB table we have one down here for modifying the employees deleting the employees and so on let's do a quick test just to make sure everything is working all right so if we do um this same um API path from earlier and then instead we write uh status you can see we get a service is operational all right so we're in Postman I'm going to enter in the URL here let's do a post and we go to body uh we'll select raw and then for type let's do Json all right so inside of here we need our employee ID all right and we'll add in a few extra things like maybe a job title full name and let's do salary turn employee ID is required and we have now um employee ID 101 job title CEO full name Bob Swit and then a salary of 1.5 million go ahead and click Send oh we need to fix our uh URL here so SL production slash employee let's try send again ahuh and then we got a mismatched parameter as well so we expected a string but we got back a number so even though it's a number make sure to put it in quotes so we'll put 101 in quotes and now let's try and send this all right so we got a 200 and it's been uh saved and uh yeah so success let's enter in a few more people do up will give me a salary of 350,000 not my real salary by the way but I can put whatever I want here though Jason Bourn 2 with security with a salary of 80,000 and we'll send that okay so we entered in a bunch of entries here and we got status code 200 so it looks like we're all good here all right so now let's go back to our Dynamo DB and we clicked uh explore item names and you can see that we have uh three items entered in here now uh one thing you'll notice is despite me doing multiple entries on I used employee ID 103 multiple times we only have one employee 3 ID entry here which was the most recent one so that's great we have our items actually being being entered in with our API now hey so just a few errors I was fixing um just want to say like Cloud watch is since we set this up with cloudwatch it's great for finding errors so look I'm in cloudwatch right here and then you can look at the time and this was the last time I run and you can look at the stream here look through the requests and see uh any errors that we might have got get item that the key didn't match the schema and there was a typo there so yeah this helped me out um and I had another issue with decimals uh not to worry I'm going to be posting the full code on GitHub all right let's go back to uh to post for a second and just go through a few of these other ones all right so here's another uh request so we go in parameters here and we're going to do uh the employee table again and then we write employee ID and then you can uh specify a specific employee ID so let's do 103 then click Send and then you see we pull um the security job title for Jason Bourne 2 with the his salary employees remember that resource if we send that you can see we get all of our entries here hey so I was just just having some issues with the delete and um patch the update methods uh it was just uh some typos in my Lambda code all good that's all fixed um so not to worry corrected code will be on GitHub so uh yeah let me just show you how to use those now so last two uh patch so you can see we have our list down here all of our um all of our uh employees and um typo has to be a capital I for this part uh so let's say um we want to update the employee uh 102 uh next we need to have a uh what's the update key so you can see from our um Lambda it's expecting employee ID update key and then the update value here so uh in this case say we want to update salary and uh next we put the update value uh so let's say there were some pay cuts the company struggling so the new salary of the marketing director is maybe um 140,000 let's say so oh whoops wrong method of course not employees we employ I mean wrong uh URL so so yeah this is part of the employee resource so we'll send that now and there we go so we updated the marketing director salary now um all right so the last one I have to show you is delete so uh maybe uh the marketing director got got laid off um in that case all we have to do is inside of our uh Json we just put the employee ID and then run this and you'll see right right now uh it's been deleted so uh if we go back to uh get and we get all of our employees you'll see that we only have one employee left Jason Bourne because I deleted the others all right so that's it so you make arrest API using crud thanks for watching hope this helped you out bye-bye
Info
Channel: Tech with Hitch
Views: 8,101
Rating: undefined out of 5
Keywords:
Id: 7bgUF6YESxA
Channel Id: undefined
Length: 17min 32sec (1052 seconds)
Published: Mon Nov 06 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.