Check out a free preview of the full Serverless with AWS Lambda course

The "API Gateway Routing" Lesson is part of the full, Serverless with AWS Lambda course featured in this preview video. Here's what you'd learn in this lesson:

Scott introduces AWS API Gateway, which allows for setting up routing for lambdas. The API Gateway routing allows for the creation of a full API on top of serverless functions. Scott takes questions from students.

Preview
Close

Transcript from the "API Gateway Routing" Lesson

[00:00:00]
>> Scott: Onto what we really want to talk about now that we know events. And we know about what's happening on lambda. But what we really wanted to do was create an API, right? We all wanna create an API with lambdas, that's kind of what we're all thinking about.

[00:00:15]
But eventually, when you'll get to the point where you'll realize there's so many things I can do with a lambda. But the first step is always an API, cuz that's where you're coming from. You're coming from creating an API on a traditional server or something like that. And you'll mess around with it and you'll be like, wow, actually I can do this.

[00:00:28]
And I can break this lambda out, and I can do all this crazy, crazy stuff. And then you get hit with performance, you realize it was a bad idea, so you don't do it. [LAUGH] But what we wanna do is we wanna start with those APIs. So if we're still on AWS, and this is just to show you what's happening.

[00:00:44]
There's this thing here called an API Gateway, and it's literally a gateway for your API. You can think of it like a proxy in front of whatever you want it to be. In this case, we're gonna put it in front of our lambda functions. So it's gonna be responsible for routing our HTTP events to our lambdas.

[00:01:06]
That's what this API gateway is gonna be for. You can come on here and you can create one. Remember, I told you, we don't have to do this. We can do it through through a configuration file, because creating out here is really bad. I do not recommend it.

[00:01:18]
So we're gonna do that on. And the event that we're gonna be subscribing to, if we go back to the lambda console and we click on the lambda, and we click on test, we click on one of these. The event that we're gonna subscribe to is the API Gateway AWS Proxy event.

[00:01:42]
So let's just take a look at this event realquick. Pretty crazy, right? There's a lot of stuff in here. But the things you're gonna immediately recognize is there's a body. So if you do a post request, you got that. The resource is the URL, the non-formatted URL that someone made a request to.

[00:01:59]
And then you got all this meta information about a request context and what identity, IM user, IP address, and all this stuff, what stage this lambda was in. Of course you got headers, any path parameters, the method that was called, and tons of stuff. So this is all you would need, so your lambda could be able to respond to HTTP request.

[00:02:21]
This event is formatted from the API Gateway. So when someone issues an HTTP request to your API Gateway, the gateway format this object based on the incoming request and sends it to your lambda as an event. It does that for you. Now there's two different API Gateway events.

[00:02:39]
There's a proxy and then there's just basically like forwarding, almost. We're gonna use the proxy, because when we use a proxy, this gives our lambdas power to control what the response object looks like. Without that, we would have to go to AWS API Gateway and we have to use their GUI to determine what the response would look like, using something called velocity templates or something like that.

[00:03:03]
It's not the fun, it's really bad, I do not recommend doing that. So by default, serverless automatically uses the proxy implementation of API Gateway. And I don't recommend going away from that, just stay with the proxy. You don't even have to do anything, just use the API Gateway and you're good to go.

[00:03:19]
If you want to do non-proxy stuff, just don't do it. It's not smart. Great, any questions on that before we get into, yes?
>> Speaker 2: So you basically got the documentation that you need for how to set up a request by going through the process of creating this.
>> Scott: Exactly, yep.

[00:03:37]
>> Speaker 2: Is there documentation elsewhere? If I was gonna create my own API documentation for my consumers, I need those headers, the way that they're formatted or whatever. Where do I find that information outside of this console?
>> Scott: So are you saying that, you have an API for your consumers and you wanna provide them documentation of how they can access your API?

[00:03:59]
>> Speaker 2: That's right. This is gonna be my API. This API Gateway is gonna be what I'm providing to my customers. How do I, where is documentation that I create. Do I need to create it from that window that you just made, or is there other Amazon-
>> Scott: Amazon-

[00:04:15]
>> Speaker 2: Or AWS documentation I can get that from?
>> Scott: Amazon does not have, I guess you would call it a portal. They don't have a documentation portal to even offer to users, so you're kind of screwed on that. [LAUGH] But when it comes down to this-
>> Speaker 2: I can fish in there, it's okay.

[00:04:36]
I can go in there, and see what I need to have.
>> Scott: Right, but what I was gonna say is you can change this, right? For instance, if we look at this API proxy, like this is the events. Your users don't have to send something like this. You see what I mean?

[00:04:52]
This is not what they'll be sending.
>> Speaker 2: Right.
>> Scott: They won't be sending. This is what API Gateway formats for you.
>> Speaker 2: Okay.
>> Scott: They'll just send a regular HTTP request like they've normally done. And the API Gateway's like, okay, cool.
>> Speaker 2: All right.
>> Scott: Based on that, here's what the lambda gets.

[00:05:03]
Does that make sense?
>> Speaker 2: That does make sense.
>> Scott: Yeah, and AWS API gateway is not like, yeah, it's the one that they want you to use. But there's other API Gateways out there like Kong or, I can't even, I've used so many of them. But yeah, when they call in, they might have lambda integrations and then they format the events completely different.

[00:05:24]
So it really depends on what's calling your lambda. It's just API Gateway, you gotta think about it as they're the ones, API Gateway is the tool that's calling your lambda and not so much your user, right? Okay.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now