Upload a npm module to AWS lambda

Ankita Kumari
Underscore
Published in
2 min readApr 17, 2019

Lately, I was working on an application using AWS lambda. Being a JavaScript developer, my obvious choice was Node.js. I have been using Node.js for more than 3 years now, hence I frequently use the libraries created by the large, vibrant and innovative Node.js community members.

Working with AWS lambda, I required to install a library from npm. It took me quite sometime to figure out a way to get the library on lambda and start using it. That’s when I decided to write this article to help anyone and save at least an hour of their life so that they can do something better.

TL; DR;

To start with, we need a lambda function. Create a function using lambda on AWS console by just entering the name.

Once you are ready with your function on lambda, move to your local system and create a nodejs application.

$ npm init

Create a file and write your lambda function in there.

exports.myHandler = function(event, context, callback) {
// function code
callback(null, “some success message”);
// or
callback(“some error type”);
}

Install the required package using npm

$ npm i <package_name> --save

Once your package is installed. Use it in your code.

var package = require('package_name');exports.myHandler = function(event, context, callback) {
// function code
package.use() //Use your package
callback(null, “some success message”);
// or
callback(“some error type”);
}

Run your code locally to test if it works. Compress the contents of your folder. Use AWS-cli to update the function on lambda. If you do not have AWS-cli installed you can look in here.

$ aws lambda update-function-code --function-name <function_name> --zip-file fileb://~/path/to/your/.zip

Your lambda function is up with the npm module and ready to be tested on the AWS console.

Thanks for reading !!!

Kindly help me grow as a writer and developer with your comments and claps. 😊

--

--