Build a Node.js Express CRUD API with DynamoDB and Host it on an AWS Server (Elastic Beanstalk)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what's up everyone welcome back to another episode of aws tutorial and today i'll show you how to build a nodejs api with the dynamo database and then host it on the elastic bean stock but before we get to the implementation i want to talk about what exactly elastic bean stock is so it's basically an aws service that uses ec2 instances under the hood but one of the benefits that it has over traditional ec2 instances is that you can let it manage all the devops stuff for you with no extra cost such as provisioning the ec2 instances doing all the load balancing auto scaling and doing all the health monitoring for you as well so this is like an ideal candidate for beginners because it's very easy to get started with but with that being said if you choose to you can still have full control of your resources now that we learned about elastic bin style let's build an api and host it there all right so right now i'm on the homepage of the aws console so what we're going to do first is we're going to create a dynamodb table to host our data and then we're going to write a code for the api and then finally we're going to create elastic bins dock to host the api so let's type in dynamodb open that into a new tab hit create table i just call it product inventory and then for the partition key i'm just going to use product id and i will select string as the type you have the option to add the store key here but for simplicity i'm just gonna ignore that and then use everything else as default create all right so it's done and now let's move on to step number two it's the right api so i'm just going to open vs code and then navigate to the folder and you want to save the project i'm going to create a file called package.json and then define the project name as node.js crowd api and then author just type in my name version is 1.0.0 dependencies we're only going to have two dependencies here one is the aws sdk and i believe that is the latest version and then the other one we're going to use is called express and that is the latest version as well and now i'm going to create another file called app.js so this is going to be the entry point of our api first thing first i'm going to import some libraries and then i'm going to define app express and i'm going to define a variable called port all right so this line here is very important because we're gonna host the api on elastic bean stock and we're gonna use nginx as our container we're gonna let nginx to use the default port throughout the traffic but this is gonna be used locally when we test it and then i'm going to define the product router don't worry we're going to define this file later in a minute and then we're gonna let app to use express.json and we're also going to let app to use the product router and the path is just going to be products and now let's define some basic get methods here first so this is going to be the home page we're just gonna return some random thing we're gonna define h1 tag that says node.js cloud api and then let's have a h4 tag that says the message success and then have a paragraph that defines the version number we're also gonna have a health check which is also a get method it sends an empty body and then finally we're gonna have the app listen to the port that we defined and then just log out something like demo app it's up and listening to port all right so that's it for the app.js file and now we're going to define this product route so we're going to create a new directory call routes and then inside this folder we're going to call it product dot js one thing to note here is when we define the router we don't have to write out dot js at the end all right first thing first we're going to import some libraries here so constant express and then we're going to define a constant called router and then we're gonna import the aws sdk and then we're going to update the region to be usc 1 and the reason for that is our dynamo table is in the east region and now let's define the dynamodb clients and then we're going to define the table name as a constant and i believe we call it product inventory and now we're ready to define the product methods so the first one is going to be a get method but we can get a single item from it and then the second one is also a get method where we get all the products in our table and then the next one is a post method where we can save a new product into our table and then the next one is a patch method where we can update the item in the table and then lastly we're going to have a delete method that we can use to delete an item on the table and then finally we're going to export this as a module call router and now we just need to define the details for all these methods but for the interest of time i'm going to speed up the video but include a link down below where you can find all the code so let's get started [Music] so [Music] [Music] so [Music] so [Music] so [Music] [Music] so [Music] so [Music] [Music] all right so i just finished writing the code but before we move on to the next step i want to point out a few things the first thing is that for all the callback functions i use an async function and the reason for that is all the dynamodb operations are async functions so i have to call it async functions here as well and then the second thing is for the error handling i'll just lock them out right here and then send the 500 back to the client but you can do your custom handling here if you want to and then lastly in the method when we try to query all the items in the table i defined a custom function called scan dynamo records which is defined here and the reason i do it this way is that when dynamo performance scan on the table it may not be able to get all the items all at once because there's a size limit on it and if this time it cannot get all the items in the table it's going to return something called the last evaluated key which means that is the point it ended so we're going to add that as a parameter and then do the scan again and we're doing it recursively here and now let's test our app locally before we deploy that to elastic bin stock so i'm going to go to terminal new terminal and then do node app.js enter i forgot to install the module so what i can do is npm installed and what it does is that it just can install everything that we define in the package.json file and then create a new folder with all the libraries so now if we do it again it's up and running and now let's go to our browser open a new tab open the dev up tool and then network close everything and then type in localhost 3000 all right now we can see our app's running and then this is 200 means it's okay and then if we go to the health it's also 200 so we get to move on to the next step and now let's go back to aws i'll type in elastic beanstalk open tab into a new tab create application for the name i just call it jane meister crowd node.js crowd api for the platform i choose node.js and then for the runtime i just choose node.js 12 because i think that's what i have in my local machine yep but you can choose node.js 14 if you want to and then i'll upload my own code and i can just go back here to remember for the node modules this is what we installed when we test locally but when we upload that to elastic bin stock we don't have to include that into our deployment package because when elastic beanstalk sees the package.json file it will just install the module for you automatically and we don't need this id there's one more file we need to make before we can create a deployment package it's called a proc file so this is the file that elastic bin stocks will look for in the deployment process so that it knows what command to run to start app technically if we name our entry point to be app.js or index.js we don't have to create this file because elastic bin stock will look for those files automatically but if you name it differently you would have to use this file to tell it what to run to start the app but that's a good practice i'm going to use it anyway let's just do web note app.js so it's basically telling elastic bean stock to run this command to start the application after it gets deployed and now we're ready to create a deployment package so let's go to the folder select everything compress one thing to note is that all these files have to be in the root directory so you have to compress it this way and now let's go back to elastic beanstalk local file and then choose the file that we just created is this one hit open we're going to leave everything as default for the configuration but let's take a look at it and see what setting you can change in here you can choose other instance instance type but i'm gonna i'm just gonna choose this one because it's free and then you can change to run time software you can you can change the proxy server to be apache i'm going to use nginx and other things you can do and then you can also change the load balancing setting and then security groups and stuff like that i'm just going to leave everything as default for this demo so create app i think this is going to take about 15 minutes i'm just going to pause the video and then come back all right so it's been 20 minutes and seems like the creation is done and now let's look at it and see if it's working so if i click on go to environment and it seems like it's working but that's one more thing we need to do before we can do operations on the dynamodb which is to give the ec2 instance permission to operate on the dynamo table and we're going to do that using the iem policy just go to configuration and under security you may see that there are two rows here one is the iam instance profile and then the other one is the service row so the service road is being used by elastic bin stock to create the ec2 instance and this is the actual row that is being used by the ec2 instance to do the operations so we're going to add it to this one so i'm just going to copy this type in iam open that into a new tab and then paste it here alright so this is it i'm gonna do attach policy type in dynamo so for this demo i'm just gonna give it full permission all right so it's done go back to elastic beanstalk click here all right so this is the endpoint for our api so i'm just going to copy this and then open postman so do http and then paste it here let's do a get request on the homepage now we're getting that and then check the health it's 200 so it's fine and now let's do a post to create some items on our table we need to have a product id and there needs to be a string and then let's do color let's do red and then price 100 before we hit the request let's go to dynamo table to make sure that we don't have anything here first so it's empty so right here send oops should be product remember that is our path that we define here so let's do send again missing required key item in the parameter there might be a typo let's see post item items now let's deploy that one more time to go back to elastic beanstalk this is good so i can show you how to deploy that again so just hit upload choose the file and that is the updated version of the package hit upload version two deploy okay it's done i need to refresh on it is using the version 2 code now let's go back to postman and do it again alright so it's successful now let's go back to dynamodb hit refresh we see one item here let's save let's create a few more blue 200 and then three 500 and now we if we go back we should see three items here and there you go now let's test out the get method for a single item let's do get and then remember when we write the code for the get item we are getting the product id from the query parameter so we're gonna pass in so we don't need this the parameter to be product id you can either do it here or just do a question mark and do it over there too and that should return the second item yep and then if i do the first one it should return the first item and now let's get all the items so we don't need this and for getting all the items i remember we say that we're going to have all at the end for the past and it should return all three items to us and yes and now let's modify one of the items which is a patch method and we're going to pass in a product id let's see what we're going to pass in first we're going to pass in a product id and then updated key and then update it value from the body product id let's change the second one we're going to change update key we're going to change the price and we're going to change it to 1000. so right now the second item is 200. if i to send it's gonna change it's gonna be changed to one thousand dollars it's successful if i refresh it's one thousand dollars and right now let's delete the third item and i believe we passed that in as the body as well and the method is going to be delete now we have three items here if i to send we're going to have two left so this is successful and if we do a refresh we're going to have two left and there you go one last thing i want to mention is that inelastic bean stock if you want to look at the logs this is what the problem is if you have any you just click here and then just do request log hit here and you'll be able to download the logs for the app let's take a look so in here you see all the system logs as well as the ad blocks and then if you want to update the version i mean the code you just create a package and then hit hit upload and deploy and then you're done and that is it everyone i hope you have learned something if you like this video i hope you can give it a thumbs up and i'll see you in the next video
Info
Channel: Felix Yu
Views: 2,257
Rating: undefined out of 5
Keywords: AWS, CRUD, API, Node.js, Express, DynamoDB, AWS Server, Elastic Beanstalk, EC2
Id: YJvXHr69AHg
Channel Id: undefined
Length: 26min 4sec (1564 seconds)
Published: Sat Mar 27 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.