Check out a free preview of the full Serverless with AWS Lambda course:
The "Your First Lambda Exercise" 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:

In this exercise, students create a lambda function and execute it locally on their respective machine. Then students pass in event data into the function.

Get Unlimited Access Now

Transcript from the "Your First Lambda Exercise" Lesson

>> Scott Moss: Okay.
>> Speaker 2: I have a quick question.
>> Scott Moss: Yeah, what's up?
>> Speaker 2: In the repo.
>> Scott Moss: Yeah.
>> Speaker 2: I just cloned it with the serverless yaml, looks a little bit different.
>> Scott Moss: Yes, let's take a look at that.
>> Speaker 2: Cuz there's no functions in that yaml file.
>> Scott Moss: Yeah, cuz you'll be making them.

>> Speaker 2: Gotcha.
>> Scott Moss: [LAUGH] That's what you're going to be doing. Yeah. Yeah, so let's look at that one, let's look at the yaml file in your repo, and let's just talk about the differences here. So we have this service, I just called it the serverless, for the provider, we talked about just the name, aws.

[00:00:32] For profile, this is the name of my aws profile, that my credentials are sort of my computer. If you don't have aws profile and you just set it to default, or if you're exporting through a batch profile, then you don't need this. You can just get rid of that.

[00:00:46] In fact, I'm almost positive no one here has an aws profile called frontend masters. So you can just comment that out. This only matters when we go deploy things, so it's not that big of a deal in this lesson. This is only going come down to when we actually deploy something.

[00:00:59] This is for aws permissions. And then, again, these are also for aws. What region do you want to deploy to? And what stage? And we'll talk about stages when we deploy in a minute. But don't worry about that right now. And then plugins is exactly what it sounds like.

[00:01:14] We're telling AWS what plugins we want to use. We're gonna talk about the serverless-offline plugin when we do the API lesson. Right now it doesn't matter, but that's where you put that. So yeah, for you, to answer your question, yeah, you would come down here and you would have to register some functions, and you would have to get them to work.

[00:01:33] So speaking of, that's what you're gonna do. If you look in the source folder, there's two files in here. There's a helloworld that has a function that has being exported on a handler. It's got and event, context and then done. I mean you can call these whatever you want, but the order is always gonna be an event first, followed by context, followed by done.

[00:01:54] Then you have this even.json. So I'm gonnao walk you through why this is here and what you can do with it. So we already talked about the starter argument, which is basically the callback that follows the node style pattern of error first and then response second. So whenever you're done inside this handler, you will call done, and you will send back to response that you want.

[00:02:15] The second argument here, context. This is basically meta information about the execution context of your lander. So locally, there literally won't be anything there, unless you pass something there. But when you run this on AWS or if you run this with serverless offline, there's going to be some context around what stage you're on, what was the ARN?

[00:02:34] Who is the IM user? Just tons of context about the execution of that function that will help you do things. So that's more for advanced cases, but very useful. And then the event is actually what was the pay load. You can think of the event as like, if you were doing a request to your API, what did you pass to the body?

[00:02:53] That's what the event is, it's whatever the payload that was sent to this Lambda, and it's always gonna be that. And depending on what event that your Lambda subscribes to in AWS, this event object is gonna look completely different. If you're subscribing to an API event compared to some other event, it's gonna look completely different.

[00:03:12] So those are your three arguments. So what you're gonna do is you're gonna find a way to get this helloworld function to actually work. And so you can actually execute it on the command line using sls, invoke, local. So you want to be able to invoke that function local.

[00:03:28] And the reason this event.json is here, because if you look at sls or serverless, and you type in invoke, and you type in help, you'll see that there's a flag that you can pass in called path. And you can get a path to a JSON or YAML file.

[00:03:47] And it will take that JSON file, and it will pass it as an input to the Lambda. So that's why have this event.json. So you can use that as a file to patch in as an input to this Lamda, so you can see what that event object would look like.

[00:04:02] So you can actually see what's actually happening. So again, there's a flag that you can use on the serverless. You give it a path to a file, and it will grab the data in that file and pass it as the first argument in your Lambda as the event.

[00:04:17] So use that, get a Hello World going, and then once you get back make another one and see whether you can do it