PHPisMyFavLol
PHPisMyFavLol

Reputation: 176

Lambda function cant load AWS sdk after switching to 18.x

I upgraded my Lambda function from node12.x to node18.x

The code worked perfectly fine on 12.x, after switching to 18.x, I can no longer include the AWS sdk:

I used to include it by simply typing:

var AWS = require('aws-sdk');

I now get this error

"Error: Cannot find module 'aws-sdk'

I think if I can just get the AWS sdk to load properly the function should be fine, any ideas?

Thanks

Upvotes: 14

Views: 28459

Answers (3)

MadMurf
MadMurf

Reputation: 2313

With folks getting notification bumps that Node16 is out of support on AWS Lamdba from June 12 worth adding a bump here that there are automated codemod scripts that might help you out here to upgrade your AWS sdk version including making them more performant.

Part of the issue with upgrading from 16 or lower to 18/20 is that aws-sdk has upgraded to v3

Up until Node.js 16, Lambda’s Node.js runtimes included the AWS SDK for JavaScript version 2. This has since been superseded by the AWS SDK for JavaScript version 3, which was released in December 2020. Starting with Node.js 18, and continuing with Node.js 20, the Lambda Node.js runtimes have upgraded the version of the AWS SDK for JavaScript included in the runtime from v2 to v3. Customers upgrading from Node.js 16 or earlier runtimes who are using the included AWS SDK for JavaScript v2 should upgrade their code to use the v3 SDK.

REF: https://aws.amazon.com/blogs/compute/node-js-20-x-runtime-now-available-in-aws-lambda/

AWS also provides automated migrate/codemod scripts to assist the upgrade detailed in https://docs.aws.amazon.com/sdk-for-javascript/v3/developer-guide/migrating.html#migrating-to-v3

for example you can run the aws-sdk v2-to-v3 transform on example.ts as follows.

$ npx aws-sdk-js-codemod -t v2-to-v3 example.ts

The result of the migrate should also reduce the overhead required to run your application, from the migrate link above

The aws-sdk package adds about 40 MB to your application. Replacing var AWS = require("aws-sdk") with import {DynamoDB} from "@aws-sdk/client-dynamodb" reduces that overhead to about 3 MB. Restricting the import to just the DynamoDB client and ListTablesCommand command reduces the overhead to less than 100 KB.

Upvotes: 1

Andrew Csontos
Andrew Csontos

Reputation: 662

Install the aws-sdk in your lambda's package.json file.

npm install aws-sdk@2

Upvotes: 1

fedonev
fedonev

Reputation: 25639

The Node.js 18 Lambda runtime is preloaded with the AWS SDK for JS v3.

The earlier runtimes have the SDK v2.

Of course you can still use the SDK v2 with the Node.js 18 runtime. You just need to package the clients as dependencies with your Lambda code.


The v2 SDK's aws-sdk package (preloaded on the 14.x, 16.x Lambda Node.js runtimes) contains every client. The v3 service clients (preloaded on 18.x) are modularised in separate @aws-sdk/client-[something] packages. Here's a v3 sample import for getting an S3 object:

// v3
import { S3Client, GetObjectCommand } from "@aws-sdk/client-s3"

See the Getting Started guide in the SDK v3 docs.

Upvotes: 20

Related Questions