AWS Lambda With DyanamoDB in Java using Sam CLI | CRUD In AWS Lambda and DynamoDB With Deployment

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello and welcome back to the channel my name is Mahesh karya you're watching me on YouTube now and in this session I'm gonna take you through how to create a restful API using all the operations of Creator update delete using Lambda functions on the top of java and using dynamodb so we are going to create this two combination using Java and everything will I'll be doing from very scratch so don't worry about if you are not aware about few of the things already uh only things you have to have is Sam CLI okay so if you don't have it already you just need to grab this from here and you will be greeted with very first link AWS Samsung uh this is officially provided by AWS so don't worry about the further changes and all that support kind of things long term support you will get of course and Linux no I can Windows you can download according to your channel Choice like once you have it already use Control Plus Battery to open your terminal and once you have it like you can directly search for uh uh Sam hypernaven version so through which you will uh see that uh this is my current version I'm using it and you just need to configure uh don't forget to configure your IDP password okay here you need to enter your key access key and in default profile that like you can choose yourself AP South one is my uh currently I am into AP South one so this is my location in Mumbai let's suppose so this is uh what I have selected okay you can choose according to your choice your assistant I I generally don't use this this is kind of monster in our AWS word uh so because all the time we used to choose this one but we always find the nearest location is this one so yeah this is it I guess we can quickly start with in like initiating a new template so Sam in it is the simplest command to get started with any Lambda functions either node.js or go or whatever you want to go with and hit enter with the one okay and here hello world will be using which is a minimal approach that will get now Python and zip is a very common combination to like test and do the things but we we are going with the Java so I'll be choosing this Java image which is seven number seven is Java 11. uh that is that is uh image you can choose if you want like I will be going with zip for now so let's go with the zip or image it's all your choice and once you have it this is invalid uh package type what is package type package type is one of course dependency manager will be two ah that is May 1 because everyone most of the guys are familiar with me one it doesn't matter you can go with Gradle as well and extra tracing uh you can do if you want like it's all your choice and yeah we can provide the project name project name would be Dynamo app okay seems cool because we are doing most of the things on the top of Dynamo things okay so yeah this is uh going to take a little while so uh okay so we are ready with the uh code this is initial code okay this is my function this is SRC Main and app.java so yeah this is how it looks like this is a very uh basic demo and of course you can like uh go through it uh so you need request and response and based on that request like uh you can get all the methods you can check so we'll be using a switch case in order to like check if the request is gate uh post or put or delete okay and uh the another thing the important thing is events but we are not going to use events in order to provide the input we need events so I'm going to delete The Unwanted files first I am not going to use the test cases I want to be writing test cases you can write it's all your choice okay and after that I don't need this uh read me for now let me delete this one I need one a git ignore don't forget to add getting node if you haven't already I'll rename this hello world first instead of that I will be saying Dynamo TV app okay this sounds a Dynamo app maybe okay so that Dynamo app that I need to uh add as package name here so Dynamo app is already there so I don't have to worry about that uh in pom.xml we need two dependencies uh that I'm going to add quickly our first one is for a dynamodb and another is one for now here we'll be adding our Json support okay so you can choose this always you can use any editor by the way it's all your choice okay so this Json and AWS uh dynamodb is important for us so uh what I'll be doing is I'm going to remove this all unwanted code first that I don't need this try catch block for now so let me remove this try catch block quickly I don't need this function I will be using our own functions I don't need this unwanted code once again this function I won't be using instead of this I'll be simply returning hello word okay that page content I don't need to return for now so this seems okay to me for now okay this is the simplest example of using it collectors for now I don't need this another thing is important thing is a template so this is a cloud formation template okay if you haven't heard about the cloud formation is the infrastructure as a service provider you can design your own infrastructure uh you don't need to go to the AWS console again and again whatever you need easy to dynamodb of all the cloud watch and whatever you need API gateways all the things you need to mention here you need to write uh events or whatever you want like here only okay so this is the output let's suppose here you need to create API uh so this is how you will find your end point so I am going to remove this I don't need this two things so I'm removing first of all the unwanted code here I need a to use proxy Plus instead of using slash hello I'll be getting all the routes after slash okay and here I'll be using any so this is uh what I need to do at this moment and uh let me remove all this things this is not important to me now instead of this hello world function I'll be saying that this is uh change all occurrences uh I can't see yeah this one instead of this will be saying uh dynamodb function okay so this is my di namo DB function this sounds good and uh instead of this hello world I'll be using this is my dynamodb app by the way okay the folder name so let's check this one so this is the Dynamo app okay I need to rename this hello world function to uh my this function name which is looks good to me for now okay so this is it that's all we need to do memory size you can Define x84 Arm based processor you can choose if you want uh handle request shower and time you can choose again here and yeah this is it I guess we can now we need a a table uh that user table we are going to Define here after this so let me grab this here like this okay so make sure to it should be inside resource now we need to uh connect these two things uh together we have added any already uh so after that to in order to connect uh those two we need to write few uh permissions we need to provide few permissions that we say policies uh that we need to attach to it so I'm going to add policies after runtime uh here okay these are the policies crude policy that is going to connect this user table and after that this will allow all this operation gate put delete update item query and scan again so this is the simplest way of doing things after that uh I don't think anything else is needed as far as I know at this moment code URL let me check with my code URL this is uh the function inside which it will find the things so this is my name of the function that I have just renamed here this one okay and here I guess this is it and now dynamodb dot app we can proceed I guess this is it okay so make sure to install a Docker before proceeding so you have to have Docker available if you don't have it simply search for Docker and use this Docker desktop Link in order to install for uh your respective of like a systems so this is it this is what we need to do uh now let me uh go to the app.java first of all we need to create a user.java or just like our entity you can create an entity package as well if you want but I'll be going with simply uh single package I'll be using because I wanted to make this code Minima you can create a constants and a number of other things if you want this is also uh very important so yeah I guess we can uh start writing this is my public class user so at the rate table okay and here you can provide your table name of course and that table name let me grab this quickly in order to save the time okay so here we go this is my Dynamo app let's suppose okay yeah this is my Dynamite okay so yeah see uh this uh this is what we are getting uh and this is my table name we use this annotation in order to provide a hash key it will automatically generate to generate it automatically we use this so you might be familiar about GPA Auto generated this is like a simple ID after that attribute name you need to Define every time so this is my employee ID let's suppose okay so I don't need this employee ID for now I can remove it as well let me remove this employee ID I can use this name and email I can provide password as well so empid I'm going to remove and say TMP ID I am going to remove it okay so yeah this is it gate name set name and just normal uh things I'm doing here nothing much and if I go to app.java how we don't need to Define this I am going to remove this okay let me remove all the things okay I don't need this much of things so I this have I have an input and context this is the final thing that I have uh first of all we need to find the HTTP method that so what I'm going to do here and uh once you have it like so we'll be having output status code that we need to return and we need to check this HTTP method based on that we'll be adding a switch case Okay so that switch case will call uh the respective things and uh you it's all your choice you can create a separate functions as well if you want or you can create like switch case where HTTP method will be gate so it will call the respective functions or methods ah in our language okay so this is my switch case simplest way to to monitor or you can also Track by path okay slash gate slash ID or oh as per your choice again uh that headers I need to put so don't forget to add this here okay and another thing is to to provide the response by the end of this so here I am going to provide the response okay so yeah this is it and after this we need to find and create our our own methods so first of all to get all users I'll be saying that okay this is what I'm I'll be doing to uh to uh get all users okay so this is what we'll be doing dynamodb mapper we need uh and scan a user uh with this dynamodb expression okay and one by one after that we'll be doing a create thing so create user will be like this where we'll be passing a request button that request body from Json we are going to save this user directly um update user will be might uh find first user first as usual like here we need to find a user first and then we'll be going we'll load those views that will check if this is not equal to null then we'll set this name and email and we'll save the existing user it is updated or not will return accordingly and will find the status accordingly the delete one is the last one that will be doing and this is it this is what we do for uh check and if it's there direct delete method is there delete dot save method is there and uh dot scan method is there so yeah this is it this is uh not a big deal right so yeah I guess I need to grab all the packages here on the top of this so let me remove all these things and instead of I'll be using my packages so as you are assuming that you you are using IntelliJ or idea so uh you don't need to add it manually so I guess this is it I'm not sure still why I'm getting error this here uh but okay fine did not provide the error okay uh insert plain text okay let it be so uh let's grab this and now make sure your Docker is running already inside your system and then you can use this command in order to build okay uh so what it will do it will get all the packages the template sorry I need to change the directory to dynamodb app and here we can use this Sam build function and for the first time it will take time of course to grab all dependencies if something goes wrong it will provide uh here okay this variable demo mapper is not found and that demo mapper we need to provide here let me grab this quickly so yeah inside app after this we need to provide okay these two things now uh it seems like okay to me and if I go again do Sam bid so you can you can now see all the errors that you will get here one by one I will fix the things and uh again so now what we are getting is compilation hello world is some somewhere it is finding a hello world that we missed a hello world somewhere that's what I am going to search here Ctrl shift F command V hello world inside template okay so I I forgot to rename this to uh demodb function okay so instead of hello world we'll be using demodb function now let me try this one last time and hopefully this time it will get executed so okay let's uh let's find what else we have app doesn't exist inside 16 app line number 16 okay and here this app doesn't exist okay so let me see why this is getting failed because inside here it should find for dynamodb app space dot app so dot app is my function name okay so I need to remove this line because this is not needed and here we go this is done okay everything is running fine now the very first command that you need to do is to use this guided command so you can use this Sam deploy guided and here you can Define again Dynamo DB or Dynamo app okay this is by Stack name stack is the thing what is going to create okay this is uh this template.yaml which contains a lots of things which is uh which we say as a stack this is my stack okay episode 1 is my region after that uh you need to confirm the changes yes we need to confirm the changes clim rule creation of course you can provide all the permissions it will automatically roll back if you say no uh and yes for in case of you don't want to disable it or don't want to do it I don't have authorization Define you can Define authorization for protection layer but for now we'll go with the basic things save arguments to it will create another Tamil files so it's completely fine I'll show you this will be a Sam config dot month hit enter hit enter and this is it so for the first time it will take time so be patience okay it's gonna create a complete stack so behind there are lots of other ways as well like you can directly convert your Docker compost file to Cloud information as well so this is also super cool we say as Docker context uh which I have made video on the top of that already you can go through it till then you can wait for this okay it's going to take time again and I guess I forgot one thing here I yeah okay make sure to remove this after this whatever it may be you can Define this uh path according to your choice okay so this is my Gateway API Gateway it's going to create here and uh yeah so it's going to also create a S3 bucket and it's going to upload all the things that uh that is here and of course you can check the things on local locally as well it's not needed Sam local start API is the command to to check everything locally so see these are the things that you need to cross check okay it's going to create a Dynamo function if your permission I am role and the table of course okay these are few things it's going to create in the stack so we'll say okay this is my chain set okay we used to call it chain set okay and once you create then you it will be synchronized with all of your changes so there is another command that we'll be using so you can track this on AWS as well if you click here so yeah here uh you can see this Dynamo app so it got created actually and here as well you can see this is my API okay so what I'm doing command shift p I'm using Postman so I'll be creating new call Api calls from here and make sure to use a slash something like user maybe so in here you will see if something goes wrong you will see this error message here in order to track the error what you can do you can go here directly into Dynamo app and here this chain socket got created so events you can check here all the events that's uh that's happening behind the scene and resources you will check here okay this is my function this is my role and inside this function you can directly go to that uh that Lambda function that you just got created and here uh let me see here okay this one this one is uh the thing and here it got created you can check or monitor the things as well you can click on monitor you will see the cloud was logs okay view logs Cloud blocks okay or uh if you don't find anything here uh yeah this this logs got created uh click on it and you will see uh okay what's happening that error message you will see this class not found error that you are getting Dynamo app dot app this this is the error that if something goes wrong don't forget to do this okay now in here let's go again uh let's find for now you can uh check uh here that we are missing this thing as well few of hello world things are still remaining so um make sure to remove this with this okay the first thing okay after that uh here as well you can see this API somewhere this is inside AWS that doesn't matter now okay and this hello one instead of that will be using uh here Dynamo [Music] app okay and this should be let's suppose artifact ID should be this okay so this sounds good always you can choose install the things required things and this is it I guess and I of course you can uh check this out locally as well so I'll say uh Sam local uh start API okay so that will directly run this on localhost uh this thing if you go here if we uh refresh the page slash user you you can track the further errors here uh still uh this is invalid Gateway error message class not found somehow we are not getting the class once again uh that's we need to work so I I did rename all the things with Dynamo and this uh is Dynamo okay so these are the minor changes that I'm doing here and in my template and now I'll be using this sync command in order to like get in sync with my older stack name let's see if it works or not I'm I did major changes actually it should work so uh so if if I uh go there in my console so this is this is what the name of my stack Dynamo app let's see if it's still there or I can make the changes if if possibly you just need to say yes okay it will take a while so uh now we are done with updation so you learned like how we can sync with it every changes that you can check it locally as well let's try this out let's uh grab this command shift p Postman new Postman API test this out make sure to use something like user here you go get this empty uh Json I'm using Postman uh this extension by the way nowadays it supports inside your vs code so after that uh another request would be uh again I'll create new post uh thing inside body uh that you need to provide a raw Json format here we will be having a few things let's suppose name of course a name will be Mahesh and email would be Mahesh at gmail.com okay Mahesh at gmail.com and this is it I guess I'll pass this uh user it should work it got created if you go here you will see that user is inserted here I'll see uh how I can use this for put as well so this is my put API where I need to pass the same URL but in the body we need to pass one more additional field that would be my ID so I know my ID that I just got created I'll use another field here only now with using ID you can provide the slash ID if you want like in query params it's all your choice but I wanted to be in the body itself all the time it's a kind of secured as compared to providing an uh URI so yeah this is it if I'm going to uh put this uh I'll say mahesh1 and Mahesh one so record should get updated so a user got updated let's see if this user got updated so yeah the changes are here you can clearly see and if I go into new Postman API request that would be my delete request and delete as well will be passing inside body itself again uh for the security purpose and otherwise you can choose other ways as well so so here I need to pass this again this is my Json format try to send this and try to uh yeah sorry I need to paste this URL again here okay so you can provide the routes according to your choice if you want guys like this is got deleted and this is my basic crude operations that you have gone through uh the code is available inside description so don't forget to uh like grab this I I made these changes only this Dynamo Dynamo because might be this Dynamo function name was not right this is where you find or create your description and again we'll uh will try to run this locally because I was not running this locally because it was connected with my them local start API dynamodb actually it was connected with so I was not sure like how it will work from locally so let's try this out as well command should we create new slash user hit enter and this is it we will get might get error actually because locally uh somehow this is not working so for every build I need to create a jar and that jar uh okay so this is working locally as well it was not working locally so you can try this out by using this uh command so this is it I guess for this tutorial and thank you so much for all of you guys uh for being with me supporting me all the time it's been pleasure to be here and don't forget to subscribe my channel if you haven't already and smash that like button if you enjoyed and uh I'll see you soon in the next video till then thank you so much Tada bye bye
Info
Channel: Mahesh Kariya
Views: 2,815
Rating: undefined out of 5
Keywords:
Id: gLKzqXgDD1A
Channel Id: undefined
Length: 27min 25sec (1645 seconds)
Published: Fri Jun 23 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.