Serverless with AWS Lambda Serverless Lambda Setup
Transcript from the "Serverless Lambda Setup" Lesson
>> Scott Moss: So if everyone has this repo up, the format is pretty much in the readme, but just to go over it again. We're gonna be checking out to different lessons. Each lesson has a number on it. So we'll be starting with lesson one, so feel free to check out to that lesson, to lesson dash one.
[00:00:34] There aren't really too many differences when it comes down to the actual language that you choose to use inside of Lambda and AWS. Pretty much still has the same input and expects the same output, it's just your language of choice. So there really aren't that many differences here.
[00:00:49] There definitely are some differences, but that's more advanced and we will not be covering that stuff today. But I'm willing to talk about it, for sure. So let's do that, let's check out do lesson 1. And what we're gonna do is let's just open this. And you don't have to follow along here.
[00:01:11] You're gonna be released, and you're gonna do an exercise, but I'm just gonna go through a hello world lambda, some different examples, the configuration. And how you can get started and I'm gonna let you loose. So let's just take a tour of a couple things. First step, what you are gonna have to do, and you can it now or when you do your exercise, is you're gonna have to install globally serverless.
[00:01:36] So make sure you globally install that with whatever your flavor is. So make sure you globally install that. And once you get that going you should have called SLS, don't hit enter when you do that. You should have SLS or server less, SLS is the shorthand name for it, but you can just type out serverless.
[00:02:00] So you should have that command ready, and that's pretty much all you're gonna need to do this course. You got AWS set up, you got serverless. Again, if you don't have AWS set up, you can still follow along locally, you just won't be able to deploy. Okay. Now that we got that, I already have this repo here that's set up for you, it's already scaffolded out so you don't have to make a new project.
[00:02:24] What I'm gonna do now is I'm gonna walk through creating a new project from scratch and setting up a Hello World, and then you will be set to do that in this repo. So what I'm about to do now do not follow along you already have a repo set up for you I'm just gonna make a new one.
>> Scott Moss: For this lesson, so I'm just gonna say demo sls, oops.
>> Scott Moss: Actually. Demo.
>> Scott Moss: So what I'm gonna do is I'm gonna use the sls command which is short for serverless, and if you go to help, you'll see that there's some commands here. One is gonna be for generate.
[00:03:07] Where's it at?
>> Scott Moss: Actually they're not listed here, maybe they updated it. Let's look at the docs and see what that actual command is.
>> Student: Create?
>> Scott Moss: It's create now? Is that what it is? Let's see.
>> Scott Moss: Yep, it's create, okay, nice. So sls create, and then you can patch it at template, the template is basically what language do you want to start with?
[00:03:42] Do you want to use advanced things like TypeScript or WebPack, stuff like that. So and there's tons of templates. So we go look up serverless templates,
>> Scott Moss: And we look up AWS node is the one we're gonna be using.
>> Scott Moss: Actually, let's look at the how to do it and not the actual code.
>> Scott Moss: So basically, if you pass in a template like this, let me zoom in on that, you can pass in the platform dash the language you're trying to use. And it'll actually that function with that language on the specific platform. So for us, we want to create one on the AWS platform using nodejs.
[00:04:31] So we'd say sls or serverless create -t, and for us it'd be aws-nodejs. And that's going to go ahead and create a function for us.
>> Scott Moss: So sls create -t aws and then nodejs.
>> Scott Moss: And it's gonna do all that stuff right there. Let's see where it put it.
[00:04:56] Looks like it might have put it, alright, it just put it right in the root. So let's do it in a different directory and we will call it Demo SOS. There we go.
>> Scott Moss: Cool. So now if I open this up, I now got a bare bones service function here.
[00:05:20] So let's take a tour of what's here. It's only three files, minus the get ignore it's only two files. So we get this one thing called a handler. So handler is the term that Lambda and most functions use to describe the actual function that's going to be deployed on the platform that you're deploying to.
[00:05:41] So you can think of the handler as the entry function into what you're gonna be accessing. It's the actual lambda, although your lambda could call other functions. Whether they're other lambda functions or functions in your code. This is the actual lambda function. And handlers is the key word that they use, you can call it whatever you want.
[00:05:58] That's just the word that they use, this is the handler. So basically they generated us a handler here and just from the looks of it you'll learn later today. This looks like it's gonna be a handler for an API, basically I know that because they're responding back with a status code.
[00:06:14] All lambda functions aren't just for APIs, these ones specifically looks like it was coded for that. So the quick things to note here is the parameters and we'll talk about that and then the response. So that's the handler file and then if you go to the service YML, this is the configuration file for a serverless itself.
[00:06:31] It's heavily commented, very well documented. So I'm not gonna cover everything here because it's literally so many things in here. In fact, you can extend it to put whatever you want. So there's an unlimited amount of options you can put in here. But the important things to remember is one, it's a service.
[00:06:47] This service is basically like the namespace for these groups of lambdas that you're gonna be creating. So you can put many lambdas in this project and you can deploy them together or separate it. But they're all pretty much gonna be namespace with this. You could think that's what a service is.
[00:07:01] It's like the name space for the lambdas. It's like their group. And then provider, because we chose AWS and node, it went ahead and put that in for us. AWS node, I'm actually gonna change that to 8.10, which is newly supportive AWS, like 3 or 4 weeks ago, maybe.
[00:07:17] So I'm going to do that. And then if you go all the way down here, you'll get to this part that says functions, and this is where the magic happens. So, let's open up the directory and we can just do a quick glance here. So functions, we have a function called hello.
[00:07:30] We can put whatever we want there. That name, it doesn't matter, that's the name of your function. This is how it's gonna show up on AWS. This is gonna be the name of your lambda. But it has nothing to do with the file name, it's whatever name you want this lambda to be on the platform that you deploy it to.
[00:07:47] So when we go look on AWS, if we were to deploy this function, we'd see a lambda function called hello. And then now we need to tell serverless, where is the source code for this function that we're calling hello. The way you do that is you add the handler property here, again that's the word that they use to describe the actual lambda.
[00:08:05] And then you just give it the path to the file that has the lambda. This path is relative to the serverless.yml. So if we go look at the handler.js, you'll notice that this file over here is called handler. Right here, oops. And then there is a .hello. That's not the file extension, that hello is the name of the export.
>> Scott Moss: So that has to match. So again, this is the name of the file, handler. And then dot, it's not the extension of the file, it's the name of the export that you're exporting. So those have to match or service will not find your function. It will just break.
[00:08:48] So now that we got that, what we can do is, I'll just keep this here. And then we can actually run this lambda. So let's do that, and there's a couple ways we can do it. Because we didn't set up an api, this is just a function, we can just invoke the function locally.
[00:09:01] We're not going to set up an api right now, that's later. So the way we can do that is we can just type in sls invoke, and then you have to pass in local. If you don't pass in local, it's gonna try to invoke it in AWS but it's not there yet so it'll just break.
[00:09:14] So you add type in local and -f is the name of the function that you actually want to invoke in our case the function is called, hello.
>> Scott Moss: So if I'll run it you can see that's what this function returns which is exactly what was in the callback response here
>> Scott Moss: So again this all worked with service because we told service that we had a function called hello. Its source is pointing to this path called handler which is the file name. And then the export is hello. And then inside that handler we have this three events. We don't, these events right here, this event, this context, and this callback, is not so important right now, because we're going to get to it.
[00:10:02] But really, the only thing to note here is that if you've ever used node, you know the callback pattern is mostly error first, and then the actual data. That's the pattern that it's following. So if you had an error you would put the error here first. And then it would error out.
[00:10:17] But we don't have an error here, we're just gonna send back this response. And you could put back whatever you want. It's just, you literally put whatever you want here. When we get to APIs, you can't put whatever you want there. But we're just invoking this locally, so you can respond with, it really doesn't matter what you respond with.
[00:10:31] You can respond with whatever, because we're just invoking it locally.
>> Scott Moss: And that's basically, at the end of the day, that's a lambda. That's it. And then, you would deploy this to AWS. And then the next part is really how you actually activate this lambda. What does it respond to?
[00:10:51] Does it respond to HTTP events? Does it respond to S3 PUT and DELETEs. Does it respond to Kinesis Streams? Or DynamoDB events, there's so many things you could do which will change how these arguments look. And then that's where things start getting interesting.