Write and Test Your First AWS Lambda Function

Sam Julien
InstructorSam Julien

Share this video with your friends

Send Tweet
Published 3 months ago
Updated 3 weeks ago

After creating a lambda function you get access to an in-browser editor where you can write, test, and deploy your function.

When testing your function you get access to a menu where you can edit the event body that gets passed to the lambda function.

Sam Julien: [0:00] Let's create our first AWS Lambda serverless function. I'm here in the management console. You might see Lambda in your recently visited services. If not, it's under Services and just under Compute, the third from the top there. I also like to star mine so that Lambda shows up in my Favorites sidebar over here.

[0:20] I'll go ahead and go to Lambda. It's going to list out your functions. I don't have any currently. You can go over here to this orange button and click Create Function. It doesn't matter what region that you're in right now. We're going to create one wherever you happen to be.

[0:36] We're going to leave this as Author from Scratch. There are also lots of blueprints available on AWS for different types of Lambda functions. Feel free to explore those. You can also use container images, or you can browse a serverless application repository. For us, we're going to leave this as Author from Scratch because we're just going to show off the basic "Hello, world!" function.

[0:59] We need to enter a function name. I'm going to type, Egghead Hello Lambda. Then you can choose the runtime. We're going to use Node.js, but notice here that you can also use things like .NET or Go or Java or Python or Ruby. There's a lot of different options for what you can run on AWS Lambda. I'm going to leave it at Node. We're going to leave all these other things just as the default and click Create Function.

[1:24] It'll take Lambda a couple of seconds to spin up this function, but once it's done, it's going to take us to our Code Editor. If I scroll down, you can see here that I've got the index.js, and it's just going to return a body of "Hello from Lambda!" I can go ahead and test this by using this test event.

[1:45] There's no body here in this function quite yet, but that's OK. We can leave this for now and click Create. Let's add in a name and hit Create. We can run test. You can see here that we get our Status Code of 200 and our response of body "Hello from Lambda!" Let's add in a parameter there, just so we can see that what we're passing through will work.

[2:09] Inside of this JSON.stringify(), I'm going to change this to event.dog and go ahead and deploy that. Then, in our test, let's click on this Test, go to Configure Test Event, and let's replace key1 with dog and replace value1 with bark. Then I'll delete this, and I'll hit Save.

[2:32] Now, if I hit Test again, you can see here that the Status Code is 200, but now, the body is bark. Now, we've created our very first Lambda function. It's just a serverless function. We can change this code in index.js to do whatever we want, calculate things, hit another API.

[2:50] Easy as that, we didn't have to actually spin up a server and do any sort of provisioning or anything like that.