AWS Lambda & DynamoDB - AWS Serverless Part I

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hey guys welcome to the lesson today we'll be looking at AWS lambda functions in this tutorial we'll write two lambda functions one to read data from the database and another one to write data back to the database will be using dynamic DV which in case you don't know is an AWS know sequel database that stores key value pairs similar to MongoDB or Couchbase but more on that later by the end of this lesson you'll know how to write lambda function how to give it permissions to talk to DynamoDB and we'll do that via IAM roles and also how to do some basic testing just to make sure everything works okay all right before we get started I just want to give a brief overview of AWS lambda and of serverless computing in general so what is lambda and why do I want to use it basically AWS lambda is one ingredient in Amazon's overall server list computing paradigm and what it does is it allows you just to run code without thinking about servers in other words Amazon abstracts away all of the other components that you would normally need to run code including servers platforms virtual machines and basically anything else so that you can just focus on writing code and telling it when to run so since you don't have to worry about any of those other things the time to production or time to deployment is very low you can literally write a lambda function configure it and having it run in production in minutes flat another great benefit of using lambda is that you pay only for the compute time that you consume this means that you are literally only being charged for the time that your code is actually executing now this is in contrast with a traditional model where you may be paying for servers updates maintenance perhaps even IT personnel regardless of when your code is running or when it's not running if we take a quick look at the pricing structure here we can also see that your first 1 million requests with lambda are free and then after that you're literally paying fractions of a penny on the dollar for any requests after that so this is a big benefit it's a very cost effective way to run code all right one other side note on serverless computing of course that doesn't really mean that there's no server involved your code is still running on a server somewhere it's just not your server it's someone else's server so actually you could just as easily call it server full computing but you didn't hear that from me all right that's enough of that let's get coding all right so the first thing we want to do here is to go over to dynamodb and create a simple users table so that we have some data to work with when creating our lambda functions now of course you'll need a AWS account to do any of these exercises so if you don't have one just head on over to aws.amazon.com I won't walk through every step of the signup process because it's pretty self-explanatory but you just want to go over here to create a new AWS account and then follow the prompts fill out all the forms until you've created your new account then go ahead and sign in and that should bring you to the AWS management console so from here we basically want to go over to the dynamo DB area so just type in start to type in dynamo here it should pop right up just click on that all right now before we do anything else we're gonna want to go ahead and select an AWS region from the right hand side of the top menu here now you're gonna want to go ahead and select the geographic region that's closest to your location for example I'm in Seattle Washington so I'm going to leave us West to Oregon selected and you just want to make sure that you know throughout the remainder of these exercises that whether you're working on dynamos lambda or anything else that you have the correct region selected all right so we want to create a table so click on the create table button and I'm just going to give this the name users partition key that is just a unique identifier for each entry in the database so i'm going to use an ID for that we'll leave that set to string leave everything else set to the defaults and click on create alright so it'll take a second or two for our table to be created and then I think what we'll do is we'll go ahead and add one item in the table so that we've got something there for us to use when we create our first lambda function so if you click on the items tab and then click on create item that should pop up the create item dialog and here's where we can actually add entry into our database now there are a couple of different views here this is tree view I actually like to go here and change it to text view because then it looks more like JSON you know it's a little bit easier for me to read this so you can use either view that you prefer I'm going to go ahead and set this to one two three four five and let's see so I think we'll do a first name say Bob and then let's do a last name as well and we'll say Johnson okay so once we have that just click on the Save button and that will go ahead and add that entry to our to our table all right and that is literally all that we have to do with dynamos we'll come back here later to to check to make sure that our lambda has actually put values into the table once we get to the writing portion of the of the tutorial but for now that's all we need to do okay so the next step is going to be to create an I am role to give our lambda function permissions to work with this dynamic table alright guys so before we head over to create our roll there is one more piece of information that we're going to want to get from our dynamodb table so if you click on tables from the left-hand menu and then go ahead and click into our users table that we just created you want to look at the overview tab and if you scroll all the way to the bottom you should see the Amazon resource name or arm and what this basically is is it's a unique identifier to the users table so that we can input this into our role later on so that the role knows which table to provide the permissions for so let's go ahead and just copy this value here and head over to iam so we can create our role so let's click on the services drop down you may see that you already have I am over to the side but if not just start to type it in it should pop up there for you so click on am okay so we are at the Identity and Access Management landing page here we want to create a role so let's click on roles from the left-hand menu now you guys may have noticed that my screen is much bigger than before I've blown it up a little bit so hopefully this is good and you're able to see everything that I'm clicking on and everything that I'm typing into the forums here so let's create a role click on create role let's leave AWS service highlighted this is basically asking us what kind of role do we want to create and now we want to create a service to service role because we want to give our lambda function permissions to access our dynamos evey table so click on lambda allow lambda functions to call AWS services on your behalf and let's click on next okay so here we want to add a policy if you start to type into the box AWS lamda basic execution role now that should pop up here we go just put a checkbox by that one and click Next tags these are optional we're not going to use any tags today but this may come in handy if you have a larger application where you have lots of rolls and you need a better way to organize them or some way to search through them then you can use tags so for now we'll just click on next all right now let's give this roll a name I'm just going to call it lambda DynamoDB roll and we can see it's got our basic execution roll policy already added to it so let's go ahead and create that roll okay now that that role is created we do want to add one more policy to it so if you click on the lamda dynamodb role okay and we're going to click on add in line policy now just expand the service section here and we're looking for dynamo we're going to add permissions to allow this lambda function to call dynamos there it is so just select dynamos evie okay now in the actions section we're we're specifying actions that are allowed in the dynamo DB database so we could very easily just click on all actions right here star and just give blanket permissions to access anything or to take any actions against dynamo but really best practice says that you should give the least amount of permissions necessary to fulfill whatever function that you're you're trying to accomplish so we're going to need basically get and put permissions so we can go ahead and click on add actions and search for let's do get item ok click Add and there it is dynamo DB get item and let's do one more for put item ok and that's been added great okay now let's expand the resources section here leave it leave it as specific and click on the add arne button right here and here's well where we will paste in our copied amazon resource name for our dynamic table and once you paste that in all this other stuff should populate automatically so then we can just click on the Add button okay that looks good and let's see we don't need to do anything here so let's just just click on review policy all right um let's see we need a name for our new policy so let's just say dynamo read/write access and create policy okay there we go so our role is all configured and all set and ready to go and once we get into creating our lambda functions we will basically assign this role to our lambda functions so that they can access the dynamic table all right so next task will be to actually get in and start coding some lambda functions all right so let's go ahead and click on services from the top menu and start to type in lambda into the box here you should pop right up there we go and we are at AWS lambda so quick region check just look up here in the right-hand corner and make sure that the region that you previously selected is still in fact selected I've got us West Oregon so that looks good to me and let's go ahead and click on create a function ok now as you can see at the top there are a couple of different options here to get going we are going to use author from scratch so just creating a lambda function from ground up but you could also potentially create a lambda function from blueprints which is a template or from a shared library or shared repository where you can find lambdas that have been built by other people so let's go ahead and leave author from scratch selected and let's give this function a name I'm going to say get user lambda or let's say get user data okay for the runtime I'm gonna use node 8.10 because that is the well the latest version of node that's currently supported by lambda so for role let's leave choose an existing role selected and under existing role we're going to select the lambda dynamodb role that we created in the previous exercise alright so once you've got those selections made let's go ahead and click on create function all right so this is our Lambda IDE if you will or the area where we can basically code and configure our lambda functions now this first tab that's collapsed here called designer we can take a quick peek in here and this is the area where you would just configure different events that may trigger your lambda so just to back up a step basically lambda functions can run as a response to various events some examples of such events may be uploading a file to an s3 bucket which you can configure here so that your lambda triggers once that file is uploaded once that event is emitted from s3 then your font your code will run here as a response to that and do any processing that you may have your lambda function can also be called from a REST API and we'll see that a little bit later in the next video when we wire up our API get gateway to trigger our lambda functions so we don't really need this for now let's go ahead and collapse the designer tab function code this is what we'll be looking at currently so there's three different ways to get your lambda code into your lambda function first of all you can see right here there's some boilerplate code and we can edit this code directly right here and be done with it of course that works well you know in the most trivial situations if you don't have any external dependencies you want to just have a very basic lambda function then yeah you might want to just edit it right here however if you do have external modules for example NPM packages or anything like that that your codes gonna be relying on then you would want to create your lambda function outside of this interface you know in your own IDE and then zip up that whole code reploid it here to AWS and this is just basically the same as the previous option only you're uploading a file from Amazon s3 from s3 bucket so we're gonna sort of take a hybrid approach here I don't want to direct the I don't want to edit the code directly in here but I want to create the function outside of AWS in my own IDE and I use visual code so I want to create a code the function there and then I'll just basically copy and paste it here one other side note on that we won't have any external dependencies other than the AWS client now you can just assume that you'll always have access to AWS client in your code that is to say that it exists implicitly so you don't need to you don't need to add it you don't need to install it you can just import it directly in your lambda code and assume that it's going to be available alright guys so let's head over to my code editor now I use Visual Studio code you can use whatever you want of course I do highly recommend you check out Visual Studio if you haven't it just has really really good performance really good plugins and it's just a really good overall editor so let me let me see if I can enlarge this a little bit so you can see better okay hopefully that is okay alright now if you do use visual code there is an extension for a WS lambda that I want to point out so if you go ahead and click on extensions here you know I've already installed this but you could just search for a WS lambda and what this does is it provides some boilerplate snippets to get you started so it'll it'll basically scaffold some boilerplate code for an AWS lambda function now let's try this out and I don't believe you'll have to restart Visual Studio but if you can't get it to work you may want to go ahead and do that mine's already installed so I don't want to really uninstall it and go through that again but let us just take a look and I'll show you how it works alright so I've got my project here A to B s lambda what I'm going to do is I'm going to create a new folder and call that get user data I'm just gonna create a file called index j s and this is going to be basically holding my lambda code now with this extension that I was just talking about you can type in lambda and you see you get some commands popping up already now if I do this one lambda AWS see that automatically scaffold starting function for me there so this is a great way to start out all right so just a brief word about the anatomy of a lambda function before we start coding first off you'll see that this function is called handler all right that is the function that is being exported from this file and if we pop over to AWS again really quick we can see that right here so this lambda function is being invoked via index dot handler where index is the file name index TAS and handler is the one function that's being exported from that file all right back to visual code lambda functions take three arguments by default event context and call back the event object contains information about the event that triggered the lambda so for example if this lambda was triggered by a rest endpoint in API gateway then that event object would contain information about the request body about URL parameters and other information regarding that request the context object provides methods and properties that provide information about the invocation function and execution and environment and the callback is just a callback method that you're probably familiar with if you've used nodejs a callback method that you can return to the calling function that invokes lambda all right we actually won't be using any of these three at least initially so let's go ahead and get rid of this commented out code and this console.log and start fresh here okay so you can see our only import here is AWS SDK first thing we want to do here is make sure the region is set so we can do AWS config dot update we want to pass in a region object corresponding to the region that we selected so in my case it's us West too but make sure this contains whatever region you're using currently in AWS all right now - so the purpose of this function is going to be to retrieve an item from DynamoDB so let's take a quick look at the dynamic API and see how we can do that all right so here's some basic description a overview of the AWS DynamoDB service object let's go ahead and search for yet item alright so this is the function that we want to use and we can model our code after this so once we instantiate the object then we do get item pass and params and a callback function so the first thing we want to do we want to instantiate an instance of the DynamoDB service object so let's do new a WS dot dynamo DB and this API version is just kind of a standard object that it's used to instantiate various various AWS service objects so this is a standard format so this can be passed in as is I believe there's only one API version construct our parameter object so this object is going to basically describe the item that we want to pull out of dynamodb so first we want to specify the table name it's the users table all right and then we want to specify a key which I don't know if you remember earlier we talked about the primary key for our users table which was ID all right so we want to set ID and so DynamoDB uses a sort of special flavor of json called DynamoDB json and it looks similar to regular standard json except values are actually prefaced with a key value in this case s which describes the type all right so we're gonna a little bit later we're gonna look at a way to unmarshal this diamond mo DB JSON to more standard JSON so we can use it so it's a little bit more predictable and familiar when we're using it with our REST API all right but for now we'll just use the the dynamos evj son as is all right now that we've got our params object and go ahead and write our yet item function so gdb get item and we'll pass in params and we'll pass in our callback function all right so we'll check for our error first and if there's error will just say console.log all right and if we do get our data back then we'll just console.log that as well all right and again you'll notice that we're not using callback because we're just gonna test this function directly from lamda so there's no calling agent that a callback could be passed to so we're actually we're not using any of these arguments right now all right so I think let's go ahead and we'll go ahead and save this now we can go ahead and copy all of this code and we're gonna paste it into lambda so go ahead and control C copy that let's go back over to the AWS console back over to lamda and we can just replace our existing lamda code with the code that we just wrote all right oh it doesn't like something here this should be all caps there we go all right now you can go ahead and save that okay and I think I need to make my screen a little bit bigger here see come on there we go maybe that's a little bit too big there we go all right so now we're ready to do a quick test of our function so what we won't want to do is head over here and what we need to do is configure what's called a test event so if you just expand this drop-down click on configure test events we want to create a new test event so let's call this one get user data same as our lambda function now the way this works is basically you can provide arguments here or you can provide a JSON object that would mimic for example your event object that would normally be provided you know by means of invoking your lambda function from a REST API since we're not doing that we really don't need to provide anything here so we're just going to leave it as is and click create okay now let's go ahead and click on the test button and test out our lambda function all right execution results succeeded so that looks good and we can see right here here is our object from our dynamo DB so Bob Johnson 1 2 3 4 5 that was successful so our lambda function is working all right all right now that we know our lambda function is working there are a couple of things I want to do to improve it so let's head back to the code editor okay now if you remember earlier I mentioned this kind of funky DynamoDB json syntax we want to get rid of this we don't wanna have to deal with types in our keys especially when this JSON comes back to our to our application we don't want to have to actually do dot you know IDs to get this value we just want to do ID right so in order to do that we can use something called document client and this is going to unmarshal unmarshal our DynamoDB json tube our standard JSON alright so right under your d DB service object what we can do is create a new variable constant and let's do document client okay so we're going to do new aws.amazon.com dot document client and again I'm just going to pass in the region like I did here all right now now that we've got our document client what we're going to do we're actually going to invoke get item via the document client and not the DynamoDB service object so let's copy and paste our variable name here and the syntax is slightly different if you're using document client so instead of get item it's just going to be get all right now the only other change we'll need to make is we can go ahead and get rid of this string syntax here and we're going to do key oops put ID directly under key like that okay let's bring that back here all right I think that looks pretty good so let's go ahead and copy this code save this file we'll copy this back into AWS and we'll see if this works all right so just scroll down here to our lambda I'm going to do control all delete and just copy this code here um oh you know what I fix this in AWS but not in my code editor so I've got to remember to fix that that should be all caps alright and let's save it alright and we can use the same test event as before so let's just go ahead and hit test that looks good succeeded so let's see and that we can see our item had it looks now like more traditional JSON syntax we don't have that weird s key value anymore so this will be good for use with our actual application all right there's one more thing I want to do to improve our lambda function and if you remember when we first created our function we're using the node 8.10 execution environment which means we can use fun stuff like async await instead of a traditional callback so we're going to refactor our code to use async await back over to the editor so what we want to do now is to refactor this older callback style function to use a more modern async/await now since we're using the node 8.10 runtime we are able to take advantage of a a little bit of magic that happens with AWS SDK so what it'll do is the AWS SDK will basically check for a globally defined promise function and if it finds it it will add the promise utility method to all of the AWS request objects so we can basically call this this promise utility method on any of our request objects such as get item or put item in order to premise a PHY it so that we can then use async await okay so the first thing we can do is get rid of this callback we won't be needing that anymore so go ahead and get rid of that and we can also get rid of this function keyword here now let's add a sink all right and we'll just use a arrow function here so now handler is an asynchronous function and we can refactor this bottom part here where we do get item to use a weight all right so we use try catches with async/await catch and take an error all right so we can basically take this part put it up here on our try now we want to we want to make this a variable so that we can capture the data the data coming back so we'll say constant data equals a wait document client get params and we get get rid of this last part here because that's part of the callback style so now we can do dot promise just like that underneath that is where we can put our console.log for data so go ahead and copy pasted or cut paste that console.log data right here like that okay we can get rid of this old air handling code as well all right just clean up my brackets here a little bit and under the catch error I think we'll just console.log that as well how to log error okay that looks good so now our function has been refactored to use async/await and let's see if this works so let's go ahead and copy all this code I'll go ahead and save that and let's pop back over to AWS collapse this and go down here I'll do ctrl a delete and I'll paste my new code in there and again boy I'm never gonna learn all right this should be all caps there we go I thought I corrected that in the code editor no I didn't there I'll fix that here once and for all all right back over to lambda and let's make sure this is saved and we'll go ahead and test out this this newly refactored function so again we can use our same event data our same test event and just hit the test button succeeded that looks good so let's pop this open and there we go getting the same result as before so that is good with our newly refactored function everything is working perfectly all right so from here where do we go our next task is going to be to create another lambda the next one should go much more quickly because we'll be will basically copy and paste our getitem lambda function and just change a few things so that it does a put instead of a get so then we'll be able to test putting items into our database all right so what we want to do now is to create a second lambda function for put user data so let's go back up to let's see the functions level should do it yep okay and let's click on create function now we're going to do the exact same thing that we did before except this time we'll name it put user data okay no js' 8.10 choose existing role and again we'll select our lambda DynamoDB role and click create function okay so that that looks good there's our boilerplate code and let's go back over to the editor so I think what I want to do here is to create another folder as well to hold our index jjs for our new lambda so let me click on my parent folder here new folder and we'll do put user data okay and I'll create a new file index genius alright so to start out let's go back to our get user data function let's just copy all this code and paste it into our new function alright because we don't want to reinvent the wheel a lot of this code is going to be reused in fact most of it so we can go ahead and save that now let's see now what do we have to change in order to make this put in item rather than get so alright let's look at our params so we're before we were just getting by ID this time we're actually going to be specifying all the fields that we want to add so we had first name and last name first name and let's just put any name here Jennifer and let's do a last name white that sounds made-up doesn't it well good alright and then the only other thing I think we have to change is this get and we'll change that to drumroll put okay so with those two simple changes let's see if this will actually add this item to our database so go ahead and save that and let's copy all oops select all copy and let's go back to lambda go ahead and get rid of this boilerplate code and let's paste our new code in there okay let's go ahead and save it now we will have to configure a new test event so just pop this open configure test events again we can just put put user data same name as our function leave the keys alone that's fine click on create and click on test alright success that looks good let's check the details mmm let's see so there was an error message coming back from Dynamo missing required key item in params item in params let's go back to our code and see what could be the problem ah I see the problem so this needs to be item instead of key because we're putting not getting okay so let's just save that and again these kind of details you can just find from the Amazon SDK for dynamo DB that's where I got all of this data from or that's how I've you know figured out what kind of syntax was necessary how this params object was constructed and and what methods to call and so forth so all that info is in there now let's just go ahead and select all copy let's go back over to lamda okay we can close this up for now and let's replace our function again with that corrected code let's save it and hit the test button all right let's check our logs to make sure that went through okay and tada okay so I'm not seeing any errors I'm seeing null here I think that's because we're not returning anything from our function let me check console.log data okay so there's probably nothing being returned here whereas when we were getting an item from DynamoDB before the the item that we retrieved would be in this data object where with Pudding there's really nothing returned so I'll tell you what we can do now I believe it succeeded but let's check our database and make sure so we should be able to find that new entry with Jennifer white so what I'll do here let's pop open this drop-down and you should see DynamoDB since we were just in there I'll open this in a new tab just so that I have lambda and dinamo both open all right so let's click on tables let's click on our users table let's click on items and there's our Jennifer white item 1 2 3 4 500 do you guys see what happened here so yeah I made a mistake here so instead of creating a new item we over wrote the pre-existing item because I used the same ID I'm sure you guys probably saw this as I was doing it alright so let's go ahead and leave this I'll tell you what we'll do let's change our lambda function so that we specify a new ID and a new name and we'll just make sure that that gets added all right so let's hop back over to visual code I should have noticed that when I was first doing it alright so let's do 6 7 8 9 0 and let's change well let's just change this back to Bob Bob Johnson so he just got a new ID now his IDs been stolen by another person ok so we're going to save that let's go ahead and select all copy ok let's go back to lamda - lambda' there we are let's go back into our put user data function and let's just copy paste this in here let's clear this one out put the new one in save it and let's test this one out okay I don't need to check the logs because I'm pretty sure it's gonna do the same thing you did before let's pop on back over to dynamo let us you see this refresh button here over on the right click that and that should refresh our results and there we go so yeah that was my mistake earlier I I just specified the same ID which I didn't mean to do but that was good anyway that you guys could see how that works so now we've got a new ID and we've got a new entry so that just kind of proves the concept that the ID needs to be unique so what lesson that we learned today if we try to put an item with the same ID as an existing item that item is going to get overwritten but anyway this is good so both of our lambda functions are working or we're able to get we're able to put data back into the database and it's all working as expected one more quick test we can do we can go back to our get function we can change it to this ID and just make sure that we're able to retrieve this one as well so let's just do that really quick well I'll just type this in six seven eight nine zero so go back to go back to our functions let's go back into the get user data okay and we'll just modify this code directly right in here six seven eight nine zero so this should get our our new item that we just added alright save test and let's check our logs and there we go Bob Johnson with the new updated ID so that is still working our changes are backwards compatible it passed the regression test and all it's looking good alright guys I want to thank you all for joining today's lesson I hope it was helpful I hope you learned something and I hope my explanations were clear enough but if you have any questions or if you got stuck anywhere along the way please please leave comment and I will do my best to help you out so again thanks for joining I also have a new udemy course in the works right now which is building and deploying an entire serverless app from start to finish so look for that and once that's available which I believe it should be February of 2019 I will add that link in the description below so again thanks a lot guys for joining in have a great day [Music] [Music] [Music]
Info
Channel: Cloud Path
Views: 121,035
Rating: 4.9590244 out of 5
Keywords: AWS Lambda functions, lambda functions, DynamoDB, AWS serverless, serverless architecture, cloud computing, amazon web services, AWS Lambda, aws lambda dynamodb, lambda serverless, amazon lambda, aws lambda intro, lambda for beginners, introduction to aws lambda, lambda dynamodb integration, serverless lambda, aws, aws lambda example, aws lambda tutorial, serverless tutorial, aws lambda tutorial for beginners, serverless computing, aws lambda nodejs, lambda nodejs
Id: ijyeE-pXFk0
Channel Id: undefined
Length: 42min 23sec (2543 seconds)
Published: Thu Jan 03 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.