Build a CRUD REST API for DynamoDB w/ AWS Lambda & API Gateway using Node.js AWS-SDK V3 & Serverless

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey everyone i hope you're all doing well and thanks for tuning in to another video in this one we'll be building a complete crud api that connects your dynamodb table using api gateway and aws lambda alumnus will be written in node.js using the latest v3 aws sdk and of course we'll also be using the service framework as well as good of actions for our deployment pipeline so hopefully you'll find this video useful and as always if you have any ideas or suggestions or something you want to see feel free to drop a comment below but for now let's dive right in now the first thing we'll be doing is setting up the deployment configuration as well as the im policies so we're going to go ahead and head over to the i management console and we're going to go ahead and create a new user so i'm already on the page i'm just going to name my new user dynamo db crud api user feel free to name it however you wish and we're going to check off programmatic access only because we need the access keys um so we're going to click that and we're going to click attach existing policies directly and now this is going to be a bit tedious but we're just going to go through this list in order so the first thing that we need is um i am full access this gives us access to impermissions and we also need s3 full access this is also part of the serverless framework in which basically what the serverless firmware needs to um enable a successful deployment um because this video we're doing working with dynamodb we would also lock down with db followsus we also need cloudwatch logs full access if i can find it here there it is okay um and we also need api gateway administrator because we'll we'll be working with that as well and we also need cloud formation um and last but not least we need uh aws lambda full access so i'm going to go ahead and check that off awesome so we can just skip the tags part i'm just review that we have everything um awesome okay this looks good so we're going to go ahead and click create user okay awesome so now we are faced with uh a one-time um presentation of the secret access key so we're gonna go ahead and copy that and now we're gonna go ahead and head over to our github repository and click into settings and we're gonna scroll down and do secrets and we're going to add a new repository secret now i'm just going to call this aws secret access key and we're going to paste that in and add the secret we're going to also add another secret this is going to be the ableist access key id and this one we can always view it under im so we're just going to go ahead and copy that and we're going to go ahead and paste it in awesome be careful of any uh space white space at the end or the beginning um but yeah awesome so once that's done we can actually go ahead and write our github actions configuration file so we're going to go ahead and open up our favorite editor i'm just going to use vs code in this case and because the configuration file for github actions um in terms of all these serverless projects is going to be the same across many of my other serverless projects or tutorials in the series i'm just going to use the same file that i've pre-written um so if you haven't watched the other videos um i'm just going to also quickly go over what's going on i'm just so we're not totally in the dark out in the dark here but basically we're going to create a folder in the root directory called dot github and within that folder we're going to create another folder called workflows and within this folder we'll finally create a file called main.yaml i'm naming it mainayama just because i'm going to be deploying on the main branch or sorry this job is going to be triggered on the main branch um so basically basically this is a file i've already pre-written it so i'm just going to paste it here but just to quickly go over what's going on um we're specifying our name here um we're also specifying that we're this job will only uh be triggered or be executed when we push through the main branch and um this is the job is just going to be called deploy um we're choosing the operating system of which we're going to be executing this job or caching our node modules using node for the version 14.x and of course we're installing the dependencies and last but not least we'll be install uh we'll be running serverless deploy which allows us to deploy our serverless project using the serverless framework um to the aws cloud this is where we actually get to use the secret or the access keys that we have generated earlier on so with this out of the way we can finally go ahead and install all the dependencies that we need for our project before we just run the command to install our dependencies i'd just like to quickly go over why we're using the version three aws sdk as opposed to version two which is what most of people are used to um so the reason why is because version two version three is built to be highly modular um and what that means is that instead of installing the entire aws sdk which we would have to do in version two we can only we only have to install the single one um along with maybe a few one or two like some modules that we might need um to complete our use case basically we only need to install what we need and this will help us greatly reduce our alignment package size and ultimately increase performance and things like the deployment time and whatnot so overall it's just a great way in moving forward on the documentation page for the version two aws sdk they also recommend you to use version three as well um and if we actually take a look at the package size here um version two is incredibly large because it basically covers all aws services um so it's around like 62 megapixel megabytes as opposed to just the client download db uh package sub module that we need um this is only around like 4.5 megabytes so you can see the incredible size difference um so yeah this is why we're using version 3. um we'll also be using or installing the dynamodb utility uh functions or methods uh in this case this one only has or only provides access with marshall and unmarshall and basically these functions allow us to convert back and forth between javascript objects or json into dynamic db records and the reason why we need this is because if we take a look at this example here this is just a stack overflow post that i found basically this gives us an example of what a dynamodb record might look like um and we'll of course definitely go through this more in detail when we actually deploy our functions and test them well basically the dynamodb format has a separate nested key within that specifies the type um the data type of the value it's going to be storing so s stands for string here and basically it's in a really it's just in a different format as opposed to json and those functions will help us convert between this and regular json um so yeah that's what we'll that's why we'll also need that as well and last but not least we also need the serverless item roles per function plugin and basically uh the reason why we need this is because for our project today we'll be creating multiple endpoints and each endpoint will have a separate lambda handler and it's a really good practice to ensure that each lambda has a highly restrictive set of im permissions and basically allow a set of access rules uh in terms of what it can access on the aws cloud um and because each landlord will be performing a different operation for example like reading or getting or adding things to the database we we want to make to ensure that each liner will only do those things and nothing else so that's just a good practice and this serverless plugin will allow us to basically specify a really granular set of rules for each lambda so with all that explained let's go ahead and actually write up our service.yaml configuration file so in our root directory we're going to go ahead and create a file called serverless.yaml and the first thing we want to specify is the service name or basically our project name and this is going to be a dynamodb credit guy so i'm just going to name it as such and we also want to specify the cloud provider that will be using in this case it will be aws and the runtime for our laminas which is node.js 14.x which is a currently late the latest version supported on aws lambda we also want to specify the stage for development i'm just going to use dev here because we're still in development and we can also specify the general region of which our resources would be deployed i'm going to be using us west one uh preferably you'll you always use the region that's closest to your clients of service the next thing we want to specify is the global environment variables that um so we're going to specify under the environment field and i'm just going to create a a variable called dynam oops done it will db table name and essentially this will help us store uh the dynamodb table name which we can then access within um our lambda functions themselves and to do this i'm actually going to be creating custom variable later on in a second um i'm just going to reference it here first as self colon custom dot post table name so if you can guess already basically our dynamodb table for demonstration purposes we'll be holding like article posts for like a blog or something um of course your use case will be unique um to yourself um so yeah now we're gonna go ahead and create the custom field and of course this is gonna hold our post uh table name and i'm just gonna call this post table and dash i'm also gonna include the um the stage as well so we can have a unique table for each stage if we choose to move further along the development process um so yeah next thing we need is of course the plugins and we only need one plugin here which is as mentioned earlier the serverless im rules per function plugin so we can just list it as such and awesome and once that is out of the way now we can go ahead and basically write the configuration to deploy our actual dynamodb table and this is going to be under the resources field and basically under uh under here we can actually declare cloud formation templates directly i'm going to using yaml format of course and yeah so we can now specify the deployment configuration for dynamodb table so it's going to be as follows we're going to create um we're going to create a field called post table basically um it's gonna be basically for our project for demonstration purposes at least in this tutorial we'll be creating a table that will store a set of like i guess user posts for maybe like a blog or blogging service or something like that um of course your use case will be unique to yourself um so feel free to name this to however you need i'm just going to call this post table here um also we're also going to specify the type of resource this is going to be and of course this is going to be aws dynamo oops too many colons there dynamo db double colon and table awesome and of course we also need to specify some properties here um so namely the first thing we need is of course the table name um this is the actual table name that will appear on the aws cloud as opposed to the field here um so for the table name we've already declared it here in our custom variable so we're just going to reference it there as such custom.postname us oops too many curly braces all right and the next thing we need is the attributes definition so for the next two fields i'm just going to quickly write them out first and then i'll explain them in a second um what's actually going on but basically here we're going to create a an element in the list called post id and we're also going to specify the type of this element and i'm just going to keep this as a string you can also if you want your ids to be like numerical you can just specify this as n for number i'm just going to keep it as strings um and of course after that we also need another field called key schema and again i'll explain this in a moment but under key schema we're going to create a new element and we're going to call this attribute name and of course it's also going to be post id and we're going to specify the key type field for this and it's going to be hash all right all right so basically what's going on here is that the attributes definitions field is going to specify an array of attributes for describing the key schema for the table as well as the indexes and what i mean by key schema is basically the field here and this specifies the attributes that make up the primary key of a table or the key attributes of an index and basically what that means is that we're specifying the primary key of which we will be for example looking things up in the table and basically um yeah looking things up in the table so that we can uniquely identify documents uh within the table um and yeah the last field that we actually need is um basically specifying the provision throughput and this allows us to specify how much we want to allocate in terms of reading and write operations and i'm just going to keep this at the very bare minimum which is just one so i'm going to keep it as one and of course we also need to specify the right capacity units um and we're also going to specify that as one as well and i'm just checking if i made a title here okay awesome so once that is out of the way we can actually go ahead and write our functions configuration so i'm just going to include this above the resources field and all right so the first thing i want to do is of course put this all under a functions field and the first thing we want to create is our get endpoint so i'm just going to call this get post and we're going to specify our handler path or where our lambda handler is going to be in this video to keep things simple and organized i'm going to put everything under the api.js file that we'll create in a second if you want to refactor it feel free and definitely do so um but yeah so i'm just going to call uh create this on an api.js file and i'm going to call my function get post later on and we can specify the name of the function and it's going to be called get dash post you can also include a description i'm just going to leave that out for now we also want to specify the memory memory size um because the documents we'll be working with today are going to be quite small i'm just going to use the minimum value possible which is 128 megabytes and i'm also going to specify timeout of five seconds awesome all right so the next thing we want to specify is the event triggers and basically this is going to be a set of events that actually allow our lambda function to know when to execute um the specific one we want is going to be http event triggers which is basically like when someone makes an api request to our endpoints um then our lambda function will be triggered and so two things that we need to specify under here are the endpoint path and it's going to be post slash and curly brace post id and we're going to basically this is allowing us to specify path parameters so that when we actually call this endpoint we can just pass the post id and a path parameter as opposed to the request body um since this is after all get endpoint um and yeah so that get sorry so under the method field we're gonna specify get as the http method and awesome now the next field that we can specify is going to it's going to be the i am role statements and as mentioned earlier this is basically specifying the permissions that we have for this lambda function specifically and it all follows the same format it's going to be dash sorry it's going to be an element into the list and it's going to be uh the first field is going to be effect and we're going to be allowing um the following action and the action is going to be dynamodb colon get item and it has to be in this format um we also want to specify the resource of which we will be allowing this action on um and the resource is basically going to be this dynamic db table that we will be creating um together and to access that there's a special function i believe it's called a function i guess um it's uh basically get attribute i believe um but basically with this we can we can reference the arn basically the amazon resource name of this table once it has been deployed the air en basically allows us to identify which table among all the tables perhaps in our region and aws account um that we are referencing um so we're going to go ahead and type that in as post table and that's basically referencing this field here and we're going to get the attribute of that dot arn so we're going to specify as such and you'll see this this really similar format for basically all the other lambda functions as well so now we can go ahead and move on to um specifying the configuration for our create post function which is basically our post endpoint um and of course it's all going to be under the api.js file i'm just going to call the function create post so create post as well um memory size is going to be really similar 128 uh megabytes and timeout oops download's gonna be five seconds as you can see it's gonna be get get a bit tedious here so i'm just gonna start copying and pasting a lot of this often a good practice in software engineering um so i'm gonna paste that in um of course we wanna make sure we change the http method here and because it's the post endpoint we'll be attaching a request body um so we don't need the path parameter so we can remove that safely and one thing to note here is the action we've got to change this so we're going to change this to put item as the action for the post endpoint and of course the resource is going to stay the same after that we can cop oops my copy and pasting didn't work there after that we're gonna copy this entire thing um and we're just gonna paste it again because we're gonna it's gonna be really similar for our put endpoint um so i'm just gonna call this update post um doing this trying to keep this quick memory size timeout is going to be the same and this is going to be a put end point and because this is a put endpoint um i guess it doesn't really matter i guess it's up to your preference but i'm going to keep this as a the post id as a path parameter for the put endpoint um but yeah feel free to modify that or change that as you wish um so yeah uh one more thing that we need to change is of course the action once again um this in this case we're going to be using update item i believe you can also use put item they they have really similar rules um so if you want to read more about the documentation on that uh it's definitely available i'll probably drop a link down in the description below as well but we're just going to use update item since that is i believe the one meant for put um so we're gonna keep it like that okay and oops i forgot uncopy that so i'm gonna recopy this okay and the last or sorry the delete the second last input we want is the delete endpoint um or i guess the last endpoint the set of crud endpoints but basically this is going to be a delete post i'm just going to quickly change that and of course the delete endpoint we do want to keep a path parameter as a post id because we don't need any request body for this um so we're going to keep that there and as for the i'm roll statement action we just want to change this to delete item instead of update item up there and the resource is going to stay the same awesome okay so the last thing that i want to do is actually create a separate one of separate um and or start separate lambda handler and basically end point um specific to for demonstration purposes this is not going to be a production ready um lambda but basically this is gonna be a get all post so like a batch get retrieval end point um so i'm just gonna call this get all posts um get all posts i guess awesome okay um but basically this is going to be for demonstration purposes later on so that we can actually just quickly see all the posts that are available in our table um but you don't have to include this if you don't want to but i'm just going to include it for now and of course we're going to change the resource path to post because we are getting multiple things at once and it's going to be get again and um be careful here uh because we're getting multiple things um the actual we want to specify is not get item it's actually going to be scan essentially we are scanning um the dynamodb table okay awesome so one we can just quickly hit save um and that's pretty much it for the serverless configuration file thank you for following along i know it's a bit tedious especially with all the laminates that we had to configure but yeah since this is done we can actually go ahead and start writing all our lambda functions and writing some code i apologize i actually realized i made a small mistake um so i actually paused the video um before i move on to subsequent sections just so i can review things like the file that we wrote together um in case we made a mistake um so the mistake that we actually made um here which is actually pretty important is that attributes definitions it's not supposed to be plural here it's just singular so attribute definitions um so just be careful about that um but yeah we're just gonna quickly save that and move forward all right all right so the first thing we want to do now in terms of writing our code and setting up our lab does is create a file called db.js and basically this is gonna help us instantiate our dynamodb client um and yeah so to proceed we're gonna uh with sorry not withdraw my bad we're gonna extract um the dynamo db um client um constructor here from the module that we have imported earlier oh sorry oh my bad i actually haven't installed all the modules yet so we'll just do that in a second um i realized i explained it earlier but i never actually ran the command to install them but anyway we're going to instantiate the client by doing uh calling new uh dynamodb client and i guess you can just pass an empty object we don't actually need to specify any arguments here because this is all gonna be under a serverless application um but one thing that we do need is of course to export this client um so that we can use it uh in our api api.js file later um but one thing that we're going to do now is uh just quickly install all the necessary dependencies i'm going to be using yarn feel free to use npm if you wish um but basically the first thing we need is at um 80 or ampersand sorry this is not ampersand um at aws sdk client dynamodb we also need at aws sdk dash util.dynamo db i'm just trying to make sure i spell all this correctly and last but not least we need serverless im roles per function oops function awesome okay so i'm going to wait for this to install once that's done we'll be back so we're back and all our dependencies are finally installed and because they've been installed we actually need to quickly create a git ignore file and just ignore all the node modules because we definitely don't want to commit all that so this should update in a second i believe um all right awesome it's been updated it took a while um okay so now we can actually move on to writing our lambda functions and as mentioned earlier this is all going to be under a file called api.js so i'm going to name it as such and now the first thing i want to do is import the database file that we have specified just now so required.db and we also want to import the set of commands that we need for basically communicating with our table so we're going to first uh basically import the correct module and this is going to be client dynamodb i'm going to specify the command in a second the other module that we need which is going to be providing our marshall and unmarshall functions is going to come from the util dash dynamodb module awesome okay so because we're using version 3 sdk the format is a bit different than version 2 if you've worked with version 2 sdk functions before basically the way we're going to do this is we're going to actually import a set of commands um and we will be sending these commands um using the send function which we'll see in a second um but basically the commands that we want to import are going to be oops get item command i'm not sure why it's not highlighting it right now but i guess we'll just type it up we also need put item command um we also need delete item command so basically as you can see there's basically a command for each of the operations that we have for our endpoint um so scan command and update item command awesome all right so once we've imported all five uh commands we can now start to write our handlers all right so the first handler we're going to create um is our get post um i'm just going to go off the order i've specified them in the service.yaml file it's going to be asynchronous function and we're going to pass in the event the lambda event as an argument the first thing we want to do is create the response variable basically this is going to be the response that we'll return for our endpoint and the first thing we want to specify is our status code um sorry this is not the responsible return from the endpoint it's going to be the response that we turn from the lambda function um and the body will be what's going to be returned by our endpoint all right okay so the next thing we want to specify is of course our try catch statement or block basically this will allow us to handle all the errors that may arise when we try to retrieve the post inside the try block though we're going to create a variable called params and this is going to store all our retrieval parameters the first of which is going to be the table name and because we specify in our serverless diama file earlier that our table name is going to be accessible in environment variable we can access it as such so it's going to be process dot i'm not sure why it's doing that sorry my intellisense is not working correctly today but anyway process.m dynamodb table name all right awesome um so once that is specified we also need to specify the key and this is going to be an object holding uh basically our primary key or the key of which we're going to be searching for the post which in this case just happens to be the primary key and this is where we need to marshal our key because we're going to be passing a json object or javascript object in general and it needs to be converted into a dynamodb record in order for it to recognize that it's gonna it's a proper key so um we're just gonna pass the jobs uh the json object uh within the marshall function uh basically we're gonna specify the post id field and we're going to extract the post id from the path parameters under event dot path parameters dot post id awesome okay i'll just put a comma there and semicolon there and now we can actually try to call the function to get the uh get the post um and this is the item is gonna be available under the item field so i'm just gonna deconstruct that um we're gonna of course await on this call and it's gonna be db.send and remember db is the um client that we have instantiated earlier so it's going to be basically the client function uh module and we're going to call the send function and we're going to create a new version of the get item command and we're going to pass in the parameters in there as such this is just the syntax of the v3 functions and how they're supposed to be used and you'll see this in a second that's going to be really similar for all the other commands later on awesome so once this is uh basically once we have retrieved it i guess we can do a quick log and just say um i just wouldn't just log the item um in an object i guess um awesome okay and of course we need to return it and this is going to be under the response body and because we are using a lambda proxy integration we need to make sure that the response body is stringified um so just be careful of that we can also include a quick message into the response buddy as well so we can just say successfully retrieve post and the data which is going to be our item and i guess here uh we need to check actually if the item exists so if the item doesn't exist and we couldn't find anything this will actually be undefined so we need to make sure that it's defined first and if it is defined then we call the unmarshall function and pass in the item and this will help us turn the item back into a javascript object or json object so that we can return it in the response but if item is not defined as if we didn't find anything we'll just set it as an empty object and we don't need to unmarshal that or anything um awesome and then if we want to see the actual like item itself in java in dynamodb record form um we can just log include that in the response as well so you can see the difference between two all right and so for the uh catch statement um i guess we can just include a quick error message um to log it we also can set the status code to 500 on the server-side error and of course we also need to specify uh the response body and we can just change this to i guess like fail to get post as the message and we can include fields such as the error message field of course none of this is necessary or or yeah none of this is necessary but it's good practice it's nice to see what went wrong especially when you're debugging so i'm just going to include this and feel free to use it in your code as well so we're logging the message as well as the stack trace of the error that was generated and awesome that pretty much wraps it up the last thing that we need is of course to return the response object and yeah so that's pretty much it for the get post lambda handler and now we can go ahead and copy most of this and proceed with our create post lambda handler so i'm going to paste this in i'm going to quickly rename that to create post a lot of this is going to stay the same of course the body the sorry the request parameters are going to be a bit different because we're going to be doing a different operation here um one thing that we do need here before we modify the parameters is actually uh we're going to parse the um event body and basically this is the request body that we will be storing into our database so the post itself and all its properties and whatnot we also need the table name for the parameters but instead of key here because we're not trying to search for anything we can just specify item because this is going to be the item that we want to store into the dynamodb table and similarly again we also need to marshal this because we're going to be taking in the request body which is going to be coming in as javascript or our json format so we can actually just we just pass this in and if it is defined i guess this is just some error handling if the body is defined pass it in if it's not we'll just pass an empty object i guess maybe you might not want to store an empty object depending on your use case but um yeah this is just how i'm just i'm going to code it up feel free to modify this as you need um next step here is we're going to change the command that we're actually sending obviously we don't want to get get anything here um the result we don't need to deconstruct anything so we're going to change this to create result we also want to change the command to put item command and we can just keep this as parameters which is what we're going to be passing in all right we can delete this log here and we can also keep most of the response body uh messages um so successfully created post i guess we can just change that um the data um uh we don't really have any data because we're not retrieving anything so we can delete most of this and but what we can return is also the create result just so we can see uh what's being returned by dynamodb awesome and we can just quickly change the message here as well fail to create post um and the rest can stay the same so as you can see that was pretty quick uh we just had to modify a few things uh here and there um and yeah now we can copy this function and proceed forward with our update post or put endpoint lambda handler and to get this started we can just quickly rename this function to update post and um a few there are actually a few things we need to change for the update function uh which we'll go over in a second but first things first um besides the event body which is going to be the request body that we're going to parse we also want to create a new variable called object keys and basically we're going to uh obtain all the keys from the request body and we're going to use this in a second we also will not need item because we're not adding a new item to the database we're simply updating an existing item so we need to actually find the item we want to update and so we're going to use the key field instead and recall from earlier when we use key we need to make sure we marshal a json object we pass in that holds the key and in our case we only have one attribute that is going to represent our primary key or the key that we're searching by and that's going to be post id which is passed in the path parameters of the endpoint and it's going to be called post id awesome and so the next part this is where it gets a little tricky this part uses something called the update expression which is um specific to dyno with db and basically what it is is that it's like a special string expression that allows us to perform updates on to existing or existing um documents in the table and um ideally i've pasted it here i've pre-written the example uh we can just go over it but basically uh here uh this is for example if they had like an attribute called session id to store their session id um this is the alt this is the expression that we want um there are different variations of it for example you can have set you can have remove um in this case set will only allow us to add or modify existing attributes so essentially will not be able to delete any existing attributes from any existing documents but that's fine depending on your use case you might need to tweak this a little bit but i want to keep it simple without adding too much logic the other field we need is expression attribute names um basically this specifies the attribute name and of course we also need the expression attribute values um and this just holds the type and the string or sorry the value itself so ultimately we want to obtain this format but because we don't know we want to keep things dynamic we're gonna have to write some code um i actually uh obtained this solution from a stack overflow post and just modify a little bit so i'm just gonna um paste that in here to save us all some time and i just want to make a quick note the reason why they're using a pound sign here is because um uh dynamodb obviously has its own set of reserved keywords and this basically allows us to escape it so depending on use case and depending how you name this key value here or essentially this value here um you might need to escape it by using the pound sign the rest is just the convention and the format that they want you to follow um so yeah i'm gonna leave like this uh feel free to read up more on the documentation if you would like but uh i prefer for now just to move forward so we can speed things along a bit more um awesome so once this is done we can go ahead and actually make the call to perform the update and um instead of create result here we just call this update result and of course changing the command to update item command and we're just going to pass in the parameters as usual and for response body um our message is going to be a bit different we can say successfully updated the post hit save as for the error message we can just say fail to update post and i guess yeah the rest should stay the same awesome so once this is done we can actually go ahead and copy this once again um and paste it below so we can uh write or delete uh lambda handler um so i'm just gonna call this delete post um awesome okay so as for the delete lambda handler we don't need a lot of this um update expression stuff because it's not that complicated it's pretty much uh like the get endpoint we're just gonna get the post id from the path parameters as we have specified earlier we don't need to get the keys from the op request body um we don't actually even need the request body at all because um we're sorry i'm trying to get highlight the whole line there because we don't need um to basically update anything or pass anything into the database we're simply deleting it so we just need the post id and parameters and then we just need to change the command to delete item command pass in the parameters and that's pretty much it um we'll just change the name of this variable here to keep things consistent delete result uh and we can just paste that in and we can just say successfully delete a post fail to delete post awesome okay and that is pretty much it for the delete post as you can see here copy and pasting is really helping us save the day of course there's probably refactoring optimizations we can make here to prevent duplicate code i'm just going to paste this out here so that paste them all out individually so that you can feel free to modify the code yourself fork the repository and just like twink around with it to however you need um but yeah but last but not least what we need to do is create our get all post function and this essentially will perform a scan on the database and get us all the necessary values that we uh desire and basically what we want to do here is um we actually need a parameters object here i guess it's a little bit um redundant or a little long we can just keep it simple um but what we want to do is deconstruct the result and get this field called items and make sure that items plural because after all we're getting multiple items at once or at least an array of them and then the command that we'll be sending is called the scan command which we have imported earlier and as for parameters we can just create a simple object here because we don't actually have a key or anything we're just scanning an entire table all we need to specify is just the uh dyno db table name itself so we can leave it like that awesome and that's pretty much it so one issue with this um get all post function that um i want to bring up and which is the reason why i didn't i was hesitant about um actually including in this video is that uh the way that the scan command works is that there's actually a threshold in terms of the amount of data and its size that it can return in one function call um i forgot the limit maybe it's like a one megabyte or something like that but the uh one way to circumvent that or basically fix that uh fix that issue is to use one of the fields that are returned by its output and that field is called i'm just going to type it here last evaluated key i believe um so it should be a field returned by the command the send scan command and basically that will allow you to perform multiple or subsequent scans based off of where you first where you left off um so yeah if you have a huge table with many documents you'll need to do that and pretty much fix this function but because we're not going to be inputting a lot of stuff i'm just going to make one scan call and that should be sufficient for our use case for now but just keep in mind this is definitely not an ideal function this is just for demonstration purposes but yeah just small things here we can just quickly change the message once again successfully retrieved all posts i guess and failed to retrieve posts awesome and uh once again um this part here because we are retrieving posts and items uh we need to unmarshal this again because it's gonna come back as a document oh sorry not documentdb dynamodb um format so we're going to go ahead and turn all every single element items into into an r marshall or basically a javascript object um so to do that we can do items dot map and we'll just pass the item into the callback and we're gonna return the unmarshalled item awesome so that is pretty much it as for the data field of course you can also log items in its raw form as well so i guess we can do that um yeah and we can go ahead and take a look at what it looks like when we test the endpoint and awesome that pretty much wraps up uh all our lambda handlers um one thing that we do need to make sure we do uh we include is to export all our functions um just so that uh they have access so i'm just gonna quickly um shorten everything here i apologize you can skip ahead right now if you'd like um but basically i'm just gonna copy and paste all of these so that we don't make a typo or anything and i'm gonna export all of them and awesome okay so once all these are exported now we can finally move on to deploying um the entire project basically and then we can go ahead and test all our endpoints and so we're back and the diploma has finally succeeded as you can see all the steps in the github actions workflow have succeeded and so we can go ahead and head over to api gateways console and you should be able to see your api listed under apis and if you want to head over to dynamodb as well you should also see uh your table having been created here um awesome so if you want us we can just quickly validate that um there's actually nothing in our table right now no items here um so now we can actually start to test our endpoints and to do so a quick way to get the base url is to just go under stages on the left side menu and click into dev and this will get essentially get us our base url our endpoint um if you want the actual endpoint itself with the altogether you can you can just click into it and then paste copy that as well so i'm going to be using postman here to test it i hope this is large enough um i've actually already pasted it in here as you can see it's the exact same one with the same id just for demonstration purposes and to speed things up i'm just going to click send to show that there's no post with uh post id of one um this is the batch endpoint um there's no post at all right now and now we can go ahead and actually just quickly create one um i've tested it earlier just to see if it works um and we should create a new one i'm not sure why that took so long um but anyway it has succeeded um so we can quickly use the get endpoint as you can see the result we get both the json version basically the unmarshalled version as well as the marshall version which is the dynamodb copy or format version and as you can see the format is pretty different um which is really great that we have these unmarshall and marshall functions utility functions provided for us otherwise we have the form of the ourselves um but this is what it looks like if we use a single endpoint just querying on post id of one uh this is what we get as well just a single object as opposed to an array um an array of objects um yeah if we go ahead and try to update this um i'm gonna copy uh this request body here and use it in our request body for the put end point um and be careful because of the way we implemented is we're going to be passing the post id as a path parameter and not the request body um so we need to remove this actually otherwise it will throw an error um because you cannot be updating primary key like that um so uh now we can go ahead and hit send um actually sorry we haven't actually updated any information so it wouldn't appear as if it as if it updated so i'm just gonna put like uh no author here maybe uh and change the views to like ten thousand um if we hit send this should successfully update and if we just go to the get endpoint we can see that the author and the views have updated but everything all stays the same which is the exact behavior we have we have specified um and yeah just to wrap this up i guess we can just quickly delete this um so we can hit send on our delete endpoint of post id 1 and it should successfully delete it and if we try to retrieve it again it is clearly gone and yeah that's pretty much it for this tutorial kudos to everyone who stayed to the end i know this tutorial must have been a pretty long but i really really hope you learned something or found this video helpful and if you did please do consider hitting that like and subscribe button down below as it really helps with the channel and series an incredible amount and as always i hope you're all staying happy and well out there but for now take care
Info
Channel: Jackson Yuan
Views: 1,570
Rating: 4.9298244 out of 5
Keywords: programming, tech, coding, node.js, aws, amazon web services, aws lambda, dynamodb, api gateway, REST API, CRUD API
Id: hOcbHz4T0Eg
Channel Id: undefined
Length: 44min 0sec (2640 seconds)
Published: Fri Jun 18 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.