Building a REST API in AWS with Node.js and Serverless Framework

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys what's up this is aj of bridging code and for this tutorial we're going to build a truly serverless rest api in aws using node.js and the serverless framework if you're watching this tutorial i'll just assume that you know what serverless framework is or from familiar with it to some extent but just in case you're not serverless framework or better known as just serverless is a new way of building and deploying your serverless applications with relative ease and less manual work they support a variety of cloud computing providers including the top three companies which are amazon web services microsoft azure and google cloud platform so what we're going to do today is to write a restful api with node.js and use the serverless framework to define the cloud formation stack that we will contain the aws resources or services that we will utilize and deploy it over to aws a rest api is going to be a to-do app which will create a task list down all the created tasks get update and delete a task through lambda functions and store these tasks in a dynamodb table and expose this rest api through aws api gateway now before we get started with everything please make sure that you already have serverless installed in your machine if you don't have it installed yet i have pasted a link to the instructions on how to install serverless for me the easiest way to install it is to run npm install g serverless on your command line or terminal assuming you also have node installed on your machine already which you should since the app that we will be writing is a nodejs besides this you will also need to configure your aws credentials if you don't have it configured yet with the aws configured command and enter your access key id and secret access key which you can obtain by creating a new user in im for me i just gave my user programmatic access since i don't have to log in to the console with it and assigned it an administrator role since it is this is just a demo but in reality you would want to give your user a more restrictive access for security reasons now to set up our project we open our terminal and type serverlesscreate for this app we will use a serverless template called aws node.js and we also specify the name of our app project and the path now that we have that created let's open this project on visual studio code and as you can see here are the files that were generated for us by serverless based from the template that we used with the most significant file being the serverless.gmo where we will configure the aws services that we want to use or involve for this app like dynamodb and of course the lamp functions that will process our http requests but before we start working on this file let's get some of the packages that we will need like uuid to generate a unique id for the task that we will save to dynamodb and of course the aws sdk package for node.js now let's go back to our serverless file and delete these unnecessary comments first under provider i will set the region where i want to deploy the rest api and that is ap dash south east one or the singapore region because this is the region where i'm closest to next also under provider we will configure the role that our lambda functions would have which is to allow it to describe and query the dynamodb table and get put update and delete items in it we will also set the resource to allow these actions for all dynamodb table under the region we just set which is ap south east one next is we will create a dynamodb table and set the deletion policy to retain so that our table doesn't get deleted every time we build and deploy with serverless we will also set the primary key of this table to be an id of type string set our read and write capacity to just one since this is just a demo app and we don't really need that much and name our table to do's configure the lambda function to create a to do task here in the serverless file specify the path of our create to do node.js code and this will also essentially create an api gateway trigger to invoke the lambda now let's write the code to create a to do task we will import two packages of course the aws sdk to write the code to save the task to dynamodb and the uuid which we will use to generate a unique id for our task we get the current date and time as an iso string for the created and updated date of the task get the request body let's also check if the task we're trying to save is a string if not return an error response message task is not a string now let's set the parents that we will pass the dynamodb to create the task will it take with the table name being deduced and the item which is uh the task that was extracted from the event body date time updated at daytime now we call dynamodb's put function and pass over the params and a callback function return an error response if something goes wrong turn and return a successful response with the same task data dot items callback null response okay now let's build and deploy this code over to aws using serverless by running serverless deploy this may take some time depending on the speed of your internet connection or how close are you to the region that you are trying to deploy let's grab this api gateway url that was created for us by serverless and paste it over to postman to submit a post request and then let's try to submit a task jason task do homework okay it appears that we have successfully created a task and if we go to dynamodb table that was also created for us when we ran serverless deploy um as you can see i was also able to save the task here next is we're going to configure a lambda function that lists down all the tasks we created and we're going to start off by copying and pasting this existing configuration i'm going to rename this list specify the path of our list to do code and this time it will be a get method i'm just gonna copy and paste this gray to do file since uh they will be fairly the same i'm gonna rename this to list to do let's see let's rename this to listed two of course and um delete all that unnecessary code let's delete this too since we'll only need the table name and let's change this to scan now ideally you will want to design your dynamodb table to be able to use the query function rather than the scan function because it performs more efficiently but for the purposes of this tutorial we'll just keep it quick and simple by using this scan function if you want to read more about this it's in the aws documentation let's set the status code to 200 and finally we return a list of the created items now let's build this code again and as you can see we now have a new api url to get a task let's paste this over to postman and as you can see it lists down all the tasks we created earlier and if we create a few more tasks eat lunch another one [Music] clean let's try it again and as you can see it is able to list down all the tasks that we just created next is we are going to create the lambda function to get a specific task using the task id so again i'll just copy and paste this we name it to get get to do get to do this time we are going to add a path parameter called id that serves as the selector to get the task we want still get method again to save us some time i will just copy and paste this list you could as they are fairly similar let's rename this to get to for the params besides the table name we will also need to specify the primary key as selector which is the id and we are going to get that from the path parameters this time we will use this get function for the response let's return a status code of 404 if we didn't find any task with the specified id message task not found let's remove that uid since we don't need to generate a new id and let's build it oops sorry i forgot i forgot to rename this should we get to do okay let's build this again and as you can see this is the url of the new get api that was created for us by serverless let's copy this piece it over here let's choose a task that we want to fetch and get this id and as you can see we were able to get the task with the specified id let's get another one and there you have it for our next lambda function should be able to update a certain task rename this to update update to do this time it will be a put method because we are updating an existing item let's copy and paste the get to js so it's faster we need this to update to do let's remove that update to do for this one let's also check if uh done is still a boolean if not return a an error response let's set in the key the id of the task we want to update and we put in the expression attribute values which is a collection of placeholders with the updated value and we only intend for the task and done attributes to be updatable and of course the update expression containing the placeholders in the expression attribute values let's call the update function having the same arguments as before let's change the status to 200 and return the updated task and finally let's build it again and as you can see this is the url of the new put api let's copy this and paste it over to postman let's again choose a task we want to update and get its id let's delete that because only task and done will be updatable let's add the word done and this should now be true seems okay and let's get this task and as you can see we were able to update this task lastly we also want to be able to delete a certain task we need this delete delete to do delete to do and of course this time it should be a delete method let's copy and paste this get to code rename it to delete to do everything will basically be the same except let's just hold on let's delete function probably don't need that let's just return an empty object we need that to delete to do let's see let's build this code again let's go ahead and copy this and paste it over to postman let's have to delete let's again choose a task we want to delete submit submit okay it seems that we have deleted the task so if i try to retrieve this task it's 404 and we didn't find anything because we deleted it lastly i just want to show you what serverless created for us when we deployed with it and api gateway of course it first created a cloud formation stack because this is the template containing the aws resources we need and here in aws lambda here are all the five lambda functions we just created for our rest api and here in api gateway here are all the five endpoints of our rest api so that's it for this tutorial hope you guys liked it if you have any questions feel free to drop them in the comment section and i'll be sure to make time and answer them don't forget to hit like and subscribe and thanks for watching bye
Info
Channel: Bridging Code
Views: 6,603
Rating: undefined out of 5
Keywords: Serverless, AWS, REST API, Node.js
Id: 2hwzFMoj-dA
Channel Id: undefined
Length: 26min 10sec (1570 seconds)
Published: Wed Oct 14 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.