Use AWS Lambda and API Gateway to return resume data

Will Button
InstructorWill Button

Share this video with your friends

Send Tweet
Published 7 years ago
Updated 4 years ago

In this lesson, you will learn how to create a simple AWS Lambda function to submit a name via an API Gateway and return a resume for that person. At the end of the lesson, you will be able to create a Lambda function, and API Gateway, and understand how to submit data via the API Gateway that will be accessible via the Lambda function

[00:02] Let's use Lambda and an API gateway to submit a name, and return a resume. Let's see what the end product is going to look like. We're going to create a post request to this API, and in the body, we need to include a first name and a last name.

[00:20] When we submit that, the API returns the first name that we passed in, the last name, and then the work history for that user. To build that ourselves, in our AWS console, we're going to go into Lambda, and we'll create a Lambda function. We're going to skip the blueprint, we'll give it a name of GetResume.

[00:42] Our runtime's going to be Node.js, and then we can start typing in the code right in this little edger window. We'll type exports.handler to export the function named handler. This has two parameters, an event and a context. I'm going to create an object called resume, and on our resume we have our first name object, which we're going to get from the event object.

[01:15] This was the one that you saw me pass in, in a demo in the beginning. Same thing for last name. Any parameters you pass into a Lambda function are available within the event object, and then I'm just going to create resume.workhistory. Then I've got some work history that I'm going to paste in there.

[01:35] The last thing that we need to do is our context object, as a succeed method. If that gets called, we want to return our resume. When we create this, it's going to get saved is a file named index, and so the handler that it's going to call will be index.handler. That's how it knows what code to execute.

[01:56] We're going to create a new role, and we just need basic execution permissions for this. We're going to create a new role policy for it. If we examine the policy document that gets created, it's pretty simple. It just allows the action of, in the logs area of creating a log group, creating a stream, and putting log events, which is basically how the Lambda function is given permissions to write logs to CloudWatch so that we can view the logs after the fact.

[02:32] We can have some different options for memory, anywhere from 28 to 1,500 megabytes, and then the timeout is currently set at three. We can take that all the way up to 60 seconds, if needed. I'll click, "next," and then confirm everything looks like we want it, and create the function.

[02:50] We can test this now, and we'll need to provide our first name, and our last name. We can submit that, and the execution result is succeeded. You can see, here's the first name we testing, the last name, and the work history we included as well.

[03:09] Finally, on the bottom here is summary information. The duration was 11.8 milliseconds, and we used nine megabytes of RAM to execute that request. Our Lambda function is built, now we need to create an API that will return that information. We will go into the API gateway and we'll select, "create an API."

[03:30] I'm going to call that resume. Now, the root end point is automatically created for us, and that's called a resource inside of the API gateway. We need to create a method that we're going to use to interact with it, and we want to create a post method so we can submit our first name and last name.

[03:50] The integration is going to be done with Lambda, and we choose the region where I built my Lambda function, which is US-West-2, and then type in the Lambda function. You can see, there was some autocomplete there as well. We have to confirm, now, that we want to give the API gateway security permissions to invoke our Lambda function.

[04:15] We'll click OK, so it integrated with Lambda in that region, and then set a div to call the GetResume Lambda function, and we can test that. We need to add our request body, hit test, and request return to status of 200 after 38 milliseconds, and here is the information we submitted, and the work history as well.

[04:42] Our final step here is to deploy this API, and we can deploy it to a specific stage. We're going to call this production. Now that it's deployed, we have some other options available to us. We can enable an API cache, we can send the logs to CloudWatch, as well as enable CloudWatch metrics, and set up some throttling for burst limits and rate limits.

[05:06] We can also copy this link address, and we can paste that URL into our Postman REST Client, select post request, and in our body we want to include, again, first name and last name. We can submit that, scroll down to the response, and first name Joe, last name Bob is returned as well as the work history, again, and that's it.