An Introduction to AWS API Gateway with AWS Lambda (.NET Core 3.1 Application)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone and welcome to dot net core central in today's video i am going to continue my exploration of aws lambda serverless functions and today's video i will focus mainly on creating a restful api using aws api gateway and aws lambda functions to do that first i'm going to change a little bit of my existing function and what i'm going to do is i'm going to just get rid of the input string and here i'm just going to say executing api gateway and of course i'm going to change this later when i when i want to use the query strings and stuff like that from the api gateway but for the time being i'm going to keep this implementation very simple and then we'll slowly change it to use some of the advanced feature so this is again it's a simple function right now it is just returning hello world what i'm going to do is i'm going to deploy this function in aws and how to deploy the function what all to do i have shared all this in my last video if you have not watched it i'll strongly suggest you to watch it i'm going to provide the link in the description but i'll go ahead and deploy this function and i'm going to keep the name as same lambdademo and it has updated the function now if i go back to aws go back into my function i can see the function was updated 10 seconds ago and i can go to the test and execute it and the test should show hello world now let's get into api gateway so api gateway is as the name suggests it is an api gateway it will be the first entry point of an api call and then here you can do a bunch of thing you can add authentication and authorization at the api gateway layer and then after authentication and authorization is done you can send the request back into your backend service now that service can be a easy to server running iis it can be an ecs running a docker container or it can be a lambda in this video i am going to show how to use api gateway with lambda the reason for this is as i explained earlier some of the services are free forever and some of the services are free for 12 month api gateway supports a million call per month for 12 months and that's a lot of call to start your patient project so for this example i'm not going to use websocket or an http api i'm just going to go and create a restful api so i'm going to select on build and for the api instead of using the example i'm going to use a new api i'm going to say test api i'm going to keep the default setting here once the api is created i'm going to create a new method and it's going to be get once the get is created you can see there are few options you can have an http endpoint or you can use a mock response in my case i'm just going to end up using the lambda function so i'm going to give my lambda function here so this is going to be the lambda demo i have only one so that's showing up i'm going to use the default timeout and i'm going to save it says as you can see here it is asking that this api gateway will now have invoke permission to the lambda and the iam role required for the api gateway will automatically be created as a part of this process now as you can see the api gateway is created it's connected to my lambda i can click on test to test the method and once i test i can see it's coming back with hello world and as you can see here are some logs but this is not a proper suggested http response object we are going to change it in a little bit but before that now let's go ahead and deploy this because once we deploy we'll be able to test it using a public url so i'm going to go here and i'm going to click on deploy api once i click on deploy api last for a stage i'm going to create a new stage and the stage for me is prod i'm just going to deploy in prod let's say click on deploy once i click on deploy it will be deployed and as you can see this is the invoke url for the deploy this is my prod stage and now if i click on this it's going to execute the url and you can see the hello world response is showing us as expected now let's go back to the lambda and make some change to use proper api gateway request and response so for that we are going to use couple of things first of all we are going to add the nuget package for aws api gateway so i'm going to go to browse and i'm going to search for lambda.api and if i search that it is going to show up amazon.lambda.api gateway events and that is what i am going to install now once installed i'm going to go back to my demo function and you can see here my test here is failing because it does not expect to parameter anymore but i'm not going to work on the test right now because that is not the focus of this demo i'm going to go back here and here i'm going to go and add the namespace for api gateway events and then here instead of returning a string i can say api gateway response so that's the response i'm going to return and just like the return is going to be api gateway proxy response in the request before expecting the i lambda context i'm going to take api gateway proxy request now once i take the request here what i can do is from the request object request object i can access things like query string parameters and it's a dictionary so i can do if query string parameter is not equal to null and request dot query string parameter dot contains key name then i can set here string name equal to no name and then here if the query parameter contains name then i can say name is equal to request dot query parameters of name so this is going to set the name and in the end here what i can do is and here we can say got name yes name and then here instead of returning a string we can say new api gateway response and for the response you can give status code and as you can see the status code is an end in our case it's going to be 200 because we are returning a success for the body we can say the username past is name and as you can see the body is string and then you can set other things like adders and is base64 encoded and things like that but i'm going to just leave it at the body now i'm going to deploy the lambda function again and i have an error i'm going to just comment this test out for the interest of time and then i'm going to try to deploy the function again and it has updated the function now if i go back and i execute this so now you can see in the body username pass no name because we have not passed any name if i go and say name is equal to test see it says still username past is no name though it should have picked this up and the reason for that is for some reason if we go into the monitor if we go into the cloud watch for some reason we are not getting the name and if we look into our console.write that we added we are going to see that the name is missing in the cloudwatch log it's taking too much time but we know that the name is sure not showing up and here is the it is the log if we see that it says no name name got no name here it's because it's not getting the name from the name that is passed and the reason for that is when we created the lambda we missed one critical aspect is that in the lambda we have to use lambda proxy integration that is something which is missing here in the get so and as you can see there is no real way to modify it at least i could not find a way to modify it just says edit documentation so what i'm going to do is i'm going to delete the api and i'm going to recreate it and i'm going to start again build new api i'm going to create a new api keep the same name as before test api keep this is as default and this time when i create a new method i select http get and here this time when i select the lambda function i am also going to use lambda proxy integration so i'm going to click on that and as you can see request will be proxy to lambda with request details available in event of your handler function so this will ensure the entire request object is available to the handler function now i'm going to save this so it's going to save it after it's been saved i'm going to again deploy the function and i'm going to create a new stage and name it as prod deploy once the deployment is successful i'm going to execute the new url and this time i'm going to say name equal to test again and as you can see username pass test is now showing up and working as expected and as you can see if i open up the developer toolbar we added 200 so if i go into the network and i re-execute this we can see here we're getting 200 response back the body contains the response body contains the response and the response header contained all the information and as you can see it's called it contains the api gateway id and things like that so this is how you can quickly create an api using amazon api gateway and a lambda function and as you can see for the lambda function there's nothing much we had to do all we had to do is add the nuget package for api gateway and then instead of returning string and getting a string as a parameter we got the api gateway proxy request as the request object and api gateway proxy response as the response and from the request object we can get parameters like query string header and other thing here we are using the query string and then we can return api gateway response with proper response code now we can do the same thing with post and same lambda function can work as a post parameter and here we can use something like request dot method http method and we can say if request.method is equal to post strings post then we can execute function but i would suggest if you have a post versus get keeping two different lambda one for handling post versus another one for handling get so this is all i wanted to cover for today's video if you have any question regarding the implementation or if you want me to cover some details around api gateway and how to use it for post put and other method let me know i will cover in a different video but as i said it's very straightforward you just create a new method and instead of get it can be poster put and once you select the http method you can define what you want to do with that method so if you want to use lambda and as i mentioned you can use same lambda function and it would just work right now if i just test the same lambda function it will work exactly the same because there is there's nothing different that we are we're not going to send a request body because we're not going to use but you can see it comes back with the response no name passed because it'll never have a query string so there will not be any name passed so it's as simple as that if you like this video please give me a thumbs up and if you are new to my channel if you think you are going to get value out of my channel please subscribe to my channel and thanks so much for watching this video
Info
Channel: DotNet Core Central
Views: 5,659
Rating: undefined out of 5
Keywords: aws api gateway, aws api gateway .net core 3.1, aws api gateway lambda, amazon api gateway, aws api gateway lambda .net core 3.1, aws api gateway .net core, aws api gateway lambda .net core, amazon api gateway lambda .net core 3.1
Id: ad2md33t1U0
Channel Id: undefined
Length: 14min 17sec (857 seconds)
Published: Sun Apr 11 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.