AWS Lambda Error: Validation Exception Value [] at 'names' failed to satisfy constraint: Member must have a length greater than or equal to 1

Viewed 2476

I have a lambda function which is triggered by an AWS SNS Topic. The function takes the information passed in from the SNS event and send's an email to a user using Nodemailer.

When I run my lambda function locally it works correctly. But once it's deployed I get the following error in my CloudWatch logs:

ValidationException: 1 validation error detected: Value '[]' at 'names' failed to satisfy constraint: Member must have length greater than or equal to 1.
    at Request.extractError (/var/task/node_modules/aws-sdk/lib/protocol/json.js:52:27)
    at Request.callListeners (/var/task/node_modules/aws-sdk/lib/sequential_executor.js:106:20)
    at Request.emit (/var/task/node_modules/aws-sdk/lib/sequential_executor.js:78:10)
    at Request.emit (/var/task/node_modules/aws-sdk/lib/request.js:688:14)
    at Request.transition (/var/task/node_modules/aws-sdk/lib/request.js:22:10)
    at AcceptorStateMachine.runTo (/var/task/node_modules/aws-sdk/lib/state_machine.js:14:12)
    at /var/task/node_modules/aws-sdk/lib/state_machine.js:26:10
    at Request.<anonymous> (/var/task/node_modules/aws-sdk/lib/request.js:38:9)
    at Request.<anonymous> (/var/task/node_modules/aws-sdk/lib/request.js:690:12)
    at Request.callListeners (/var/task/node_modules/aws-sdk/lib/sequential_executor.js:116:18) {
  code: 'ValidationException',
  time: 2021-10-01T18:02:30.538Z,
  requestId: '023d6346-a49a-4dc6-a07f-92bd578a3601',
  statusCode: 400,
  retryable: false,
  retryDelay: 94.04142545650443
} 

This is the code for my lambda function:

const pug = require('pug');
const {newTransporter} = require('./handlers/emailTransporter');

exports.handler = async (event, context) => {
    const message = event.Records[0].Sns.Message;

    const mailAddressObj = {
        'userEmail' : message.mail.commonHeaders.replyTo,
        'clientEmail' : message.mail.commonHeaders.to[0],
    }
    const {userEmail, clientEmail} = mailAddressObj;
    const html = pug.compileFile('./views/bounceNotificationEmail.pug');
    const mailOptions = {
        from: 'sendingEmail@email.com',
        to: 'clientEmail,
        subject: `Subject Header`,
        html: html({
            client_email: clientEmail
        }),
      };

    try {
        let transporter = await newTransporter();
        let info = await transporter.sendMail(mailOptions);
        console.log("Message sent: %s", info.messageId);
        return info;
    }
    catch (e) {
        console.log(e, 'error')
    }

    return console.log(mailAddressObj);
};

1 Answers

for starters in your example you don't close the quote after the "to" parameter :P

on a more serious note however, I reckon that when you run this locally, you wrote an JSON event that triggers your lambda manually and it doesn't have the same shape as the real life event produced by SNS. In your specific case, message that you initialize in the first line of your function is in fact a string, yet you use it as an object.

What you should have done is

const message = JSON.parse(event.Records[0].Sns.Message);
Related