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

The AWS Lambda runtime environment doesn't have certain features from the latest version of an AWS SDK. How do I integrate the latest version of the AWS SDK for JavaScript using layers in my Node.js Lambda function?Short descriptionTo integrate the latest version of an AWS SDK into your Lambda function's deployment package, create a Lambda layer, and then add it to your function. You can use either the AWS Command Line Interface (AWS CLI) or the Lambda console to create a Lambda layer and add it to your function.The following procedure uses the Node.js 14x runtime as an example. For a complete list of runtimes and the AWS SDK versions that Lambda currently uses, see AWS Lambda runtimes.Note: The following solution increases the size of your function's deployment package. For information on Lambda storage limits, see Lambda quotas.

ResolutionNote: If you receive errors when running AWS Command Line Interface (AWS CLI) commands, make sure that you're using the most recent AWS CLI version.(Optional) Confirm the AWS SDK version that your function is usingTo confirm which AWS SDK version your function uses, do the following:1.    Create a function in the Lambda console.2.    Update the function's code to return the version of the AWS SDK it's using when the function is invoked.Node.js code to have a Lambda function output the version of the AWS SDK that it's usingconst AWS = require('aws-sdk') exports.handler = async (event) => {return AWS.VERSION;};3.    Invoke your function. The function returns the version of the AWS SDK that it's using.4.    Compare the version of the AWS SDK returned by your function to the latest version of the AWS SDK listed in the AWS SDK documentation.Install and package the latest version of the AWS SDKNote: Deployment packages must be compatible with the Lambda runtime that you're using. It's a best practice to use the same operating system for your runtime that's specified in AWS Lambda runtimes. For example, by launching a compatible Amazon Elastic Compute Cloud (Amazon EC2) instance.In a local, Lambda-compatible development environment, do the following:1.    Create a working directory by running the following command line interface (CLI) command:mkdir -p aws-sdk-layer/nodejs2.    Change to the working directory by running the following command:cd aws-sdk-layer/nodejs3.    To install the latest version of the AWS SDK, use an Amazon Linux 2-compatible instance to run the following command:npm install aws-sdkFor 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 developing Lambda resources.-or-If you're using a Windows or macOS operating system for development, you can use Docker to run the following command:docker run --entrypoint "" -v "$PWD":/var/task "public.ecr.aws/lambda/nodejs:14" /bin/sh -c "npm install aws-sdk; exit"Note: Make sure that you're using the most recent version of Docker before running the command.4.    Create a .zip file to upload to your Lambda layer by running the following command:zip -r ../package.zip ../5.    (Optional) Verify the version of the AWS SDK that you installed by running the following command:cat package-lock.jsonCommand output example{  "requires": true,  "lockfileVersion": 1,  "dependencies": {    "aws-sdk": {      "version": "2.888.0",...To create a Lambda layer and add it to your function using the AWS CLI1.    To create a new Lambda layer that includes the latest version of the AWS SDK that you want to use, run the following publish-layer-version AWS CLI command:Note: Replace node_sdk with the name that you want to give the layer. Replace My layer with a description of the layer. Replace the compatible-runtimes value with the runtime you're using. Replace the region value with the AWS Region that your function and layer are in.aws lambda publish-layer-version --layer-name node_sdk --description "My layer" --license-info "MIT" --compatible-runtimes nodejs14.x --zip-file fileb://../package.zip --region <specify a region>You need the LayerVersionArn value that's in the command output to complete the next step.2.    To add the layer to your function, run the following update-function-configuration command:Note: Replace my-function with the name of your function. Replace arn:aws:lambda:us-east-2:123456789012:layer:node_sdk:1 with the LayerVersionArn value that was in the output of the publish-layer-version command.aws lambda update-function-configuration --function-name my-function --layers arn:aws:lambda:us-east-2:123456789012:layer:node_sdk:1 --region <specify a region>For more information, see Configuring a function to use layers.To create a Lambda layer and add it to your function using the Lambda console1.    Open the Layers page in the Lambda console.2.    Choose Create layer. Then, do the following:For Name, enter a name for the new layer.(Optional) For Description - optional, enter a description for the layer.Choose Upload a .zip file.Choose Upload. Then, choose the name of your deployment package .zip file.For Compatible runtimes - optional, choose one or more compatible runtimes. For the example in this article, Node.js 14X.(Optional) For License - optional, enter any applicable software license information for the layer.Choose Create.3.    Open the Functions page in the Lambda console. Then, choose the name of the function that you want to add the layer to.4.    Below Function overview, choose Layers.5.    Choose Add a layer.6.    Under Choose a layer, choose Custom layers. Then, do the following:Choose the name of the layer that you created.For Version, choose the version of the layer that you wish to use. For example this example, 1.Choose Add.(Optional) Test the setupInvoke your function following the steps in the (Optional) Confirm which AWS SDK version your function is using section. The function returns the version of the AWS SDK that it's using.You can also verify the latest version of the AWS SDK in the CHANGELOG file of the AWS SDK that you're using. For more information, see Changelog for AWS SDK for JavaScript on GitHub.

Related informationProgramming model (AWS Lambda developer guide)Best practices for working with AWS Lambda functionsHow do I build a Lambda deployment package for Node.js?