Lambda execution failed: Cannot find package 'aws-sdk' imported from /var/task/index.mjs

0

I'm coding a Lambda function to manage the storage of an object within a DynamoDB table.

According to AWS Lambda development guide, the runtime environment provides the dependency of JavaScript SDK.

nodejs-package.html

The Node.js runtime includes a number of common libraries, as well as a version of the AWS SDK for JavaScript. 
The nodejs16.x Lambda runtime includes version 2.x of the SDK. 
Runtime versions nodejs18.x and later include version 3 of the SDK.

Here's the SDK version I installed in my local development environment.

  "dependencies": {
    "aws-sdk": "^2.1581.0",
  }

Here's the code that provisions the Lambda function based on Node.js version 16.

    const userSignUpFn = new lambda.Function(this, "userSignUpFn", {
      functionName: "user-sign-up",
      runtime: lambda.Runtime.NODEJS_16_X,
      code: lambda.Code.fromAsset(
        path.join(__dirname, "../src/lambda/user-sign-up")
      ),
      handler: "index.handler",
      architecture: lambda.Architecture.X86_64,
    });

Lambda handler.

import * as SDK from "aws-sdk";

exports.handler = async (event) => {
  const db = new SDK.DynamoDB.DocumentClient();
};

As I understand, there shouldn't be a need to package this dependency as a .zip file or a layer. However, I'm receiving the following error when my function is invoked.

Endpoint response body before transformations: 
{"errorType":"Error", "errorMessage":"Cannot find package 'aws-sdk' imported from /var/task/index.mjs\nDid you mean to import aws-sdk/lib/aws.js?",
"trace":["Error [ERR_MODULE_NOT_FOUND]: Cannot find package 'aws-sdk' imported from /var/task/index.mjs",
"Did you mean to import aws-sdk/lib/aws.js?",
	"at new NodeError (node:internal/errors:387:5)",
	"at packageResolve (node:internal/modules/esm/resolve:852:9)",
	"at moduleResolve (node:internal/modules/esm/resolve:901:20)",
	"at defaultResolve (node:internal/modules/esm/resolve:1115:11)",
	"at nextResolve (node:internal/modules/esm/loader:163:28)",
	"at ESMLoader.resolve (node:internal/modules/esm/loader:841:30)",
	"at ESMLoader.getModuleJob (node:internal/modules/esm/loader:424:18)",
	"at ModuleWrap.<anonymous> (node:internal/modules/esm/module_job:76:40)",
	"at link (node:internal/modules/esm/module_job:75:36)"]}
Lambda execution failed with status 200 due to customer function error: Cannot find package 'aws-sdk' imported from /var/task/index.mjs

Additionally I found a similar error mentioned in this post.

a-nodejs-18-based-lambda-importing-from-aws-sdk-v3-fails-to-find-the-client-ssm-package)

Fix is about changing file extension from index.mjs to index.js, it was already tested and it does work, unfortunately it is not an option for me to update the extension

How can I manage to use aws-sdk provided by lambda runtime?

  • Hi,

    I'm sorry to hear that. Would be possible to you attaching the index.js file content?

  • Hi @Mikel Del Tio, I just updated question to include lambda handler :)

gefragt vor 7 Monaten2431 Aufrufe
2 Antworten
1

First of all, please do not use JS V2 and Lambda Node runtime 16, you will be forced to change your entire function in a number of weeks.

As for your error, you are trying to import the package as a module, which it is not. Changing the extension forces it to be treated like a module, and that is why it works. For some strange reason you cannot change the extension, so you must change how you import the package. Use require() rather than import.

https://www.freecodecamp.org/news/how-to-use-the-javascript-require-function/

profile pictureAWS
EXPERTE
beantwortet vor 7 Monaten
profile picture
EXPERTE
überprüft vor 7 Monaten
-1

Hi,

As other colleagues suggest, I recommend using a more recent version of the Node.js runtime, and thus avoid problems in the short-medium term.

Take a look at the following example for Node.js 20.x Lambda runtime:

import { DynamoDBClient } from "@aws-sdk/client-dynamodb";
import { DynamoDBDocumentClient } from "@aws-sdk/lib-dynamodb";

const client = new DynamoDBClient({});
const docClient = DynamoDBDocumentClient.from(client);

export const handler = async (event, context) => {
  return {
    statusCode: 200,
    body: JSON.stringify('Hello from Lambda!'),
  };
};

You can find more examples on the AWS documentation.

profile picture
EXPERTE
beantwortet vor 7 Monaten

Du bist nicht angemeldet. Anmelden um eine Antwort zu veröffentlichen.

Eine gute Antwort beantwortet die Frage klar, gibt konstruktives Feedback und fördert die berufliche Weiterentwicklung des Fragenstellers.

Richtlinien für die Beantwortung von Fragen