How to use npm modules in AWS Lambda

How to use npm modules in AWS Lambda

ยท

5 min read

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.

    ๐Ÿ’ก
    However, it's important to note that AWS Lambda has a file size limit of 50 MB. If your npm modules exceed this limit, you'll need to explore an alternative approach.
  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 .
    
    ๐Ÿ’ก
    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
    
    ๐Ÿ’ก
    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
    
    ๐Ÿ’ก
    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 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.

    ๐Ÿ’ก
    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 .
    
  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 nodejs14.x nodejs16.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 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.

    ๐Ÿ’ก
    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.

Did you find this article valuable?

Support Mikaeel Khalid by becoming a sponsor. Any amount is appreciated!

ย