MIKAEELS

Explore the blog

How to use npm modules in AWS Lambda

When it comes to incorporating npm modules into your AWS Lambda functions, there are two primary approaches to consider:

  1. Direct Inclusion: This method involves directly zipping the node_modules directory along with your app.js handler file. It's a simple and straightforward approach, ideal for smaller projects.

    Package limits: The compressed direct-upload quota differs from the combined uncompressed function-and-layer quota. A layer does not bypass the latter. Check Lambda quotas and consider S3 upload or an image deployment when appropriate.

  2. Lambda Layers: For larger projects or scenarios where you want to reuse the same third-party libraries across multiple Lambda functions, Lambda Layers come into play. These layers enable you to separate your npm modules from your function code, making it easier to manage and share dependencies.

In this blog, we will explore both of these methods, demonstrating how to work with npm modules in AWS Lambda, with and without Lambda Layers.

How to use npm modules Without Layers in AWS Lambda

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

  1. Have the following folder structure:

    app.js package.json node_modules your_npm_modules

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

    npm init -y npm install axios

    Let's fill out the code for the app.js file, which makes use of the npmmodule:

    const axios = require("axios"); exports.handler = async (event, context) => { try { const response = await axios.get("https://example.com/api/data"); // Replace with your API endpoint. const data = response.data; return { statusCode: 200, body: JSON.stringify(data), }; } catch (error) { return { statusCode: 500, body: JSON.stringify({ message: "Error occurred" }), }; } };
  2. Open your terminal in the directory where app.js and node_modules are and zip the folder's contents:

    zip -r lambda.zip app.js package.json node_modules

    Note: We have to zip the contents of the directory, not the directory itself. When the archive is extracted lambda will look for a** app.js file, not a some-directory/app.js file.**

  3. Update the code of the lambda function, by uploading the lambda.zipfile. Open your terminal in the directory where the lambda.zip file is located and run the aws lambda update-function-code command:

    aws lambda update-function-code --function-name <YOUR_LAMBDA> --zip-file fileb://lambda.zip

    Note: fileb://: This is used for binary files. The content of the file is treated as binary data. For Lambda function code, which is typically a binary ZIP archive, you should use fileb://.

  4. Verify that the lambda function's handler is set to app.handler (anapp.js file exporting a function named handler).

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

    aws lambda invoke --function-name yourFunctionName --cli-binary-format raw-in-base64-out --payload '{"name": "mikaeel"}' response.json

    Note: Now that we've zipped the app.js file and the node_modulesdirectory 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 when lambda layers come in.

How to use npm modules With Layers in AWS Lambda

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

  1. Have the following folder structure:

    app.js layers axios nodejs package.json node_modules

    Note: Note that the nodejs/node_modules folder structure is what lambda expects when working with layers in Node.js - see source** **here.

  2. Open your terminal in the layers/axios/nodejs directory and install your npm modules:

    npm init -y npm install axios
  3. Open your terminal in the layers/axios directory and zip the contents.

    Note: When the zip archive is extracted, the nodejs/node_modulesdirectory structure has to be directly accessible.

    # in the layers/axios directory zip -r axios-layer.zip nodejs
  4. To create or update the lambda layer, open your terminal in thelayers/axios directory and run the publish-layer-versioncommand:

    aws lambda publish-layer-version --layer-name axios-layer --description "add axios library" --zip-file fileb://axios-layer.zip --compatible-runtimes nodejs22.x
  5. Attach the layer to the lambda function. The easiest way to add a layer to a function is by using the AWS Lambda console.

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

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

  6. The last step is to update the code of the lambda function. Enter the following code in your app.js file:

    const axios = require("axios"); exports.handler = async (event, context) => { try { const response = await axios.get("https://example.com/api/data"); // Replace with your API endpoint. const data = response.data; return { statusCode: 200, body: JSON.stringify(data), }; } catch (error) { return { statusCode: 500, body: JSON.stringify({ message: "Error occurred" }), }; } };

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

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

    Note: Note that we are zipping the app.js file, not the folder that contains it.

    zip -r lambda.zip app.js

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

    aws lambda update-function-code --function-name yourFunctionName --zip-file fileb://lambda.zip
  7. Verify that the lambda function's handler is set to app.handler in lambda console (an app.js file exporting a function named handler).

  8. Invoke the lambda function and verify that it can use the npm module from the layer.

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

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

Runtime and packaging checks

Use a supported Node.js runtime and preserve package-lock.json. Run npm ci --omit=dev in a clean packaging directory, and build native dependencies for the Lambda operating system and architecture. The example URL is a placeholder; replace it with a real API and configure timeout/error handling before deployment. For a CDK packaging workflow, see Lambda HTTP API or Dockerized Lambda.

Quick Recap

What's the file size limit for bundling npm modules directly in a Lambda deployment package?

The direct-upload compressed ZIP limit is separate from the uncompressed package limit. Layers count toward the combined uncompressed limit; they do not bypass it. Larger ZIP uploads can use S3 where supported. Check the current Lambda quotas.

What's the difference between bundling npm modules directly vs using a Lambda Layer?

Direct inclusion zips node_modules alongside your handler code into one deployment package. It is simple and is subject to ZIP deployment quotas. A layer can share dependencies but still counts toward the function's combined uncompressed limit. A Lambda Layer separates dependencies into their own reusable package, attached to the function, and can be shared across multiple Lambdas.

What folder structure does a Lambda Layer need for Node.js modules?

The layer zip must contain a nodejs/node_modules directory structure at its root. Lambda specifically looks for dependencies under nodejs/node_modules when the layer is extracted.

How do you publish a Lambda Layer?

Zip the layer's nodejs directory, then run aws lambda publish-layer-version with --layer-name, --zip-file, and --compatible-runtimes flags, and attach the resulting layer to your function from the Lambda console.

Add Mikaeels Blog as a preferred source on Google

Comments

Share your thoughts and questions below