Configure and deploy NodeJS function to AWS Lambda using serverless framework

Sumant Mishra
3 min readNov 5, 2019

Here, we will discuss how to set up the serverless framework using NodeJS, configure and deploy our code (function) in AWS Lambda using terminal.

The serverless framework is an easy and open way to build serverless applications. It’s a command-line tool that provides scaffolding, workflow automation, and best practices for developing and deploying your code as Functions as a Service (FaaS).

Install via npm

Run below command to install serverless globally:

npm install -g serverless

Note: sudo may be required in Mac to install the package globally.

Configure AWS credentials

To deploy in AWS as a Lambda function, we need to provide Access Key ID and Secret Access Key for an IAM User. To set credentials, run below command:

serverless config credentials --provider aws --key [Access key ID] --secret [Secret access key]

[Access key ID] and [Secret access key] need to be replaced with actual Access Key ID and Secret access Key. Please refer to AWS documentation for creating IAM users in AWS and getting credentials.

To change credentials, please use -o parameter in the above command:

serverless config credentials --provider aws --key [Access key ID] --secret [Secret access key] -o

Create a Service

To create a new service or serverless project, run below command in the terminal:

# Create a new Serverless Service/Projectserverless create --template aws-nodejs --path my-service# Change into the newly created directorycd my-service

Above command will create a folder structure like below:

Configure .yml file

By default, it comes with below configurations:

In the above code, aws is the cloud provider and the lambda function name is hello. The default AWS region is us-east-1 which can be overwritten.

role is required with permissions allowing services like cloudwatch and cloudformation.

Handler function

After scaffolding the serverless project, it creates a JS file named handler.js with some predefined code:

This code can be modified depending on the need.

Deploy in AWS

To deploy our serverless code in AWS Lambda, we just simply need to run the below command in terminal:

serverless deploy

After successful deployment, you should be able to see the function in the functions list in AWS Lambda service dashboard:

On clicking on the lambda function name, code can be seen there:

I hope, this will help in scaffolding a new serverless project, deploy and maintain your function in AWS Lambda service.

Please share your valuable feedback.

Thanks!

--

--

Sumant Mishra

Fullstack Architect || TOGAF 9 || AWSCSAA || Cloud Practitioner || NodeJS || React & Angular || Docker || Coder