#5 Create Rest API using AWS Lambda (ServerLess) + API Gateway + DynamoDB (No SQL SB) And Postman

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
okay this is naveen here welcome back to tabin automation labs so guys today we are going to talk about what is aws lambda functions which is a server-less compute service provided by aws a lot of people were asking about naveen give me the practical use case of lambda how exactly it works can be integrated with our api gateways and there are certain other use cases also i'll try to cover in my upcoming videos so today we are going to talk about this particular architecture this diagram i got it from the aws documentation so this is the api gateway that i'll create and i'll be i'll be using the api gateway services and then i'll be using one lambda function that is a serverless compute service and then i'll be using a dynamodb which is again nosql database which will store the data in the basis of documents that is again for highly scalable and performance point of view if you really want to use you can use a dynamodb over there and this is a very good combination that you can use it and then we will try to hit the api from the client side it could be postman it could be your mobile application or your web application or whatever so first of all we need to understand that what is a dynamodb so quickly i'll just give you an overview because uh if you see this dynamodb is provided by amazon this is again a nosql very flexible database is a key value uh pair format documentation which is store the value on the basis of key and value pair format so it is very highly scalable single digit milliseconds performance at any scale you can get it fully managed multi-region multi-master durable database with inbuilt security backup and restore and in memory caching for internet scale application dynamodb can handle more than 10 trillion requests per day can support peaks of more than 20 million requests per second it's like crazy and there are a lot of uh clients are available in the market a lot of companies are already using it for example lyft airbnb redfin they're already using it and enterprise application like samsung toyota capital one a lot of gaming companies also they are using it it's clear written for mobile web gaming and tech for iot devices for sensor devices you can simply use it so this is like a perfect example with the lambda functions integration we can use that now what i'm gonna do that in my previous video i've already talked about in this particular series if you haven't seen it please go and check uh amazon web services by naveen automation labs four videos i've already talked about what do you mean by ec2 and how to create easy to uh instances and how to execute your selenium test cases using docker containers and everything you can do that now today we are going to talk about aws lambda function so for that what i'm going to do that i have to log in first so this is aws console login simple enter your username and then click on next and i'm going to enter my password over here this is the console that we know that now first thing i'm going to do that okay i'm going to create one dynamodb over here so you can simple select one dynamodb you can select a service like dynamodb and this is a storage service provided by aws what you have to you have to create a table first so let's create a table okay so the diagram is that we have to create a table and we will post the data we will purchase the data in this particular database it means we will insert a data in this particular database using our post api call so i'm going to create one rest api over here and then we will hit the rest api the business logic we will write it over here a simple logic we will write it over here which will process the request and then it will insert the data into the database and we'll send the response back to the api gateway and we will manage one api gateway of aws also okay so let's create a table name so let's take any table name let's say i want to maintain the data for any employee okay let's say employee api you want to create so i simply create one employee a table over here what will be the primary key so primary key cannot be null primary key should be unique and it should be passed i cannot do a blank primary key although this is not a sql database but it maintains the partition key over here so let's see i'm writing let's see simple employee underscore id is a primary key and then let it all the default settings don't change anything and click on create once the table got created you can see that table is being created over here so let's wait for a few seconds and once the table got created after that we can see the table structure so simple click on this particular employee table and then click on items when you click on items you see that okay this employee id table column got created and the value we don't have so we will insert a data like that it does not maintain other columns you can create four columns for different number of rows that is the power of nosql database you don't need to maintain any schema over here or predefined structured schema over here this is schema less database now this is the first thing second thing is that i'll open this aws in a new tab so that i can open my gateway so now i have to create my api gateway so i'll simple select for api gateway over here i'll be using api gateway service okay and then i'm going to create one api service over here i'm going to create one api over here so what i'm going to do i simply click on create api button you can see that product api is already created so that okay in your case you have to create a new api so click on create api now what kind of api you want to create so let's see i want to create one rest api okay not the private one with the public one i want to give this access to through the entire world i'm just going to create one public rest api so it supports uh lambda http aws services let's click on build right and then you have to give your api name over here so you simply click on new api this particular radio button and give our api name so let's see my api name is uh employee api whatever the name that you want to give description is that let's see api for employee something like this end point i'll create reasonable so let it be like that and then what i'm going to do simple click on create api when you click on create api and you simply select over here and click on action and then you have to create a resource over here so click on create resource and let's see my resource is employee is a resource okay and after that this is my resource path after forward slash this is my path variable employee resource path variable will be created like that and simply click on create resource okay then you click on this and then we have to add an action over here simple create method what type of method http method you want to introduce so we have to use post method because we have to insert the data we have to create a data so we will be using a post call over here and then click on write check and now you see that okay what kind of integration you want so i want lambda function http mocking aws or bbc link so i want lambda function so simply click on it what kind of lambda region you want to prefer so you can select the region let's say i'm using simple ap south one whatever the region availability reason that you can select over here and then lambda function name that you have to give but right now we don't have any lambda function so now what we have to do we have to create a lambda function so now what we have seen we have uh we are creating one api gateway dynamodb table is created now we will create one lambda function now how to create a lambda function again open in a new tab and then i'm going to select for lambda so it's just simple select this particular lambda function service and and then you have to click on create function so i'm going to create a new function author from the scratch and let's write your function name so let's see i'll simply write a function name over here whatever the function name that you want to write let's say i'm going to give some name employee this is my function name and then what kind of language that you want to use so let's say i want to use python you can use java you can use node you can use ruby you can use dotnet also you can do that so let's say i'm gonna use uh python 3.6 or 3.7 whatever so let it be let's see i'm using 3.6 or you can use 3.7 also okay and then after that it says that okay permission you have to give a permission so change default execution role click on this and then create a new role with basic lambda permission three options are available so i'll tell you how to create a new role over here then you just let it be like that and then simply click on create function so when you click on create function the function will be created let's wait for few seconds it's creating the function you can see it over here and then this is the ad employee is a lambda so this is a logo of lambda and then you simple scroll down and it will tell you one basic information that okay yeah this is the a python code you can write it over here so it will give you my editor as i told you that lambda is a function it's a kind of uh code that you have to write over here in the form of function and the function name is let's see lambda handler that we have written and whatever the response that you are getting the response will be given in this particular event dictionary and from this particular dictionary you can fetch the results so let's see what i'm going to do that i simply write couple of things over here right so i'll simply say that return okay instead of writing this particular return this is the default return you can let it be like this and i'll do one thing that instead of this particular return status code is equal to 200 and i'll return a message over here let's see i simply write a simple message okay that data is created successfully okay so whenever this particular lambda function will be called then it will simply return this particular data is created successfully right so now after that s u double c e double s okay then you have to save it so how will you save it simply click on deploy so you can see the deployment package successfully saved over here after that you go back to your api gateway and then here you have to give that particular lambda function okay so right now you don't see that lambda function so what you do you simple click on this post click on employee and click on post once again and here you have to give that particular lambda function name so whatever the lambda function name that we have created this is add employee we have to write it okay let's see yeah this is coming add employee and then simple click on save and then simple it's saying that you have to give the aba gateway permission to invoke your lambda function click on ok now here you can see that this is a complete architecture complete design flow it will tell you that whenever you are going to call this particular api from the client so it is giving you the client you can test it directly from here this is the method request and it will go to the lambda function and the lambda function name is add employee over here and then from the lambda function it will give you the response so you can see the arrow over here give you the integration response the method response and back to the client so client will get the response over here now if you run just click on test and then here what we have to do here we have to do a request over here right now before that what i'm going to do that simple go to this particular okay and let's hit this particular api let's click on test can you see this the response we are getting that 200 and the data is created successfully it means whatever the lambda function response that we have created over here data is created successfully the same response we are getting but one thing you need to understand that this lambda function is so right now it's not interacting with the dynamodb so what we have to do we have to integrate integrate between lambda and dynamodb so we have to write a code in such a way so that we can make the connection with the database i mean we can fetch the value uh sorry we can insert the we can process the data into the database so how will you do that so right now this is simple when you call this particular lambda function from your apis you are getting the response you can see the response over here that response is 200 200 data is created successfully perfect okay now i'm going to write the actual apis so i simply write that okay yeah this is the lambda handler so there is a documentation is called boto3 documentation for integration with the lambda function with so i simply write boto3 with a dynamodb so you can refer this particular documentation provided by amazon aws you go to this particular documentation and then to insert the data there are multiple options are available you can describe a table you can fetch the data you can put the data so what you just need to do you simple use put item a query that you have to handle put item method is used to put the data so here certain examples are available over here so first thing you just need to scroll down i've already written some basic code i'll show you that what you have to do so you simply scroll up and then you have to make the connection with the database first connection in terms of you have to create the object of dynamodb so simple let's see uh uh i simply say quick start and then um what we have to do we have to import boto3 first we are writing python code over here so instead of this import json i'll write import bottom three after that i have to create the by using this particular border three what we have do there are certain methods are available you can see that uh simple 103 is available and then you can um get the resource okay so let's see using photo 3 and there are certain examples um three dot resource so what is your resource name you can simply do that so i simply write 103 dot resources or resource and you have to write that i want dynamo that's it okay and dynamo db and this resource will give you let's see i'm storing in a particular object let's see dynamodb object is equal to this now by using this particular diamond dynamodb object what i can do is i can get the table what is the table that you have created so what is a table name that we have created so i'll go to my dynamodb and you remember that the table name i have created as employee table right so the same employee table i'll be giving over here employee table now once this is done the table we got we will store in a table object over here let's see table is equal to this right now i'll take my own return so let it be like this what i'm gonna do that okay i simply say that when this particular function is getting called you simple return what you simple return you simple return what you say simple return as a status code let's see status whatever status code that you want to return as 200 comma and then i simply say that successfully but before that whatever the event even means what if the response in this particular dictionary object that you are getting it we have to insert this particular event data to where to to the database so how we do that by using this particular table we have one method dot i'll show you in this you simple uh check over here the output item put underscore item and here you have to put the item in the form of items like that so it's very simple if you are getting okay little complex over here i'll make it easy for you table dot put item so put underscore item method that you have to use and what do you want to put i simply write my item is equal to that particular event so whatever the event that you are getting the same okay so this is the response from the api the same data i want to put as an item to where to this particular table and once the data is inserted return 200 status code and employee is created successfully to the client in the response pack so let it uh let me check dynamodb boto3 dot resource make sure that okay you are adding the same string over here dynamodb over here and dynamodb.table and what is a table name and the table object that you created and table dot put item where you have to write item is equal to event and then the return code 200 and the message is like that okay whatever other attributes also if you really want to return you can return that as well okay now um let's uh deploy let's save it okay and then we will try to run it again from the api gateway so i'll simply click on this post and uh okay so simple go to employee first click on post and then i'm going to test it once again so let's run it and then here we have to pass the proper request body now right so in the request body guys as we know that dynamodb when we created this employee id is the table name it's a primary sorry employee id is a column name which is a primary key over here so we have to give the right so we have to as a payload that is a request json payload so that employee underscore id is the compulsory attribute that we have to pass i simply write that employee id is let's see one zero one and then it's a nosql database so i simply write let's see name is equal to our tom and then i'm going to add let's see age is 25 right so this is a let's see request body but this is mandatory to pass because this is a primary key and rest of the other columns you can pass or not that doesn't matter these are not mandatory so let's test it and let's see so i'm gonna run it and let's see what happens so employee id is also in a string so we have to pass one zero one in double quotes okay so we are getting syntax error somewhere lambda function lambda function somewhere we are getting a syntax error so let's see in line number eight lambda function dot py we are getting a syntax error so let's see what is the error over there so line number eight it's saying return okay so i hope looks good okay let me check the error once again syntax error in module lambda function okay so let's save it and let's run it again and then from the ap gateway i'm going to run it again so let me just copy this request body and click on employee click on post and let's run click on test copy paste the request body and then click on test okay now that error is solved but now it's saying that yes x is a denied exception it means we don't have any access on that particular dynamodb table okay so we have to give an access over here so what we have do guys that when you create this particular table over here so you have to give a permission and right now we don't have any permission okay you can see that uh lambda function policy there is no policy there is there are no permissions are defined over here so what you just need to do that uh click on this permission and then click on edit and then scroll down and create a new role from aws policy template so just simple click on it and uh okay simple go to i am console and then i'll go to iam console and we have to give a permission okay so let's go to this and for lambda i want to create a permission so click on lambda and i'll be click on next permission and then let's say i want to give some administration access over here so let's select this administrator access and click on next tax and then simply click on next review and then what is the role name that you want to give so let's say i simply write lambda admin role for employee something like this and then click on create role so i have to create a role first guys so once the role is created then i can select over here okay so let's see click on use an existing group created or you can simply that role that we have created it should be displayed over here so you can see that that the role is displayed over here you have to refresh it so that the role that we have created over there it should if it will be reflected over here and then you click on save okay so then it is saved then we will run it again so i'll just okay click on test once again and now we can see that absolutely working fine we are getting a status code is 200 employee is created successfully it means data should be created inside a dynamodb now i'll go to my dynamodb and i'll simple start search click on start search you can see that data got created over here one zero one tom 25 so likewise guys i can create different data so let's hit this particular query with the different data let's see this time one zero two employee name is uh peter and h is 35 but along with that i want to add some more value so let's see i want to say the salary salary is thousand us dollars something like this and click on test so data will be created you are getting the response over here and then you simply go to your dynamodb refresh the data you can see salary column only for this guy got created so this is the benefit of no sql database we don't need to maintain the data in the form of for all the columns will remain constant we don't need to uh follow the asset compliances and everything over here so you can see peter got created 35 like that if you really want to update something so what you can do with the post call let's see with the same 102 age is 35 and salary is 2000 now so let's see run it again and then go to dynamodb refresh it you can see 2000 got added over here or updated over here like that so this is how we can see that this diagram apa gateway also we have created lambda function that we have created with the permission and the data is getting persisted into the dynamodb now this is on aws cloud you can see everything is available on the aws cloud but how right now and we're hitting the api is using the aws inside the aws only but what i want i want to deploy this particular api i want to give this particular api to my customer or to the client or okay and then i want to use it for let's see for mobile application of our web application or from the postman also right so how to do that how to make it available for public how to deploy it so what you have to you have to go to your api gateway and then select this particular employee and then click on actions and deploy the api you click on deploy the api and let's see on which environment you want to deploy so select new stage let's see i want to deploy it on a dev environment this is my dev environment of the queue environment so let's see a simple right deploy to copy like this these are descriptions so likewise you can create multiple environments dev environment qa environment like that and the url will be changed accordingly and then click on deploy when you click on deploy you can see this is a deployment url we got it see with dev over here it means this is the dev environment and then you simply open this dev click on post and this will be the invoking url you will get it over here and you simply save the changes copy this particular guy right click on it go to copy now we will hit this particular api from any client let's see postman so i'll be opening my postman over here open that particular post man and then go to plus and i'm going to paste this particular url over here and this is the post call so let's select the post over here and we have to pass the json body so click on body raw and select the json object over here and here we have to give the request body so in the request body again i'm going to write that what is the employee id employ underscore id let's see this time i'm going to create 201 and what is the employee uh name so name let's see i want to create this time name is equal okay then i want to create that okay which city let's see i'm writing city as bangalore employee id we have to pass in double quotes because when we decided the employee id that time it was a string and then let's see i'm simple writing in which country this particular employee country name is let's see india okay but employee id is the primary key it is compulsory to pass these are just the mandatory fields if the column is already available it will use the same column if it is not there then it will create a new column in the database now let's run it and let's see it is working from here or not and i'll be getting the right response so let's hit the api and you can see that okay awesome 200 employee is created successfully and that's we will go to our dynamodb and we will check refresh the data you can simply see that okay yeah uh name see we have written a small name over here that's why it's creating a new column over here right so i'll do one thing let me write the capital name over here and then send it again refresh it now you can see that okay the name column got deleted name is naveen age and salary that we are not passing but we are passing city and country and for other records city and country doesn't matter because the schema was different for these guys so this is unstructured schema for nosql database in dynamodb right select files you can create a data tomorrow if you want to delete the data you can simply select respective values let's say i want to delete all these guys go to actions and click on delete and click on delete data got deleted and you want to create it again hit the ap from here once again refresh the data data got created once again now i'll hit the spotlight apa 201 once again data is already available and let's see it is creating a new row or not no because the primary key is already there so you don't need to worry about while persisting the data that 201 duplicate key will be created no dynamodb will take care of it automatically it will just update the same record over here so likewise guys you can create number of gateways you can create dev and the qa and then like that you can simply do that so you can create multiple stages over here you can create another stage let's see for the qa environment you can create that and then okay let's see create over here so likewise you can create a qa environment over here so this is for the qa url you just copy this and you can hit in the new tab over here so you can see the difference this is qa and this is my dev environment same day you can create for a stage and the production environment and you can create your own apis for your practice point of view also that also you can do that so this is the power of our lambda functions integrated with dynamodb having with api gateway so we are not giving the direct access to the lambda functions so client is not directly hitting the lambda functions client is hitting the api gateway client is not hitting the lambda or client is not interacting with the dynamodb directly this is not this is not the case we are giving the request okay to the aba gateway api gateway is calling the serverless lambda function where the python code is written over there making the connection with the dynamodb and perceive the data over here and then giving the response back to the ap gateway that yeah response code is 200 data got created successfully and given back to the client in the postman or in mobile whatever that response scale is 200 data code created successfully over there okay now the one last case if i'm passing let's see remove this what you're doing if i'm passing let's see employee id i'm not passing over here right so let's delete this particular employee id delete that and then i'm running it again when you run it it's saying that okay no it's a validation exception that missing the employee id in the item this is the invalid request so that's why we are getting that employee id is missing error message here we are getting it right so employee id is the primary key which is compulsory to pass okay so that's all for this particular video i hope you learned about lambda functions that is a serverless compute service provided by aws it's amazingly good later on i'll try to create one more example with respect to with respect to automation test cases you can directly write one python code and then you can directly execute from the lambda functions and just call the api integrated with jenkins and run the ap which will hit the lambda function and then done same thing you can store your test results in the dynamodb also so there are multiple hundreds of use cases guys are there now it's up to you how exactly you are using it so thank you so much for watching this particular video i hope you are liking it if you like this particular video video please subscribe and click on the bell icon so that you will be notified for the next video and i'll be coming soon with some interesting video on cloud like aws and gcp as well thank you so much thanks for watching this video guys
Info
Channel: Naveen AutomationLabs
Views: 17,932
Rating: undefined out of 5
Keywords: AWS, AWS Lambda, AWS Lambda with API Gateway, AWS Lambda API Gateway DynamoDB, Rest Api using AWS Lambda, what is AWS Lambda, AWS amazon web services, AWS by Naveen automationlabs
Id: k79-kwseu8U
Channel Id: undefined
Length: 31min 24sec (1884 seconds)
Published: Thu Oct 08 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.