Lambda deployment error

0

Whenever I use curl to invoke my lambda function locally, I encounter the following issue. Even though I have aws-sdk imported. My lambda code : index.js

curl -X POST -H "Content-Type: application/json" -d '{"input_text": "Example text", "input_file_path": "s3://fullstackjun/identity.c"}' https://v8ysb49d0b.execute-api.us-west-1.amazonaws.com/dev

Error: {"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:399:5)"," at packageResolve (node:internal/modules/esm/resolve:894:9)"," at moduleResolve (node:internal/modules/esm/resolve:987:20)"," at moduleResolveWithNodePath (node:internal/modules/esm/resolve:938:12)"," at defaultResolve (node:internal/modules/esm/resolve:1202:79)"," at nextResolve (node:internal/modules/esm/loader:163:28)"," at ESMLoader.resolve (node:internal/modules/esm/loader:838:30)"," at ESMLoader.getModuleJob (node:internal/modules/esm/loader:424:18)"," at ModuleWrap.<anonymous> (node:internal/modules/esm/module_job:77:40)"," at link (node:internal/modules/esm/module_job:76:36)"]}%

已提问 1 年前747 查看次数
1 回答
0

Thanks for posting your question.

Please take a look at this Stack Overflow Thread, I was able to resolve the similar issue by following the suggestions listed here.

Adding the snapshot for conclusion of the discussion happened in that thread here as well.

Enter image description here

Let me know how it goes, happy to help further if it doesn't get resolved.

profile pictureAWS
专家
已回答 1 年前
  • I created a new lambda function using node 18.x, however I encounter another import issue. The lambda code is as follows: import { DynamoDBClient, PutItemCommand } from "@aws-sdk/client-dynamodb"; import { nanoid } from "nanoid";

    const dynamodbClient = new DynamoDBClient({ region: "us-west-1" });

    export const handler = async (event) => { try { // Extract input data from request payload const { input_text, input_file_path } = JSON.parse(event.body);

    // Generate a unique ID for the item
    const id = nanoid(22);
    
    // Define the DynamoDB item
    const params = {
      TableName: "Filetable",
      Item: {
        id: { S: id },
        input_text: { S: input_text },
        input_file_path: { S: input_file_path },
      },
    };
    
    // Save the item to DynamoDB
    const putItemCommand = new PutItemCommand(params);
    await dynamodbClient.send(putItemCommand);
    
    // Return a successful response
    const response = {
      statusCode: 200,
      body: JSON.stringify("Data saved to DynamoDB"),
    };
    
    return response;
    

    } catch (error) { // Return an error response if something goes wrong console.error(error); const response = { statusCode: 500, body: JSON.stringify("An error occurred"), };

    return response;
    

    } };

  • Can you first create a sample nodejs function with just "print hello" and then invoke it though curl. Can you please comment here with exact error message, I'll share how you should package the lambda function zip with it's required modules shortly.

  • Thanks for the reply. The following "hello world" message is executed properly when I invoke the lambda function via curl. However if invoke a function that requires aws-sdk or nanoid, I get an error. curl -X POST -d '{"input_text": "Hello World"}' https://v8ysb49d0b.execute-api.us-west-1.amazonaws.com/dev {"statusCode":200,"body":""Hello from Lambda!""}

您未登录。 登录 发布回答。

一个好的回答可以清楚地解答问题和提供建设性反馈,并能促进提问者的职业发展。

回答问题的准则