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 :
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)"]}%
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);
} catch (error) { // Return an error response if something goes wrong console.error(error); const response = { statusCode: 500, body: JSON.stringify("An error occurred"), };
} };
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!""}