Join egghead, unlock knowledge.

Want more egghead?

This lesson is for members. Join us? Get access to all 3,000+ tutorials + a community with expert developers around the world.

Unlock This Lesson
1×
Become a member
to unlock all features

Level Up!

Access all courses & lessons on egghead today and lock-in your price for life.

Autoplay

    Deploy a Node.js function to AWS Lambda using the Serverless Framework

    Nik GrafNik Graf
    awsAWS
    javascriptJavaScript
    nodeNode.js

    In this lesson we walk through the necessary configuration of a Lambda function, add a simple code snippet and then deploy the function to AWS Lambda. We test it using the invoke command coming with the CLI tool.

    Code

    Code

    Become a Member to view code

    You must be a Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson
    Transcript

    Transcript

    Instructor: 00:00 In order to configure a lambda function, we need to create a serverless.yml file. In here, we define multiple fields. First, we define a service name, myApp, then set the provider and the runtime. Next up, we allow a functions section, and add a Hello World function.

    00:22 The handler is referring to the file handler, which should export a function, run. Since we don't have that file yet, we create handler.js. We export the function run, accepting three parameters, event, context, and callback.

    00:44 The event is an object and contains all the necessary request data. Context is an object as well. It contains a couple of AWS-specific values, like AWS request ID, log group name, and so on. Callback is a function and should be invoked with either an error response as the first argument or a valid response as the second argument.

    01:07 Now we have all we need to deploy our first function. We can run SLS deploy from our terminal. This will package up to the function to a ZIP file, and then deploy the whole thing with a cloud formation stack.

    01:30 Once deployed, you can invoke the lambda function directly using SLS invoke. As expected, it returns Hello World. One cool feature about invoke is that you can also log out a debug statement. Let's try this by adding a console log, redeploy our application.

    01:58 Once finished, invoke with the --log flag. As an alternative, we also can run SLS logs, which will just return the past logs.

    02:16 Instead of using the callback function, we simply can return a promise as well.

    02:28 Now we could redeploy the whole stack, but we are not going to do this, because what we are going to use is SLS deploy function. This is way faster, as it doesn't go for cloud formation, but instead just replaces the code ZIP for the specific function.

    02:44 Once redeployed, we can invoke the function, and see that it only returns hello. Since Node 8 already supports async await, you also can declare a function as async and simply return the desired result. It works like a charm.

    Discuss

    Discuss