Building Your First REST API with AWS Lambda and API Gateway using JavaScript: A Beginner's Guide

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello friends welcome to the tech expert channel today we are here to learn how do we build a rest api using amazon api gateway and lambda function this is really interesting and very simple you can build your own rest apis just like that using these two prime services provided by aws to learn this process let's take an example of building an api for student record so i'll create a rest api for get method and post method wherein you'll be sending a student details to the pack and function through post method and you can get the student record from the backend functionality to your front end using the get method and we'll be knowing how do we configure the api gateway and also will be creating a lambda function which will handle the get and post method implementation and then later we will integrate the both kpi gateway what we have configured and also the lambda function we have developed so then once integrated we'll go for under testing through postman or browser how the data transfer happens and just like that api gateway can be start using without further ado let's get started so this is the aws console which i already logged in so let me start with configuring the api gateway so go to the search box and search for api gateway and here you see click on the api gateway once you come down you can see that there is an arrest api built environment click on the build it will ask you to create a rest api using the api gateway with the example so let's take um a new api and then let's give the api name so maybe uh i'll give the api name as uh student and description you can leave empty or you can say student records so it's an optional one and keep the endpoint region to default and click on the create api so once you click on the create api it will create an actual api within the api gateway so now click on the actions and create on the resource let's create first resource and under that resource we'll create the apis so let the resource be student and below if you see this is the path created for the accessing this resource click on the create button again so it will create a resource so under the student resource we are going to create first the get api so if you see here is a great create method let me click on the create method and drop down window it will allow you to select the api uh different methods so let me select the get method and save so once you click on the get method save you will see this window so where you can integrate with the lambda function or you can integrate with the mockup or http anything so let's first make it as a mockup so that will later integrate with the lambda function so this is my first get method and i will also create the one more method for the post so click on the post and say save and let's again select the mooc and save so mooc will allow you to just mock up the uh implementation of the methods so now that we have the uh student resource and two methods created here get and post so as of now they are all both mocked up so you will not see the actual implementation so to see the actual implementation we have to go and create a lambda so let's go to services and search for lambda so here is lambda click on the lambda function so let me create a lambda function by clicking the create function so i will go for author from scratch so let me create a lambda with the function uh student records okay and i will use the runtime as in node.js and let me keep the remaining stuff to default so when i click on the create function it will take some time to create for the first time here you go the function is already created the function name is the student records so let me open the index.js let me implement first the get method to check the uh get method i will check with the event received so if event dot http method equals to get okay so what i'll do i will call the uh get student record okay this is the function i'll be implementing soon so i'll be passing the event details as part of this function call okay and let me implement another function for first put if when dot http method is equal to equal to post right i will implement create student record okay and this is my second method so let me implement this two method over here const get record or maybe i can implement them as a function get student event and here you go so under the get student record i have to pass back the uh student details so let me create an uh temporary student detail so that i can pass it right i'm not connecting to backend database so let me create a student object and add a first name as sam and last name sunder and roll number is 1.1 okay so these are the fields i'll be uh creating for student object and now that i have to pass back right i need to create a response const response so status code will be 200 and body will be student i'll be passing let me make it uh jason string if i write and student details right and make it student right so this is what i'm going to written when the get student record has been called okay so let me take this one and then written the response let me return the value pressure from here same thing over here okay let me knock out this code so that it will not be required now so let me implement the create student record method as well here function take the event as a parameter and i'll be implementing um now that it is in a crate record i'll be getting the um i will be getting an event along with the body so let me pass the body as so let me take the json help for the parsing and i'll pass the event dot body so i'm capturing the past data into the body so let me um create a response code const response is equal to status code should be 200 if it is definitely success and what it will be um so i will send back the whatever i received right so that will have the clarity so i'll add a message here successfully created and i will send back the details where i'll add the body okay cool and then let me return this response back to the calling function so now if you can review that this is the main handler function is called when the event is triggered and if the event is based on the get request it will got the get student details and under get some request request i am just responding back with the status quo and the body with the the particular student details when there is a post request is called i am calling the create student record method or a function and under that i'm parsing the whatever data is received from the post and then responding back with the successfully created so that i can ensure that the data is available and i can put it in the database or whatever is required okay so let me um deploy this code and see the behavior and let me quickly run the test so before running test let me put some console logs so that to confirm confirm the function is implemented without any syntax error okay so let me put some arrows and i say inside lambda function this is easy for me to confirm that the function is implemented successfully anything without any errors right so let me deploy again and then let me click on test and say test1 and then let me create awesome so deploy again and let me hit the test button and confirm that yes my function lambda function is successfully created and deployed it's working you can see that law console log inside lambda now that lambda is completely created uh let me go back to the uh api gateway let me open a new screen so under the api gateway you remember we have created the uh student api and entered that we have created a student resource and another two methods so now go to the get method and under the integration request earlier it was mock integration now let's link with the lambda function and you can click on the lambda it will enable some more options you can see function name so here you can search for your lambda here you can see that it is populating student record so this lambda function which is already created just now so click on that and enable the proxy so that it will be proxied the incoming calls and saved so ensure that lambda region is on the used whichever wherever the lambda function is created just say okay and okay so it will link this get method to the lambda function let's go back to post and let's do the same thing click on the integration lambda function and then link this lambda function and enable the proxy and just save this now that both get and post are integrated with the backend lambda function and both are ready so to test these apis let's first deploy to deploy you have to go below applications and click on the deploy and deployment stage is new since it is a new one and let me create a student live as my environment agency just deploy here you go the api gateway is deployed and now running so both post and get our ram running so when you click on the get method you can see that url is exposed so when you click this link you can see that student details first name sam and last name sundar and rule number one is picked from this particular lambda function so if you see that the lambda code is implemented over here right this is where we have implemented so let me uh quickly make the roll number to two right and then redeploy and go back to link and call again here you see the number roll number has changed to 2 meaning that the api is working perfectly you are able to call the get api and it is able to connect to lambda function and fetch back the details and now let's test the post method so post is also having a new url so let me uh copy the link to check the post method you need the uh postman quest so this is my post request and go to the body so let me create a student and under that student uh let me add the uh first name first name as uh maybe uh i will give um uh searching and then let me create the last name and roll number two maybe uh 500 let's keep it as uh one or three that's better right so this is what my um post request body is okay so it may have this uh link and then the body as json and let me select this as a post method and let me click on the send here you go the post request has been sent to that particular url and the response you can see that successfully created message and the details are like the first name is sachin and the last name is t and remember is one or three fantastic so this is the way you will be creating the post method so with this we can uh clearly say that we have completed the integration of api gateway with the packet lambda to create a rest apis so now we have developed the student rest api which will expose the get method and the post method for a given student thank you for watching this video please like subscribe and share this video
Info
Channel: dTechXpert
Views: 11,447
Rating: undefined out of 5
Keywords: Aws, How to, How to create, Aws Lambda, AWS Lambda function, Lambda function, Free tier, API Gateway, Rest API, e2e, API Gateway and Lambda, http Method, GET, POST, API, simplest, How to build, Build, DRS, Dtechxpert, aws services, amazon web services, serverless, microservice architecture, distributed systems, cloud computing using serverless architecture, AWS Lambda service, AWS API gateway for secured API, AWS for beginner
Id: ceuxe2uiBsU
Channel Id: undefined
Length: 17min 57sec (1077 seconds)
Published: Sun May 02 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.