How to Implement Rest API using AWS Lambda, API Gateway and NodeJs

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello friends welcome to this session let us learn and grow together in this session i am going to demonstrate how to implement basic rest api using aws lambda api gateway node.js along with that i will also show how you can monitor logs in the cloud watch and how you can test api using postman tool let's get started first of all i am creating the lambda function that will be a serverless function to execute our code in different run time so here you can select lambda by using search bar or you can go to the services from this compute section you can click on lambda click on create function and there are different options during creating function but we are going to choose this scratch one so we can create a lambda function from scratch let me give the function name get to do here we have a different runtime option like python go and many other languages for now i'm selecting this node.js 14.x architecture options we are keeping as it is default one here we have a option to select the execution rule by default it will create a new role with basic lambda permission and this this role will be required lambda will need a permission to write logs into the cloud watch log group so that is why we need some permission relevant to that so we are keeping that default one in advance i think we have this different option relevant to network and code signing that we keeping as it is for now and and we creating this get to do default function so once we are done with this function creation let me show you the detail regarding that so we have here we have implement like created this get to do function and here we this function have a node.js runtime default code with the handler function and we can see like here it will have written the json response uh with data score 200 and somebody having some tests uh like a dummy text like hello from lambda in the response so this will be a basic node.js uh code and and and here we are able to test this lambda function by clicking on test and we can create a events test and so it will so here uh like like even created so we are able to test it and it show like hello from lambda function with the latest 200 so this function is working fine uh like here we can also test in this section uh and it is showing status 200 and hello from lambda so this function is executed standalone without any trigger in monitoring section we have uh like uh we are able to see the locks relay one to lambda like in when we check in the locks it will so um lambda function execution relevant log in the cloud watch and so when we creating any lambda function by default one log group will be created say it it can you can see like get to do function we created so this by default log group is created along with that and in this we are able to see the uh different execution we we run this function two times so it shows two locks here along with the different detail like the time duration and memory occupied for that we are also able to see this x-ray tracing and all those things if we enable that particular settings in configuration we have like default 128 mb default uh memory allocated and timeout of three seconds by default at the moment there is an only trigger um link with this lambda function so right now not nothing so here and in permission we can see like uh this is the role uh which is created default uh as a part of creating a lambda function and that it will have a like a permission relevant to cloud watch log uh relay one so if we can see this im uh role there will be a permission attached to it and we can say we can see this permission here you can see like we this this this role have a permission allocated to allow create a log groups and create a log group stream and put a log event so whenever a lambda function execute it will write a logs into the cloud watch so that is why this permission needed with the lambda function when we created these are the different setting like environment variable related one setting and many settings are available for now we are keeping it default so not using this setting for now and this trigger we will use as a this trigger we use api gateway as a trigger so let us go to the api gateway and create the endpoint here you can search this api gateway so from this api gateway section uh and there are uh api gateway allowed to do uh implement many different types of api say for example http web socket rest api private and public so for now i am creating this rest api public and i am selecting the new api and i am giving the to-do api name as a to-do api okay and i'm creating that api so after creating this api let me create the resources we need to click on this action and create resources and we can give name to do to the resources and let us create the resource once we are done with creating resource we need to create a method say for example we are selecting get method and clicking on okay so we need to link this method end point with the particular lambda function or any other uh different option integration options like we are able to link with other services some mock or http or vpc link but for now we are integrating this with the lambda function trigger so whenever this api endpoint function call it will trigger the lambda function uh so let us link that our already created lambda function get to do here and click on save so here it asking like api get we will need a permission to invoke lambda function this so when we click ok so it will auto give permission to the api gateway to execute the lambda function so here we done with like basic setup of creating api gateway endpoint uh and we link that endpoint uh with the lambda function uh get to do so here also we are able to test this api uh which we created basic api so for now no need to pass any data so we can test it we can see here we get the status code 200 and body hello from lambda so this is the this is this json response return it from the node.js code by lambda function and now we want to run this this api endpoint externally from postman so for that first of all we need to deploy this api so for deploying this api we need to go into the action and deploy api and we need to create new stage say for example dave stage i am creating and deploying this api so now we deployed this apis so we we are able to uh test that api by using postman so this is the end point i am taking this endpoint and opening the postman tool this postman tool is a very good tool to test the apis also there are many features available in this tool like we are able to do the create a collection of the api and we are execute that api uh whenever we require so this is a very good tool for api testing and all those things so let me paste here our endpoint which is i copied from api gateway uh our created function and i am executing this endpoint so here you can see i'm getting that response status code 200 and body hello from lambda and if we can change anything into the lambda function then it will be apply over here in the um in the response so let us cross check and edit that function so here we let us change it i am changing the tax [Music] i am changing the tax in the lambda function and deploying it make sure you deploy otherwise it will not have a impact and once it deployed let us run this uh api endpoint again in the postman so here you can see like we get status code 200 this is a basic first api so like we get this response whatever we and written into the node.js code over here in the lambda handler and that come into the response so here we demonstrated how we can create a lambda function how we created the api gateway and how we can link that with the uh how we can link api gateway endpoint with the lambda function uh here also you can see like in lambda function uh last time we see there is a nothing so into the triggers but now you can see like a trigger will be so api gateway as a part of uh like to do this lambda function so we link this lambda function with the api gateway that is why it's so here uh that way and uh again like uh whenever we are running this uh api from the postman it it will uh do the locks into the cloud watch so uh say for example we want to monitor some log then we are able to check that those login cloud watch you can see like these are the different locks relation to our execution let me console something and we can execute that again so i am doing some console called i am doing some console logs so it will be comes into the cloud watch log so i put this uh console log and deployed so now i'm again running this api in the postman so now we can cross check the locks in the cloud watch and let us see like whatever we console that should be come into the locks so this is the latest one we you can see like we have logged this logs monitoring demo this text we logged into the console.log so that will become into the response i mean this in the cloud watch log so this we are able to monitor the locks of api and this way we are able to implement it so together we learn here aws lambda api gateway uh and node node.js basic things and like cloud watch monitoring along and also how we can run api using postman so this is way by this session uh you uh following you are easily able to start how to implement a basic api in the aws hope you like this session uh keep it following and let us learn and grow together keep keep sharing and keep liking this this session thank you for your time and thank you for watching
Info
Channel: Learn and Grow with Jaymit
Views: 668
Rating: undefined out of 5
Keywords: How to Implement Rest API using AWS, AWS Lambda, API Gateway, NodeJs, CloudWatch, Postman, AWS, api, restapi, hands-on, demo
Id: fHihzz_RKKo
Channel Id: undefined
Length: 13min 50sec (830 seconds)
Published: Tue Oct 05 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.