Add an API Gateway HTTP Endpoint Trigger to a Lambda Function in AWS

Sam Julien
InstructorSam Julien

Share this video with your friends

Send Tweet
Published 3 months ago
Updated 3 weeks ago

Currently, the only way to run your lambda function is with the test button. In order to run the function from elsewhere, you need to create a trigger. In our case, we will be creating an HTTP endpoint with API Gateway.

API Gateway wraps requests and responses in its own format, which you will learn how to account for when writing your lambda functions in this lesson.

Sam Julien: [0:00] Now we have our Lambda function, but the only way to trigger it right now is just by clicking on the Test button, like we've been doing, to see the response.

[0:11] If we go up here, you can see we have this Add Trigger button over here. There are a number of different ways that you can actually trigger AWS Lambda functions. The most common way for web developers would be through some API endpoint. You can see here, even though we've got this unique identifier for this Lambda function, we can't actually hit that directly in a browser.

[0:35] To do that, we're going to need API gateway. If I click on Add Trigger here and go to Select Trigger, you can see that there are just all kinds of things that you can use to trigger AWS Lambda functions. For example, if you wanted to, you could add a record in a database using DynamoDB, and then have that run a Lambda function.

[0:56] To use an HTTP endpoint, though, we're going to need API gateway. I'll click on API gateway. I don't have any API gateways currently created. We're just going to go ahead and use Create API. There are two types of APIs available for API gateway. It's very confusing. REST API is the older version and HTTP API is the newer version.

[1:21] Technically, the HTTP API has slightly less features. For this purpose, it doesn't actually matter, and because it's the newer one, let's just go with that. We also need to pick a security mechanism for our endpoint.

[1:32] We're not going to do any authorization with JWTs or anything. We're just going to choose open and let whoever access this endpoint. Then I can just go ahead and click Add. If we scroll down and look at these triggers, you can see that now I've got this endpoint.

[1:49] The problem is, if I try to open this in the new tab, we haven't passed anything in. That's because we have a body that we're trying to pass in. That's why we don't see anything over there. Let's instead use a tool called Insomnia to test this endpoint.

[2:05] I'm going to go back to the Trigger here and let's just copy this endpoint. I'm going to hop over to Insomnia, which is basically just a API testing tool. I'm going to paste this into this API gateway request. If I click Send here, something weird happens.

[2:22] We get our 200 OK, but we actually don't get our body back. That's because what's happening here is that API gateway is actually wrapping the request and response with its own formatting. It's a little bit magical. You can go check out the documentation.

[2:40] For our purposes, let's just fix this really quick and I'll show you what I mean. If I head back over to our code for our Lambda function, go back over to code. Inside of index.js, we need to do something special. I'm going to delete this to do line here. We actually need to parse out the body from our event that's being passed to the Lambda function.

[3:03] I'm going to say const body = json.parseevent.body. This is basically going to just pull out the body from the event that API gateway has passed along. Let's go ahead and come down to line five here. We can replace event with body.dog.

[3:27] Then in order to save this and get it out into the world, we just need to hit this Deploy button. What's annoying about this is now if we try to test this, we're going to see that it fails. That's because we're accounting for the change that API gateway brings to this equation.

[3:44] On the plus side, if I go back over to Insomnia to test out this endpoint, now I can click on our Send button here and you can see we get back body.dog, which is bark. If I changed bark to woof and hit Send again, now you can see woof.

[4:02] If I go back over to our endpoint, I'll just show you one more time, we've got our code here. That code is parsing the body and just returning back one of our properties. That's happening through this API gateway endpoint that's right here.

[4:17] That's how to create a Lambda function, and then also trigger it as an HTTP endpoint. There's a lot of different things you can do with Lambda and with API gateway. There's an endless number of combinations you can try.

[4:30] I just wanted to give you a high-level overview just so you understand the basics of what these two services do.