Lambda function fails because the aws-sdk package is missing
Error: Cannot find module 'aws-sdk'Short answer
Current Node.js Lambda runtimes ship version 3 of the AWS SDK (@aws-sdk/client-* packages) and no longer provide version 2, the package called aws-sdk. Either move the code to the v3 client you need, or add aws-sdk to the deployment package yourself. Always ship the SDK clients you use inside your own package.
Fix it fast
1Find out which cause you have
Print which AWS SDK the runtime really contains by returning the version of one v3 client from a test invocation. If that works while aws-sdk fails, the code still uses version 2.
// index.mjs import packageJson from '@aws-sdk/client-s3/package.json' with { type: 'json' }; export const handler = async () => ({ version: packageJson.version });2Match what you see, then jump to its fix
- 1
Cannot find module 'aws-sdk' on a function that worked on an older runtime, with require('aws-sdk') in the sourceThe code still imports SDK version 2 (aws-sdk)Fix - 2
The code cannot be changed now, and node_modules does not contain aws-sdkYou want to keep version 2 and did not package itFix - 3
The package exists in the layer zip, yet the import still failsThe dependency is in a layer, but in the wrong folderFix
- 1
What does the error look like?
Error: Cannot find module 'aws-sdk'
Cause 1 of 3
The code still imports SDK version 2 (aws-sdk)
1Check
Search the source for the v2 import. The Lambda guide states that every supported Node.js runtime includes version 3 of the SDK, and that using version 2 means adding it to your .zip package.
grep -rn "aws-sdk'\|aws-sdk\"" --include=*.js --include=*.mjs --exclude-dir=node_modules .2Fix
Replace the v2 import with the v3 client for the service and call it with a command object. Install the client so it travels with your package.
npm install @aws-sdk/client-s3
// before: const AWS = require('aws-sdk'); const s3 = new AWS.S3();
import { S3Client, PutObjectCommand } from '@aws-sdk/client-s3';
const s3 = new S3Client();
await s3.send(new PutObjectCommand({ Bucket: process.env.BUCKET, Key: 'a.txt', Body: 'hi' }));3Verify
Deploy again and run a test event. The module error should be gone and the S3 call should succeed.
aws lambda invoke --function-name my-function out.json && cat out.jsonConfirmed by docs.aws.amazon.com
Cause 2 of 3
You want to keep version 2 and did not package it
1Check
List what is inside your deployment package. If node_modules/aws-sdk is not in the zip, Lambda has nothing to load.
unzip -l my_deployment_package.zip | grep 'node_modules/aws-sdk/package.json'2Fix
Install aws-sdk into the function folder and zip the whole folder, with the handler file at the root of the zip. This is a stop-gap: plan the move to version 3.
cd my_function
npm install aws-sdk
zip -r my_deployment_package.zip .3Verify
Upload the zip and invoke the function again.
aws lambda update-function-code --function-name my-function --zip-file fileb://my_deployment_package.zip
aws lambda invoke --function-name my-function out.jsonConfirmed by docs.aws.amazon.com
Cause 3 of 3
The dependency is in a layer, but in the wrong folder
1Check
Lambda mounts a layer under /opt and only searches fixed paths there. For Node.js the layer zip must hold the packages under nodejs/node_modules.
unzip -l layer.zip | head -202Fix
Rebuild the layer so the packages sit in nodejs/node_modules, then publish a new layer version and attach it.
mkdir -p layer/nodejs && cd layer/nodejs
npm install aws-sdk
cd .. && zip -r ../layer.zip nodejs3Verify
Attach the new layer version to the function and invoke it.
aws lambda invoke --function-name my-function out.jsonConfirmed by docs.aws.amazon.com
How do I stop it coming back?
- Write new code against the v3 clients and install each client you use in package.json, as the Lambda guide recommends.
- Bundle the handler with a tool such as esbuild, so the dependencies the function needs ship in the output.
- Read the runtime-included SDK page before moving a function to a newer Node.js runtime.
- Keep the zip layout in a build script so the handler file always sits at the root.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- Dispatch ProA real-time logistics platform: Flutter apps, Spring Boot services and a Kafka event pipeline on AWS.
- WorkforceA field app, an admin console and a self-hosted Node.js server for push and reconciliation.
Related fixes
Back end
API Gateway gives up on a slow Lambda or Node.js backend
$context.integrationErrorMessage in the access log shows the integration error
API Gateway stops waiting for the backend at its integration timeout: 29 seconds by default on REST APIs, a fixed 30 seconds on HTTP APIs.
4 causes, each with a fixBack end
Application Load Balancer returns 502 for a Node.js target
HTTPCode_ELB_502_Count
An ALB returns 502 when the target gives it something it cannot use: a refused or reset connection, a connection closed mid-request, a bad response, or a header over 32 K.
4 causes, each with a fixBack end
cdk synth says --app is required
--app is required either in command-line, in cdk.json or in ~/.cdk.json
The CDK CLI needs a command that runs your app and finds it in three places: the --app option, cdk.json in the project folder, or cdk.json in your home folder.
3 causes, each with a fixBack end
The AWS SDK in Node.js cannot find any credentials
Missing credentials in config, if using AWS_CONFIG_FILE, set AWS_SDK_LOAD_CONFIG=1
The AWS SDK walks a fixed credential chain: code, environment variables, the shared files in ~/.aws, then role sources such as ECS or EC2.
3 causes, each with a fixBack end
AWS SDK for JavaScript v3 cannot find credentials, even for DynamoDB Local
A DynamoDB client pointed at http://localhost:8000 fails with the same message
The SDK walked its default credential provider chain and found nothing.
3 causes, each with a fixBack end
AWS SDK says the security token included in the request is invalid
The security token included in the request is invalid
The call reached AWS but the credentials it carried were refused.
3 causes, each with a fix