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

Last updated: 2020-04-30

The AWS Lambda execution 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 Description

The AWS SDK versions built into the Lambda execution environment aren't always up-to-date, so some new SDK features might be unavailable. Use Lambda layers to integrate the latest version of an AWS SDK into your Lambda function deployment package.

Note: This solution increases the size of your function's deployment package. For information about Lambda storage resources, see AWS Lambda Limits.

The following instructions use the Node.js 10.x runtime as an example. For a list of runtimes for Node.js and other languages, and the AWS SDK versions currently used by Lambda, see AWS Lambda Runtimes.

Resolution

(Optional) Check the AWS SDK version

To confirm the current version of the AWS SDK, do the following:

1.    Create a function in the Lambda console.

2.    Update your function code to output the version of the AWS SDK. If you're using the Node.js 10x runtime, you can use this example code:

const AWS = require('aws-sdk')

exports.handler = async (event) => {
return AWS.VERSION;
};

3.    Execute your function. The function returns the version of the AWS SDK.

4.    Compare the AWS SDK version returned by your function to the latest version identified in the AWS SDK documentation.

Install and package the latest AWS SDK version

Note: Deployment packages must be compatible with the Lambda environment. For the following steps, it's a best practice to use the matching operating system for your runtime as specified in AWS Lambda Runtimes.

In a local, Lambda-compatible development environment, do the following:

1.    Create a working directory:

mkdir -p aws-sdk-layer/nodejs

2.    Change to the working directory:

cd aws-sdk-layer/nodejs

3.    Install the latest version of the AWS SDK:

npm install aws-sdk

4.    Create a .zip file to upload to the layer.

zip -r ../package.zip ../

5.    (Optional) Verify the version of the AWS SDK that you installed:

cat package-lock.json

The output looks like the following:

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

Create a layer and add it to your function (AWS CLI)

Note: You can also use the Lambda console to create a layer and add it to your function. See the following section for console instructions.

1.    From the AWS Command Line Interface (AWS CLI), run the Lambda publish-layer-version command:

Note: Replace node_sdk with the name that you want to give the layer. Optionally replace "My layer" with a description for the layer.

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

In the output, note the LayerVersionArn. For more information, see Managing Layers.

2.    Run the Lambda update-function-configuration command:

Note: Replace my-function with your function's name. Replace arn:aws:lambda:us-east-2:123456789012:layer:node_sdk:1 with the LayerVersionArn from the previous step.

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

For more information, see Configuring a Function to Use Layers.

Create a layer and add it to your function (Lambda console)

1.    Open the Layers page of the Lambda console.

2.    On the Layers page, choose Create layer.

3.    On the Create layer page, do the following:
For Name, enter a name for the layer.
(Optional) For Description - optional, enter a description for the layer.
Choose Upload a .zip file.
Choose Upload, and then choose your deployment package .zip file.
For Compatible runtimes - optional, choose one or more compatible runtimes. For the Node.js 10x example, choose Node.js 10x.
(Optional) For License - optional, enter software license information for the layer.
Choose Create.

4.    On the Functions page of the Lambda console, choose your function.

5.    Under Designer, under your function's name, choose Layers.

6.    Under Layers, choose Add a layer.

7.    On the Add layer to function page, under Layer selection, choose Select from list of runtime compatible layers.

8.    Under Compatible layers, do the following:
For Name, choose the layer that you created.
For Version, choose 1.

9.    Choose Add.

10.    Choose Save.

(Optional) Check the AWS SDK version

Execute your Lambda function again. Your function returns the latest version of the AWS SDK that you installed earlier using npm.

You can verify the latest version in the CHANGELOG file of the AWS SDK that you're using. For example, see Changelog for AWS SDK for JavaScript on GitHub.


Did this article help you?

Anything we could improve?


Need more help?