node.js – Unable to execute AWS Lambda from NestJS

I’m attempting to invoke an AWS Lambda from my NestJS service using aws-sdk lib, but I encounter the following error each time I make the invocation. I have granted all necessary permissions and policies to enable access to the Lambda from my service.

my-service.ts

import * as AWS from 'aws-sdk';

AWS.config.update({
  region: 'my-region',
  accessKeyId: 'my-access-key-id',
  secretAccessKey: 'my-secret-access-key',
});

async invokeLambdaFunction(): Promise<string> {
  const params = {
    FunctionName: 'my-lambda-function-name',
    Payload: JSON.stringify({
      phone_number: '+00000000',
    }),
  };

  const response = await this.lambda.invoke(params).promise();
  console.log("response:", response);
  return 'test';
}

response:

{
  StatusCode: 200,
  FunctionError: 'Unhandled',
  ExecutedVersion: '$LATEST',
  Payload: `{
    "errorType":"TypeError",
    "errorMessage":"Cannot read property 'session' of undefined",
    "trace":["TypeError: Cannot read property 'session' of undefined","    at Runtime.exports.handler (/var/task/src/index.js:18:22)","    at processTicksAndRejections (internal/process/task_queues.js:95:5)"]
  }`
}

If you could provide an alternative and more efficient approach for this task, it would be greatly appreciated.

Read more here: Source link