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

The "Lambda API Solution" 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 walks through the solution to Lambda API Exercise by configuring the serverless YAML to build an API on top of lambda functions.


Transcript from the "Lambda API Solution" Lesson

>> Scott Moss: Welcome back, I hope everyone had a chance to setup the route handlers in the service yellow file for the to-do's and the to-do route. And we're also going to walk through how to set up parameters as one of these routes as well and then how you can access them inside your lambda functions.

So first things first, we can pretty much just, if you followed along before. You could probably just copy what I have here, and we'll place that on the todos underneath the handler while I have both of these open. And we'll just paste that there. And instead, we'll just call this todo GET.

And then for the todo one, this is where we wanna use an ID cuz it's rest and That's just how you follow with a parameter. So we'll call this one todo and then for the parameter we'll use this syntax here, and then we'll just say id. And this right here is basically saying here's a place holder for an ID that I wanna use.

Now, depending on what version of servers offline we're using all these in the gateway now you might also have to come down here and add like parameters,
>> Scott Moss: parameters, okay, I'm sorry, yeah there it is, it's request, yeah, request object parameters.
>> Scott Moss: And then in the parameters you have to say ID True.

Just to make sure it actually goes through API gateway. Because API gateway is very configurable and very strict. So you would have to, this is telling to, like, hey, allow this to happen. Allow it to happen so the Landa can access it. Otherwise it may or may not do it.

The behavior I've observed is [INAUDIBLE] list. So it's better to be explicit that leave it up to chance.
>> Scott Moss: So, this sets up our route. And from here always should be able to do is just coming here, yarn dev, and on 4,500 here I'm just gonna use http pi here to a 4,500.

And I'm gonna head up todo, and boom, we get back those to-dos good to go. And then, I'm gonna do Slash, some random ID here, and also get back that todo. And then as far as the lambdas themselves, and what that will look like, if we go into the To Do on the hazard parameter.

Did anybody look to see where that parameter is coming in and how you can access it? Alright, what is it on? Is it on the event? Is it on the context?
>> Speaker 2: It's on the event.
>> Scott Moss: It's on the event, yes. So, let's just look at this now.

Let's just verify what's going on here. I know we look at the event in AWS. Let's just see what's happening here. And restart-
>> Speaker 2: You have some typos.
>> Scott Moss: It's not gonna work.
>> Speaker 2: Nice.
>> Speaker 2: In JSON and-.
>> Scott Moss: No. There we go, cool. I don't know why VS Code has that autocomplete just in your face as you're typing.

It's so annoying.
>> Scott Moss: All right. So now, if we run, is that TODO? Yeah.
>> Scott Moss: And we look at the logs, here's the event. You can see we got some headers, the actual PATH, so this is the PATH relative to the ROOT. Any parameters. This is what that parameter true does that actually just sets it up really nice for you like this.

And then just like the request context, the account ID, the resource ID, stage, all those different type of things. Authorizer, we'll talk a little more about authorizers, but we will not actually writing custom authorizers, and then just some basic other stuff here. Anybody take a look at the context object and what was on here?

>> Scott Moss: No? All right, let's take a look.
>> Scott Moss: So in the context object, again it really depends on what you're doing, if you need this or not But for this case for offline it really is not that helpful cuz it's offline. But basically, it gives you the name of the lambda function you're actually executing in which is great for things like analytics and stuff like that.

If you're trying to log analytics on your lambda functions, now you know what function was ran. So that's mostly what I use context for is things like analytics cuz it's more of Platform information than it is the actual event. So then, the versioning of your function, because you can actually version your lambdas, and we'll talk about that when we deploy.

You'll get the ARN. If you don't know what ARN in Amazon, ARN stands for Amazon resource number. It's basically a unique identifier for any resource on Amazon, whether it's a lambda function All the way to a database table. It's a unique identifier. And it's a combination of your account ID, the region, some random thing.

It's a unique identifier. So this is the ARN for this lambda function which, again, is really good for analytics and stuff like that and debugging. And then, you just get things like the request ID, which is the ID that AWS API Gateway assigned to this request. Sometimes, it'll be in the header.

You should probably get rid of it, because it's a security thing. And we didn't talk about this, but by default, when you deploy to AWS, Service creates a log group for you. So you could actually go look at your logs. This is the group name and the stream name of those logs, so you can go check them out.

So whenever you do a log inside of Lambda on AWS, those logs get written to a file. This is where these files are on AWS. This is the name of it. So when we deploy again, we'll go check out those logs and see what's happening. The other way is you can check out your logs is through your terminal 2, you can stream them but just looking at them while you do this is probably pretty simpler.

And then identity, this is if you did a customer authorizer, like if you add another lambda function in front of this lambda function that authorized access to this lambda function. So AWS has this thing called incognito which is like there are version of Zero. And they do things like social out, password out, and JWTs.

It would add things here, and then, client context is just context of the client sent of if you need which is almost never and that's basically what the context object is. Any questions on any of the stuff?
>> Scott Moss: Can you mock the context, similar to how you do the event?

>> Speaker 2: Yeah, from my understanding, you can't do it through service offline. But if you run evoke locally, you can mock out whatever the hell you want.
>> Scott Moss: Okay.
>> Speaker 2: Yeah.

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