AWS API Gateway & AWS Lambda - AWS Serverless Part II

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hey guys and welcome to the lesson today we'll be looking at Amazon API gateway API gateway is a fully managed service that allows you to create serverless api's create maintain and secure API s at any scale this pretty well sums up what API gateway does so with this service Amazon takes care of everything behind the scenes that needs to happen in order to handle hundreds of thousands of concurrent requests and also manage security and scaling taking full advantage of Amazon's global network and computing resources as with many of the serverless products price is a big advantage here as you're only paying for the amount of requests made and the amount of data transferred in and out of the gateway so your cost scale along with the usage of your API in this lesson we'll be using API gateway to create a simple REST API to interact with our lambda functions get and set user data so API gateway will call our lambda functions which will in turn get and set data in the database now this builds on top of the previous lesson where we set up two lambda functions and a user's table and DynamoDB so if you haven't seen that one yet you may want to pause this video and complete that tutorial first link in the description below however if you're just interested in seeing how to wire up API gateway with lambda you can definitely still get a lot of this tutorial without completing the previous one all right guys let's get coding alright let's go ahead and login to the AWS console and go ahead and type in API gateway into the search box that should pop right up and that link will take us to the API gateway landing page now if you're here for the first time you may see some sort of pop-up dialog telling you about the example REST API that's been set up for you feel free to go ahead and dismiss that and click right on the create API button ok we can leave the first two default selected we are creating a REST API and we are creating a new API so let's go down to settings and give our API a name I'm gonna call it users API and I'll enter in a quick description okay now we have a couple of options for endpoint type with regional this is going to deploy your REST API to the region that you're currently working in so in my case it's us West to Oregon now if you have a lot of users from around the world you may want to consider selecting edge optimized now with this one selected you're going to be able to take advantage of AWS as cloud front content delivery network which means that your arrest API is going to be cached at various edge locations in different regions around the world which will give a significant performance boost to users that aren't necessarily close to you or close to your region accessing your REST API so I'm going to go ahead and use edge optimized here and click on create API okay now before we start to architect out our REST API we want to think about what it's going to look like so what are we doing here we are setting and getting user data in our database so a typical REST API for that may look like domain slash users slash ID where IDs going to be a path parameter because it's a variable you know each user will have a different ID so what we can do is go ahead and click on actions and create resource now each resource you can think of as being a part of the path of your REST API endpoint so we'll give this a name let's see let's do user and you can see that it automatically populates my resource path with the lowercase version of the resource name that I've provided okay so that looks good enable API gateway cores now I don't think I'm going to do that right now but this may be useful if you for example your REST API endpoint is of a different domain than where your client is going to be accessing your REST API from in this case it's the same domain so I'm not going to worry about that I'm just going to click on create resource okay so so far here's what our endpoint looks like /user now the next thing I want to do is have the slash ID where ID is going to be a variable a variable path parameter so go ahead and click on actions let's click on create resource again we'll call this one ID and in the resource path now if we want this to be a variable what we can do is surround this the resource path name with curly braces like so alright and go ahead and click on create resource alright so user slash ID looking good so far I think the next thing we want to tackle here is going to be our get functionality so from the actions drop-down let's go ahead and select create method this time and let's choose get from the drop-down and click on the arrow okay get set up now integration type let's go ahead and leave lambda function selected obviously we're integrating with our lambda function next option is use lambda proxy integration let's go ahead and select that now what this allows us to do is access all of our request parameters and request body from the event argument of our lambda function so we'll definitely be needing that so ensure that's selected lambda region go ahead and make sure that your region is selected and lambda function let's go ahead and select our get user data function okay and let's use the default time out go ahead and save add permission to lambda function so this is just letting us know that we're about to give API gateway permission to invoke these functions let's go ahead and click OK okay so we are now inside of the get method execution control panel and I'm just gonna make my screen a little bit bigger here alright now let's go ahead and let's go ahead and click on method request there are a few options here we can take a look at okay under settings first of all authorization it's defaulted to none let's go ahead and edit that and change it to AWS I am now with this option selected basically this allows us to use I am roles to create fine-grained permissions regarding who can update who can change our REST API and also who can call the API so let's save that by clicking on the check mark here request validator this allows us to validate different aspects of our HTTP requests you can choose just body you can choose sort of all the parameters or different combinations here we're gonna choose the middle option that will allow us to validate everything basically so save that API key required let's leave that to false okay and we can take a look at some of these options here request paths ID that looks good we don't have any query string parameters so we don't need to look in there let's check out the HTTP request headers I'll go ahead and add a header just for good practice here I like to add the content type header which will be application JSON save that okay and we have no request body and we don't need to take a look at SDK settings so we can leave that alone for now all right so let's go back to method execution and we can go ahead and test out our our first get method so click on the little lightening bolt here under test okay now if you remember to the last lesson we had couple of users in our test database so let's enter 6 7 8 9 0 that is the ID of one of them okay and for headers go ahead and add our content type : and give it a value of application Jason okay we can leave everything else as is and let's go ahead and click on the test button okay so here's our response from lambda it's good that we are getting response it's bad that we got a 502 response so let's see what's going on here internal server error okay and as you can see we've got some log output over here from lambda and if we scroll towards the bottom here we go execution failed due to configuration error malformed lambda proxy response so what's going on here is if you remember back to the lambda function that we created in the last tutorial our response was basically just a console log so the thing is with API gateway it's it's expecting a properly formatted or a specifically formatted response object okay so I'm going to go ahead and jump on over to visual code here and as you can see we're simply console logging the data that comes back so we don't have a response of any kind so the next task for us is going to be to go ahead and refactor our lambda function so that we returning a properly formatted response so that API gateway can deal with it alright guys so in refactoring this lambda function what we want to do is basically return our response object that contains the status code any response headers as well as the response body which will be the user object coming back from DynamoDB so let's start to set up some of these properties and then we can add them to the response object all right let's do a response body and initialize that with an empty string and let's do a status code and initialize that to zero all right now if you recall earlier I mentioned that we can access some of the request parameters and the request body via the event object so what we need to do is get the user ID and replace this hard-coded ID with the actual user ID passed into the lambda so we can use destructuring syntax to extract I D from the event and we want to use a property called path parameters like that there we go okay so now we can replace this hard-coded ID with ID from above alright and we can get rid of our console log here and now we want to set the response body to the actual response so let's do a response body equals data dot item now data item that is just how DynamoDB formats its responses and so this item will actually contain the user data all right now also we want to go ahead and stringify this before we return it so dot JSON dot all right now let's go ahead and get rid of our catch console.log and we can set the response body to an error message in the case of an error so let's do response body equals unable to get user data okay and we'll set the status code - lets do a 403 for error oh and we of course we also need a response code for our success case we'll set this one equal to 200 all right good now let's go ahead and construct our actual response object and add the properties that we just created to it so let's do a const response okay set that to an object and let's see we'll have a status code property oops status code hold a status code all right headers now under headers you you just have an object and you can have a comma-separated list of headers so in this case let's just do a kind of a test header my header let's test all right that'll do for now all right we'll have a body be equal to the response body okay and I think that's all we need that looks pretty good so finally let's go ahead and return all right much better so now we're actually returning a response for our lambda function what we can do now is update our existing get user data lambda function with this new version and then rerun our test on our API gateway so let's go ahead and select all here copy that to your clipboard and now we need to go over to AWS lambda and copy paste this code into our lambda function so let's see click on services and you should see lambda over to the left if you've done this recently otherwise just go ahead and type it into the search box navigate over to lambda and let's click on get user data okay now just scroll down a bit and let's go ahead and copy delete and then copy paste our new code here all right let's go ahead and save that all right cool now our lambda function is updated so let's go back on over to API gateway and test out our code so click on API gateway all right click on the user's API click on the get method click on test and let's enter in our request parameters again so the user ID was six seven eight nine zero and let's add our content type header again content-type : application JSON all right now scroll down a bit and go ahead and click on the test button all right looking good now here is our user data Bob Johnson six seven eight nine zero so as you can see with that reformatted response everything's working correctly API gateway recognizes that response and looks like our get method is working great all right so next up we need to go ahead and create another method for our put functionality where we want to put the use of data into dynamic DB via our lambda function it'll look very similar to this one the the major difference will be that we're actually now dealing with a request body so we'll have our our user object passed in to the lambda function via that request body okay so for adding a user into the system what we want to do is create a post method so we won't be needing the ID path parameter anymore because we'll be sending all of our data on through the request body so what I want to do is click on user and create our new method from here so under the actions drop-down let's go ahead and select create method and let's select post and save that okay we'll leave many of these options the same way we did with the get method so lambda function that's good use lambda proxy integration let's go ahead and check that and for leave our region the same and for lambda function let's do put user data okay make sure our timeout checkbox is checked and let's save that okay all right now let's click into our method request so for authorization we can make the same selection as before AWS I am save that request validator let's go ahead and just validate the body on this one so save that leave everything else the same okay and let's expand HTTP request headers and let's just add our content type header just the same as before all right and save that alright now let's click on the request body section ok so again the request body is where we'll be passing in our ID our first name and our last name now API gateway has this concept of models so a model is nothing more than a schema for an object so in order to have a some predictability when we're coding our lambda function and knowing what what parameters we can expect to be passed in we can set up a model so let's go ahead and click on models from the left hand menu and let's go ahead and click on create alright we'll give this model a name call it user and content type will be application JSON alright we can leave the model description blank for now alright so our model needs to follow a specific syntax in accordance with the json schema draft 4 so what i want to do is go on over to the api gateway documentation and look for an example that we can copy/paste and modify from there so I've gone ahead and bookmarked the link to the API gateway documentation I'll add this to the description so you guys can just click on it there for easy reference so I want to scroll down a little bit here and let's go ahead and copy paste this example into our API gateway interface okay so what we can do is nuke everything under properties here all right whoops deleted one too many curly braces there alright we'll add our new properties in here but first let's go ahead and modify the title so this is a grocery-store input model let's change this to user model all right type object that's good and properties let's start to add our properties we've got ID now the value for the properties needs to be an object that defines the type so type and we'll set this to string all right now we're gonna copy this for our next two properties so let's go ahead and just copy this line add a comma and let's add it two more times whoops get and getting mixed up with my um neck keyboard shortcuts here there we go all right so the other two properties for our user were first-name and lastname all right that looks good so let's go ahead and click on create model alright now we can go ahead and add our model to our request body so let's go back to our users API alright click on post click on method request alright and hopefully our selections have persisted here yep everything looks good so let's go back to request body now we should be able to click on add model and application JSON and let's select our user model from the drop-down and save all right so our put request is looking good now we know if we tested this right off the bat it would fail because we still haven't refactored our lambda function to return a properly formatted response so as our next task let's go ahead and refactor our lambda function all right I'm gonna jump back over to my code editor here now I'm gonna leave my get user data lambda source code open because we're gonna end up copying and pasting a lot of this code a lot of you know 90% of this is going to be reusable so let's go ahead and open up our put user data index JSON file alright now we're gonna go back and forth here a bit and just copy and paste some of this boilerplate code so let's start with our response about and status code from the get method let's copy that and let's go ahead and paste that into our put user data okay now one thing that is going to be different is the way that we extract our properties from the request since we're dealing with a post now where most of the data or all of the data is coming through the the request body this is gonna look a little bit different so let's do constant and we'll still use our D structuring syntax to grab the ID first name and last name okay equal event and this time it's going to come from the body parameter alright now one thing we'll need to do here is json.parse okay so that we'll go ahead and convert this object into JavaScript JSON so that we can work with it later okay now we can go ahead and replace all of the these hard-coded values with our ID first name and last name okay that looks good so far all right let's go back over to our get function and let's see let's go ahead and copy and paste our success case code here from the try back over to put and let's just copy that over our console log okay now we can change this to data because we're not returning an item in this case we're just returning it'll probably be an empty object actually and we'll update our status code from two hundred to two hundred one for create item alright let's go back over to get and let's copy our error handling code okay back over to put and again we'll just copy paste this over our console log statement and I'll just change the wording a little bit here to make more sense unable to put user data okay now the last thing we want to copy over from get is going to be our actual response object in our return statement so copy that and we can keep everything as is and just go ahead and paste it here below the try-catch alright I think that looks that looks good so let's go ahead and select all copy copy that to your clipboard now we need to head back over to lamda and update our put function alright so what I'm going to do is expand the services drop-down and just right-click open a new tab on the lamda okay and click on put user data okay let's just scroll down a bit and we can we can just nuke all of this old code and paste our new code in there alright and save that alright now let's head back on over to API gateway and test out this code okay so let's see post we need to just click on your post method there now I'm sorry I click on the method execution and click on the test lightning bolt and let's set up a little test case here so I'm not going to worry about the headers since we're not validating that let's just go right for our request body ok so I'm going to set the ID just to let's see 5 4 3 2 1 comma let's do a first name let's set that to Larry and let's do it last name oops last name : let's say David Larry David alright let's go ahead and run this test ok that looks good so far status 201 the response body just an empty object which is what we expected and don't see anything off in the logs everything looks good to me so what we can do now is head on over to dynamo DB and just ensure that our newly created user is in fact in the system so let's again expand the services and you should have dynamo DB off to the left-hand menu but if not just go ahead and type it in the search bar and it should pop up so again I'm going to open this up in a new tab and let's click on tables let's click on our users table let's click on the items tab and there we go there's our Larry David user so that's good news that means our lambda functions are working our API gateway front door is working and everything is working together alright guys well that concludes today's tutorial so I hope you found it useful hope you learned something and if you find these tutorials useful in general don't forget to Like and subscribe and I'll see you guys in the next video alright have a great day guys bye [Music] [Music] you
Info
Channel: Cloud Path
Views: 31,070
Rating: 4.9751167 out of 5
Keywords: API Gateway, AWS Lambda, REST API, serverless, DynamoDB, cloud computing, AWS API Gateway, serverless REST API, AWS Lambda functions, serverless computing, aws serverless, api gateway intro, api gateway lambda integration, aws lambda intro, aws lambda tutorial, serverless tutorial, api gateway tutorial, aws cloud, amazon web services, api gateway nodejs, aws api gateway nodejs
Id: Tc1YIOAbyS0
Channel Id: undefined
Length: 26min 42sec (1602 seconds)
Published: Mon Dec 31 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.