How to use npm modules in AWS Lambda

avatar
Borislav Hadzhiev

Last updated: Feb 26, 2024
4 min

banner

# Table of Contents

  1. How to use npm modules Without Layers in AWS Lambda
  2. How to use npm modules With Layers in AWS Lambda

# How to use npm modules in AWS Lambda

There are 2 ways to use npm modules in a Lambda function:

  1. Directly zip the node_modules directory alongside your index.js handler file - very easy and straightforward.
However, Lambda has a size limit of 50 MB, so if your npm modules exceed that, you have to use layers
  1. Use Lambda layers - enables you to reuse the same third-party libraries between multiple functions.

This article covers how to use npm modules both with and without layers.

# How to use npm modules Without Layers in AWS Lambda

The code for this article is available on GitHub
You can look at this example in the without-layers directory in the GitHub repository.

To use npm modules in a Lambda function, you have to:

  1. Have the following folder structure:
shell
index.js package.json node_modules your_npm_modules

First, initialize the package.json file and install the necessary modules, in the directory where the index.js file is located:

shell
npm init -y npm install date-fns@2.24.0

Let's fill out the code for the index.js file, which makes use of the npm module:

The code for this article is available on GitHub
without-layers/index.js
// ๐Ÿ‘‡๏ธ import npm module const {format} = require('date-fns'); exports.handler = async event => { return { statusCode: 200, body: JSON.stringify({ // ๐Ÿ‘‡๏ธ use npm module today: format(new Date(), "๐Ÿ‘‰๏ธ 'Today is a' eeee"), }), }; };
  1. Open your terminal in the directory where index.js and node_modules are and zip the folder's contents:
without-layers
zip -r9 lambda.zip .
We have to zip the contents of the directory, not the directory itself. When the archive is extracted Lambda will look for an index.js file, not a directory/index.js file.
  1. Update the code of the Lambda function, by uploading the lambda.zip file. Open your terminal in the directory where the lambda.zip file is located and run the update-function-code command:
without-layers
aws lambda update-function-code --function-name YOUR_LAMBDA --zip-file fileb://lambda.zip

update lambda code

  1. Verify that the Lambda function's handler is set to index.handler (an index.js file exporting a function named handler):

verify handler configuration

  1. Invoke the lambda function and verify that it can use the npm module

verify lambda can use npm module

Now that we've zipped the index.js file and the node_modules directory our Lambda function can use the npm modules in it.

However, now we can't use the online editor. Our deployment package is too large, due to the size of the npm_modules folder.

Also, what if we need a particular npm package in multiple functions? It doesn't seem very efficient to repeat this process for every function.

This is where Lambda layers come in.

# How to use npm modules With Layers in AWS Lambda

The code for this article is available on GitHub
You can view the code for this example in the with-layers directory of the GitHub repository.

To use npm modules, with layers, in a Lambda function, you have to:

  1. Have the following folder structure:
with-layers
index.js layers date-fns nodejs package.json node_modules

Note that the nodejs/node_modules folder structure is what Lambda expects when working with layers in Node.js - source.

lambda layer folder structure

  1. Open your terminal in the layers/date-fns/nodejs directory and install your npm modules:
layers/date-fns/nodejs
npm init -y npm install date-fns@2.24.0
  1. Open your terminal in the layers/date-fns directory and zip the contents.
When the zip archive is extracted, the nodejs/node_modules directory structure has to be directly accessible.
layers/date-fns
# in the layers/date-fns directory zip -r9 date-fns-layer.zip .
  1. To create or update the Lambda layer, open your terminal in the layers/date-fns directory and run the publish-layer-version command:
layers/date-fns
aws lambda publish-layer-version --layer-name date-fns-layer --description "add date-fns library" --zip-file fileb://date-fns-layer.zip --compatible-runtimes nodejs10.x nodejs12.x nodejs14.x

publish layer code

  1. Attach the layer to the Lambda function. The easiest way to add a layer to a function is using the AWS Lambda console.

In the Code tab scroll down to the Layers section and click Add a layer.

add a layer

Select Custom layers and pick your layer from the drop-down menu, selecting the latest version.

select your layer

  1. The last step is to update the code of the Lambda function. Enter the following code in your index.js file:
The code for this article is available on GitHub
index.js
// ๐Ÿ‘‡๏ธ import npm package const {format} = require('date-fns'); exports.handler = async event => { return { statusCode: 200, body: JSON.stringify({ // ๐Ÿ‘‡๏ธ use npm package today: format(new Date(), "๐Ÿ‘‰๏ธ 'Today is a' eeee"), }), }; };

The code simply imports the npm module from the layer and makes use of it.

Open your terminal in the directory where the index.js file is located and zip the file.

Note that we are zipping the index.js file, not the folder that contains it.
with-layers
zip -r9 lambda.zip index.js

zip lambda code

Finally, let's update the function's code. Open your terminal in the directory where the lambda.zip file is located and run the update-lambda-code command:

with-layers
aws lambda update-function-code --function-name YOUR_LAMBDA --zip-file fileb://lambda.zip

update lambda code with layers

  1. Verify that the Lambda function's handler is set to index.handler (an index.js file exporting a function named handler):

verify handler configuration

  1. Invoke the Lambda function and verify that it can use the npm module from the layer

verify lambda can use npm module

The node_modules directory is not visible in the online code editor and now we can view the code of our Lambda function.

lambda code visible

You can reuse the Lambda layer for multiple functions. You can even include multiple npm modules or even your own helper functions that you reuse between your lambdas.

You can use up to five layers in a single Lambda function.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev