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 Answer
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
EXPERT
answered 10 months ago
  • 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!""}

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions