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

Nik Graf
InstructorNik Graf
Share this video with your friends

Social Share Links

Send Tweet
Published 6 years ago
Updated 4 years ago

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.

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.

xuan
xuan
~ 6 years ago

I did exactly what you teach, but my command: sls invoke --function helloWorld --log

Return nothing. Any suggestions? Thanks!

Nik Graf
Nik Grafinstructor
~ 6 years ago

Hey xuan, is there no output at all? Can you elaborate what's happening?

Dineshchse
Dineshchse
~ 6 years ago

after sls deploy I get this output Serverless: Packaging service... Serverless: Service files not changed. Skipping deployment... Service Information service: my-app stage: dev region: us-east-1 stack: my-app-dev resources: 1 api keys: None endpoints: None functions: None layers: None

but file structure and contents I have same as you did :(

Fernando Balmaceda
Fernando Balmaceda
~ 5 years ago

I have an error when I try to deploy:

Serverless Error:
 
The AWS Access Key Id needs a subscription for the service
Evan Gillogley
Evan Gillogley
~ 5 years ago

The current config will fail. Ensure to use the latest node runtime nodejs10.x or nodejs12.x.

Markdown supported.
Become a member to join the discussionEnroll Today