Build a Serverless CRUD REST API (API Gateway + Lambda+ DynamoDB) In JAVA | PART-2 | In 45 minutes

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
Hello friends welcome back once again to my video tutorial this is lipsa again with you so in my last video I have background about AWS Lambda crowd rest API so for this what we need so we need to create a dynamic table we need to create a Lambda function and we need to create the API Gateway okay so we have created all the resources in our last videos okay that is how to create a dynamitivity table from scratch how to create a Lambda function and how to create the required IM role and assign to that particular Lambda function we have done that one also we have created the resources and methods in the API Gateway and deploy that resources in the API Gateway so you have done with all our resource creation so the next part of this video series is how to write a crowd API in Java and will create a jar file and deploy that jar file to AWS Lambda okay so whenever any request comes from the client so that will hit our API Gateway so in the API Gateway we have the resources so then according to the research our Lambda code will be execute so when our Lambda code will be executed then it will be communicated to the dynamic table to do the operation Okay so let me go to my API get here to show you so what are the resources we have created so we have the employee resources here if you Mark here we have the post method then get method delete method and to fetch all the employees again we have created another resources that is employees endpoint okay which is also a get method so we have also created the stage here Dev stage and deploy these resources in our API Gateway so if I click in the post one here this is our inbox URL so this end point will test from our post map okay so this is the base path okay this is the best part and this is the resource endpoint slash employee is the resource endpoint so let me go back to my PPT to show you that so I have jotted down all the resources and methods we have created so basically I'll release the this one okay API Gateway ID then slash Deb our stage name is Dev that's a slash dip so this is our inbox URL or base URL then the save employee which is a post method so this is the end point then slash employee okay fetch employee by ID which is the get method so this is the best URL then employee slash employee ID so employee ID is nothing but the path parameter so here we'll provide the employee ID as path parameter to page or employee by ID then fetch all the employees we have employee slash employees endpoint okay and to delete a particular employee by ID we have the same employee slash employee ID as path parameters only the method type is delete okay so uh okay let me go back here so let me click here resources okay so here if you notice I have wrongly given the resource name slash employee ID so that should be provided along with the curly brushes okay you know pass a path by parameters in url you need to pass the path parameters enclosed with a curly brush so I hope you understand this one so I just noticed here I have given this last employee ID within the curly brush but here slash employee ID is only there isn't it so this is a ram format to give the path parameters so I hope you understand let me delete this research and then I'll again create the resources to get the employee okay so let me delete this one so that you can also understand how you can delete our resources if you create it wrongly or in wrong format right so let me click here in the employee ID because I have given slash employee ID only but whenever we pass a path parameter in your url that should be enclosed with your curly brush so let me click here click here in the action then delete these resources again I will regret it so delete resource delete this one okay so that resource is deleted again I'll create that Resources with a correct format so click here in the resources main resource employee then action create resource here I'll give the resource name what within curly brush I'll provide M EMP ID clear so here also employee ID so employee slash employee ID this is nothing but our resource name so enable API Gateway curse we will check this one then click here to create your resource here you can see if you mark you can add path parameter using brackets it is clearly mentioned over here for example the resource path username represents a path parameter called username okay so that is nothing but your path parameter within enclosed calipers you can provide your path parameter so click here to create resource understood so now I'll create this resource to create my method for this so click here so we have to create one delete method and one get method if you see here so to fetch or employee by ID we have employees employee ID which is a get method and to delete your employee by ID we have the delete method that is also same employee slash employee ID so we'll create so we have the resource already created employee slash employee ID now we'll create two separate method inside this employee slash employee ID resource so just select this one employee ID then click here in the action then create method so in the create method I'll select get then click here in the tick mark then I'll select this integration type as Lambda function Lambda proxy integration I am doing so I'll select this one my region is AP Southward okay then I'll select the Lambda here so my Lambda name is rest API Lambda okay then save this one add permission to Lambda function click here okay so my get method is also created successfully now if you Mark here slash employee ID within calibras is there and this is the get method now similarly let's quickly create the delete method as well so create method select here delete click here use Lambda proxy integration give your Lambda name then save this method Azure add permission okay so our delete and get method is also created so one more thing I would like to add here so see so here we have the same resources with two different methods so you can also do one thing slash the employee ID you can also create a method like this and you can also select any app from here so whether it is a get method or whether it is a delete method it will call this employee slash employee ID resource okay but I I don't want to do this I just want to share this information with you but I am okay to create two separate method within this resources that is get a delete okay so let me cancel this one hope you understand how we can add path parameter in our URL within curly base we can provide the path parameter okay so again we need to deploy this amperage as we have modified our resources we need to deploy this employee resource so again click here in the action click your deploy API then your stage this already you have created Dev stage description no need to provide any description if you want you can give the description then click here to deploy your resource so if I go here in the devs test you can see now at employee slash employee ID is created successfully and we have provided employee ID as a path parameter okay so here click on the get we have the similar employ size employee ID delete the same employee side employee ID and Method type is what delete understood okay so now what else so now we have to write our crowd API in Java and apply to AWS Lambda and we'll test our rest 10 points through Postman clear so now we need to go to our intelligent workspace so let me go to my intelligent workspace so this is my intelligent workspace so here I have already created a maven project to save the time so Cloud API using AWS Lambda and dynamodb so inside this project we'll write our prod endpoint so for this let me expand this project and go to the form.xml file first I'll show you what is the necessary dependencies required for this project so let me open this one so this is the Marine project as you can see so I need the AWS Lambda Java code so I have added this dependency here clear then I need the AWS Lambda Java events okay so this is my Java events then I need the AWS Java SDK Dynamic way to communicate with the dynamic DB we need this dependency right and I have added the first XML Jackson code Jackson databad to convert the object to string or string to object I need this dependency so that's it in my form.xml also I have added a build plugin here you can see Maven set plugin clear so that's it in my palm.xml so let me close my pump.xml okay so we need the AWS Java core events then we need the dynamic dependency then we need the faster XML okay then let me expand my SSC so as you know we are going to communicate with your Dynamic data isn't it so we need to save the employee in our diatomitivity table we need to face the employee by ID then we need to fetch all the records from our Dynamic DB and we need to delete a particular employee by ID so we are going to communicate with your dynamodb table so for this I have created a entity inside my entity package let me open that one so this is my employee class so here properties if you can see at the dynamic table so this is used to define your table name so this is my table name if you remember we have created the dynamic debit table right employee table so this is the employee table and here employee ID EMP ID is my primary key so if I go back to my class you can see this is my table name employee then dynamodb has key this is The annotation used for the partition K or primary key so this is the attribute name for this EMP ID similarly we have two more field that is name and ml clear and we have the Getters and Setters okay so that is nothing but my employee object so now we'll write crowd API to save fetch delete our employee object so for this I'll create a Lambda Handler okay so let me quickly create a Lambda Handler inside my package come that up easy to excel so let me create a Lambda Handler here new Java class Lambda Handler okay so this is my Lambda Handler okay so you know in the Lambda Handler we need to implement the request Handler okay implements here request Handler okay so it will accept one input and one output parameter so here input to my Lambda Handler is hot so we have used the Lambda proxy event so here we'll provide the AWS proxy event to our Lambda okay so if you remember when we create the resource we have given the Lambda proxy as a request to Lambda clear so I'll provide here API Gateway event okay so I'll show you what is inside this API gifted proxy requisible and API Gateway proxy response event Okay so that is clear now we'll override the method so we'll implement this method handle request method clear so in the handle request method we have the input as this one and the context okay and the return type this handle request method is what API Gateway proxy response event clear so let me go to this API Gateway proxy request event so that we can understand clearly what we will receive inside this event okay so go to event see here so in the event you can see resources there path is there HTTP method is there what is the HTM method and the headers is also there which is a key value pair of map then you can provide the query string parameters first parameters then stage variable then request context for object request context you can receive in this event and the body request payload what is nothing but your body you can provide and is base64 encoded so we have this input inside our event okay and in the response if I go to the response then what is there in the response you have the status code what is the status code of the uh endpoint then header you can provide the header and also you can provide the what should be the body of this method you can provide in this body okay so this is nothing but our AWS proxy request event and AWS proxy responsive event clear so let me change this input to API way I get to request okay for the better naming conviction I am provide your API Gateway request clear so now what I'll do I'll write a switch case over here okay I'll write a switch case so I'll write a switch case for the Post method get method and delete method so how I can write a switch case on the basis of what I'll write this or switch case just see here so I'll write a switch case here I hope you know how to write a switch case so as we know in the API Gateway request we have the HTTP method so on the basis of this HTTP method I will write a switch case get HTTP method okay so then we have the case inside the switch case so quickly add the cases one is post one is get then other one is I can write delete okay so this is my case clear so far so the basis of HTTP method will write our business logic okay then of course we have our default we'll create a default one okay so let me add the business logic here so if the HTTP method type is post then we know we need to save the employee I am just writing like this then a letter I will modify my business logic then here I'll add a if else condition just Mark here so you know we have to get method right let me go back we have to get method one is employee by ID and other is face all the employees so if you mark these two endpoints one accepting a path parameter isn't it so I'll write a if else condition condition on the base of this first parameter so let me go back if you want you can write your switch case on the basis of endpoints as well you can I think you can write your own business logic so I just doing like this on the basis of HTTP method I am putting the a plus condition so here I'll write a e phase condition so if a Gateway request just mark this statement API get your request dot get path parameters not equal to none so if I'll receive the path parameters inside my get method then it is a employee by employee ID business logic should be executed isn't it so inside this okay inside this page employee by ID this method will be executed okay clear sofa else in the else part all the employees that means my get method doesn't contain any path param clear so this is one again I have the delete method so in the delete method also we'll receive a path parameter so I'll check the first parameter present or not so first I will check the if condition whether in the request I have the path parameters or not then delete delete employee by ID method will be executed clear then default I'll throw some error if none of this case match then I'll throw some error okay so this is the structure this is the structure my handle request method so on the basis of this method type post get or delete my business logic will be executed clear now I will write the business logic so for this let me quickly create a employee service where I can write my business logic employee service okay so this is my employee service so now what I'll do so first so to save a particular method or to delay to face we need to First instantaneous our dynamodb client okay so as we are going to communicate with a dynamodivity table first we need to instantaneous the dynamodb client so how we can do this here I am going to use the dynamic mapper to save Fetch and delete a particular resource so just Mark here so here I'll create a private dynamodb mapper first dynamodb mapper okay the nmodb mapper then I'll write a method to I'd write a method to init the dynamodimi client okay you need dynamodb so inside this I'll create a Amazon dynamodb client imagine dynamodp client then client is equal to Amazon dynamodb client Builder I'll use Amazon dynamodb client Builder dot standard dot build okay so my client is Big then I'll assign this dynamodb mapper to new TP mapper and provide this client so my dynamodb mapper is ready now okay so here I'll create the Amazon dynamodb actually not dynamodb client import this class okay now this in a dynamodb method is ready so now I will write the methods so what is my methods so my method is like save employee get employee by ID get all employees and delete employee by ID so let me quickly create all methods so written type of this method I'll provide the API Gateway proxy response event okay so let me add this one then say the employee okay save employee so I'll pass to this sale employee method what if I get a property request event clear API get to a request okay then I'll pass the context to write some loggers I'll pass this context import this one foreign before any uh oppressor okay so now if you see here inside this API Gateway proxy request event I will receive the body whatever employee object I need to save in my database I will disable it as a string okay so first I need to convert this string this body to my employee object so that I can directly save this employee object to my dynamodb by using Dynamic mapper dot save method okay so how I can convert this string Json string to employee object I need to convert it by using the faster XML Jackson okay so you know the faster XML Jackson convert Java to Json string or Json stick to Java okay so for this I have already created a utility class so here I have all the logic you can see here convert object to string which will accept the employee object and by using the new object mapper write value as string it will convert this employee to Json string also I have a method converge string to object which will accept the Json body adjust string and convert it to employee object so similarly I have the convert list of object to string when I receive the list of employee object that I can convert it to list of Json string agent okay also here if you can see I have create a custom headers which is a as the content type then X Dimension author so this is the key this is the value then X emergent API version okay I have have this header map ready okay so now this is my utility class so now I'll receive Json string so I'll convert it to Json string to object employee object so how I can convert so I just simply call my convert string to object method inside my utility class okay so this is a static method I can directly call it for my class name import this one so my employee object I'll get by this method then by using the dynamodp mapper I can save dynamodb mapper dot save employee object clear then I'll definitely add a logger over here context.getlogger context.getlogger.log I can write data save to dynamodb successfully okay clear okay I need to do one thing here I will provide private Json body private string Json body I'll Define a variable here and I'll keep these things Json body so now I'll convert but I'll convert the object to string so I have the employee object now ready I'll convert this object to string to pass it in the logs clear sofa so we converted the Json string to object then we save it to my our database then again we convert the object employee object whatever employee object we have found after saving to database to string so that we can pass here in our love also if we Mark here so return type is API get your proxy response event so I'll create a return type method here so let me quickly create a private create API response I'll create a so I can call this method in all my business logic okay create API response so create API responses will be like this here you need to provide your response type okay okay proxy response event clear so we have provided the body status code and headers okay so we have set all these parameters to our response event and return that's responsible so this method create API response I can call here and pass the necessary parameter so return I can write here return create API response and pass this Json body okay Json body then status code status code is integer type that is 201 okay then headers so for headers we have already created an method inside our utility that is nothing but create header so we can directly call this method utility dot create it has understood so far so this is how you can create your style method similarly let me create the get method and delete method okay so let me quickly create this one okay so see here we have created this gate employee by ID method so input is API get your request and context so here we have first instantaneous the dynamodb mapper okay now employee ID is comes as a part param so API get your request dot get pathparameters dot get employee ID so let me again go to the API get your property request event so here the path parameters comes as a so we need to do like this API get to education.getpathparameters dot get employee ID inside this key we'll get our path parameter employee ID then by using dynamodb mapper.load so we'll provide this employee class and employee ID so employee ID nothing but our primary key so it will return us the employee object against this ID clear then we'll check if the employee object is not null then we'll convert this employee to Json string okay we have added a lager then we return this create API response so create API response will return as the API get proxy response event so we have provided this string Json body then status code will be uh status code will be 200 because it is your get method okay and we have passed the custom headers so if employee object null then we'll create a Json body with a string like this employee not found exception and employee ID will provide here here we return the also create API response here the status code is 400 and provided the Json body as a string then headers similarly when we fetch all the employees so what is the method then we'll method then we will fetch all the employee lists so for this we have to use the dynamodb mapper dot scan okay method scan method of dynamodb mapper will provide this employee class then scan expression also you can use the filter method inside this dynamodb scan expression you can use anything so for Simplicity I have just added the scan okay so then so this will return me a list of employee object again I have converted this list of employee object to string object okay then I pass this employees and context and added a logger over here and return this to this method which is Json body as a string then status code and the custom headers clear similarly we create a delete employee by ID method first we need to find out the employee ID from the path parameter by using API Gator request to get path parameter get employee ID then we have to face this employee against this employee ID if that employee is present then we'll call this delete method of dynamodb mapper to delete our employee and in the logger I have added data detected successfully against this employee ID and will return this response create API response okay else will return the status code AS 400 that means the employee is not present in our debut so that is my business logic to save face and delete up employee object clear sofa don't worry about this business logic I'll share the GitHub link for this entire project in my description box you can check it out from yourself just understand the logic how it works actually what is the input to our Lambda what is the output so what the input contents and what we are going to return in the output just remember these things and all all other things you will get it in the GitHub link clear now we'll come back to our Lambda Handler to call all this method so we'll remove this comment and we'll call our necessary method so first we need to give the save employee method so for this we need to create the object for employee service isn't it so quickly insert this handle request method create our employee solve this object so we'll call this one employee sub we will remove this one call Employee Service dot what save employee method foreign clear then we'll call get method get by ID get employed by ID so we'll pass these two parameter okay then we'll call H all employees so to catch all employees we have the method get employees then to delete we have the delete method delete employee by ID if I get to educate context and a default will throw some error so let's remove this one so few error through new error I'll pass like uh unsupported methods okay if anything other than post kit and delete will come then we'll throw this error unsupported methods clear so we'll also provide the method here unsupported method API get your request dot but http method okay so we have called this one but we need to return this one as well right so let's return this one okay clear so far or you have any doubt clear right so what is the error in the Lambda Handler here let's check that one okay one semicolon is missing here just save it so we have another error remove this return null okay save this one so let me quickly build this application we'll deploy this one and test our code so let me go to my Tabernacle and write mbn pin install okay so my jar file is created so let me go to this project with directory so this is the crowd API using AWS Lambda dynamodb let me go to the Target folder so here you can see crowd API using AWS dynamodb jar file is created so now what we'll do so our next step is what our next step is write crowd API in Java and apply to AWS Lambda and we'll deploy this jab file to our Lambda function so let me go to my AWS Lambda so this is my rest API Lambda okay so here you can see in the code section upload from G project I'll click here click here upload then go to the my project directory prod API using AWS Lambda dynamodb go to Target folder this is my chat click here open again save it okay so my jar file is uploaded successfully to my Lambda now I'll change something in my runtime settings so I'll change the Handler here okay so click here in the edit button so I'll change the Handler so here Handler your package name then your class name then your method name so let me go to my code so my package name is com.ab.ed2xl okay copy this one go there paste it over here then my class name is what Lambda Handler copy this one this is my Lambda Handler and my method name is handle request okay so that is correct handle request then click here to save okay my runtime is also set successfully now let's go back to my PPT to check what next now we are ready with our Cloud API now the time is to test our API so whether our logic is working correctly or not so test all rest endpoints through Postman so now we'll test all our endpoint save face and delay 10 point by using Postman so let me open my Postman okay so we'll test all our endpoints one by one so what is our first endpoint post to save a particular employee okay okay so let me click on it so this is our invoke URL you can test this one okay so copy this one go to your Postman so paste your end point then the method type is post so we'll select post then we have the body adjustment string isn't it so we'll provide our body so our body I can provide here okay so in the employee ID I have given mp01 the name I have given Daisy and email I have given Daisy at gmail.com so let's click here send to test whether our API is working or not okay so cool so we have get the status as 200 created we have provided this in our response isn't it status is 201 created and this is the Json body employee ID name and email let let me go back to my dynamod a bit table to check whether my data is safe there or not let me go to the dynamic table so I'll select this employee table click here employee then I'll click here to explore table items okay we have we have created only the employee table and provided the primary key or partition key now we'll see other items are added to my table or not so explore table items here you can see one item is recorded successfully employee01 which is the employee ID then email and name okay then our endpoint is working perfectly so whenever we hit this endpoint from our client then our API Gateway Post method will be executed so whatever API get your proxy request event we have provided here that will invoke our Lambda function so in the Lambda function we have written the logic for first method that piece of code will be executed and will get the corresponding response so again this response will be come back to the client okay so this is the post now check for get so select get then this is our base URL then forget we have to provide the path parameter so path parameter let's say I am providing employee 0 1 here to paste the employee by employee ID so click here send okay that is also working fine get method is also working fine so now I'll test the get all the employees from my dynamodb so for this let me quickly add more record in my Dynamic DB so I can show you the list of employee object okay so let me call the Post method again to save some more data to my DB so you can keep here let's set two here I can provide lips and in the email you can provide Let's uh anything so let me hit this endpoint okay so second record is also created uh let me go to the dynamic DB to check whether data is reflected here let me refresh this one okay to record created successfully so now I'll get all the employees okay so to get all the employees I have employees slash employees isn't it employees less employees so click here send okay you can see the list of employee object in the return clear so now what else now we have only one more endpoint that is delete so let me choose this one delete method then delete method we have the employee slash employee ID so let's I want to delete this record employee 0 2 so I can provide this one employee 0 2 and hit here send so here you can see there is no message body is returning so that might be confused a bit so only status code 200 OK is there and if I go to my dynamodb table and replace then you can see employee 0 t is deleted successfully so if you want then you can write the return statement also like this in the Json body you can provide it like this okay I have not provided it earlier so you can write data deleted successfully so that in the response body you can see this one okay okay so that's how the post get delete method Works in API Duty so we have test all our endpoints in our Postman and all are working successfully one more thing I like to share that is the log so we have added some logger in my code right we have added some loggers inside our code like this data saves save successful to dynamodp page employee by ID we have added some logger so we can say that logger in our cloudwatch logs so let me go to my Lambda and monitor this one click here in the monitor so click here in the view Cloud as log section you can see log stream you can select any of the lock stream to check what is there in the logs you can add here the filter as well okay so one minute plus 30 minute or last one hour 12 hour you can also add the custom date to check your logs okay so let me check this one see this is a page employed by ID so we have return this one in our logs okay again if you see the data set successfully to dynamodb this is the one okay and for delete uh let find out that one fetch employee list is also there see which employee list is also there in the log let me find out the delete one okay this is the delete one data deleted successfully employee02 clear super so so in this video uh series we have seen how we can create a product rest API by using AWS Lambda API Gateway and Dynamo DB clear I hope you understood this video so that's it for today's video guys thanks for watching please don't forget to like share and subscribe my video see you in my next video till then thanks for watching thank you
Info
Channel: Easy 2 Excel
Views: 3,550
Rating: undefined out of 5
Keywords: AWS, easy2excel, API Gateway+AWS Lambda+ DynamoDB, c CRUD API with Lambda and DynamoDB & API Gateway, How to Build a Serverless CRUD API with API Gateway, Lambda, and DynamoDB, Create a Rest API from Scratch with AWS API Gateway, AWS Lambda CRUD API in Java Build & Deploy a Serverless CRUD REST API using AWS Lamba in Java, Implementing a REST API with AWS (API Gateway + Lambda + DynamoDB)
Id: 2m1yraouNyg
Channel Id: undefined
Length: 43min 7sec (2587 seconds)
Published: Sat Feb 11 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.