Skip to content

How do I use layers to integrate the latest version of the AWS SDK for JavaScript into my Node.js Lambda function?

5 minute read
0

The AWS Lambda runtime environment doesn't have all features from the latest version of an AWS SDK. I want to integrate the latest version of the AWS SDK for JavaScript into my Node.js Lambda function.

Short description

To integrate the latest version of an AWS SDK into your Lambda function's deployment package, create a Lambda layer. Then, add the layer to your function.

Note: For Node.js versions 16 and earlier, the Lambda Node.js runtime includes the AWS SDK for JavaScript version 2. For Node.js versions 18 and later, the Lambda Node.js runtime includes the AWS SDK for JavaScript version 3. For more information, see AWS SDK for JavaScript Documentation.

For a complete list of runtimes and the AWS SDK versions that Lambda uses, see Lambda runtimes.

Note: The following resolution increases the size of your function's deployment package. For information on Lambda storage quotas, see Lambda quotas.

Resolution

Note: If you receive errors when you run AWS Command Line Interface (AWS CLI) commands, then see Troubleshooting errors for the AWS CLI. Also, make sure that you're using the most recent AWS CLI version.

(Optional) Confirm the AWS SDK version that your function uses

Complete the following steps:

Note: Use this method only for Node.js versions 16 and earlier.

  1. Create a function in the Lambda console.

  2. Update the function's code to return the version of the AWS SDK that the function uses when you invoke the function:

    const AWS = require('aws-sdk')   
    exports.handler = async (event) => {  
    return AWS.VERSION;  
    };
  3. Invoke your function.

  4. Compare the version of the AWS SDK that your function returned with the latest version of the AWS SDK in the AWS SDK documentation.

Install and package the latest version of the AWS SDK

Note: Deployment packages must be compatible with the Lambda runtime that you use. It's a best practice to use the same operating system (OS) for your runtime that's specified in Lambda runtimes.

In a Lambda-compatible development environment, complete the following steps:

  1. Run the following command to create a directory:

    mkdir -p aws-sdk-layer/nodejs
  2. Run the following command to change to the directory:

    cd aws-sdk-layer/nodejs
  3. Install the AWS SDK for your Node.js runtime version:
    Node.js 16 and earlier (AWS SDK version 2)
    To install the latest version of the AWS SDK version 2, use an Amazon Linux 2 compatible instance to run the following command:

    npm install aws-sdk

    For more information, see Tutorial: Setting up Node.js on an Amazon EC2 instance.
    Note: It's a best practice to use an Amazon Linux 2 environment when you develop Lambda resources.
    -or-
    If you use a Windows or macOS OS for development, then use Docker to run the following command:

    docker run --entrypoint "" -v "$PWD":/var/task "public.ecr.aws/lambda/nodejs:<version>" /bin/sh -c "npm install aws-sdk; exit"

    Note: Before you run the command, make sure that you're using the most recent version of Docker on the Docker website.

    Node.js 18 and later (AWS SDK version 3)
    To install the latest version of the AWS SDK version 3, see Install the SDK for JavaScript.

  4. Create a .zip file to upload to your Lambda layer:

    zip -r ../package.zip ../
  5. (Optional) Verify the version of the AWS SDK that you installed:

    cat package-lock.json

    Example output:

    {  "requires": true,    "lockfileVersion": 1,  
      "dependencies": {  
        "aws-sdk": {  
          "version": "2.888.0",  
    ...

Use the AWS CLI or the Lambda console to create a Lambda layer and add it to your function

AWS CLI

  1. To create a new Lambda layer that includes the latest version of the AWS SDK, run the following publish-layer-version command:

    aws lambda publish-layer-version --layer-name node_sdk --description "My layer" --license-info "MIT" --compatible-runtimes --zip-file fileb://../package.zip --region

    Note: Replace node_sdk with your layer's name, and My layer with a description of the layer. Also, replace the compatible-runtimes value with the runtime that you use, and region with the AWS Region that your function and layer are in.

  2. In the output, copy the LayerVersionArn value to use in the next step.

  3. To add the layer to your function, run the following update-function-configuration command:

    aws lambda update-function-configuration --function-name my-function --layers arn:aws:lambda:us-east-2:123456789012:layer:node_sdk:1 --region

    Note: Replace my-function with the name of your function, arn:aws:lambda:us-east-2:123456789012:layer:node_sdk:1 with the LayerVersionArn value from your output, and region with your Region.

For more information, see Managing Lambda dependencies with layers.

Lambda console

To create a Lambda layer and add it to your function in the Lambda console, complete the following steps:

  1. Open the Layers page in the Lambda console.
  2. Choose Create layer.
  3. For Name, enter a name for the new layer.
  4. Choose Upload a .zip file, and then choose the name of your deployment package .zip file.
  5. Choose Create.
  6. Open the Functions page in the Lambda console, and then choose the name of the function that you want to add the layer to.
  7. In Layers, choose Add a layer.
  8. For Choose a layer, choose Custom layers.
  9. Choose the Custom Layers dropdown list, and then choose your custom layer.
  10. Choose Add.

(Optional) test the setup

Complete the steps in the (Optional) Confirm the AWS SDK version that your function uses section to invoke your function. The function returns the version of the AWS SDK that it uses.

Related information

Best practices for working with AWS Lambda functions

How do I build a Lambda deployment package for Node.js?

How can I create a layer for my Lambda Python function?

AWS OFFICIALUpdated 10 months ago
3 Comments

Unfortunately using require throws an error:

"errorMessage": "require is not defined in ES module scope, you can use import instead"

Using import is more modern, however, the modules cannot be found:

"errorMessage": "Cannot find package 'aws-sdk' imported from /var/task/index.mjs"
replied 3 years ago

Thank you for your comment. We'll review and update the Knowledge Center article as needed.

AWS
MODERATOR
replied 3 years ago

You can address the missing package error by following the guidance seen in the resources below:

  1. Creating a layer containing the AWS SDK: https://aws.amazon.com/blogs/compute/using-lambda-layers-to-simplify-your-development-process/

  2. How do I resolve the "Cannot find module" or "Cannot find Package" errors when I run Lambda code in Node.js?: https://repost.aws/knowledge-center/lambda-import-module-error-nodejs

AWS
replied 3 years ago