Home > other >  Aws SQS and Lambda Trigger - MultipleValidationErrors
Aws SQS and Lambda Trigger - MultipleValidationErrors

Time:04-25

Hello guys I'm struggling to figure out why I keep getting MultipleValidationErrors for missing paramaters.

Hopefully someone can figure this out, it's been driving me crazy today.

Error:

> {
>     "errorType": "MultipleValidationErrors",
>     "errorMessage": "There were 3 validation errors:\n* MissingRequiredParameter: Missing required key 'Source' in params\n*
> MissingRequiredParameter: Missing required key 'Data' in
> params.Message.Body.Html\n* MissingRequiredParameter: Missing required
> key 'Data' in params.Message.Subject",
>     "code": "MultipleValidationErrors",
>     "message": "There were 3 validation errors:\n* MissingRequiredParameter: Missing required key 'Source' in params\n*
> MissingRequiredParameter: Missing required key 'Data' in
> params.Message.Body.Html\n* MissingRequiredParameter: Missing required
> key 'Data' in params.Message.Subject",
>     "errors": [
>         {
>             "errorType": "MissingRequiredParameter",
>             "errorMessage": "Missing required key 'Source' in params",
>             "code": "MissingRequiredParameter",
>             "message": "Missing required key 'Source' in params",
>             "time": "2022-04-22T14:31:01.177Z",
>             "stack": [
>                 "MissingRequiredParameter: Missing required key 'Source' in params",
>                 "    at ParamValidator.fail (/var/runtime/node_modules/aws-sdk/lib/param_validator.js:50:37)",
>                 "    at ParamValidator.validateStructure (/var/runtime/node_modules/aws-sdk/lib/param_validator.js:61:14)",
>                 "    at ParamValidator.validateMember (/var/runtime/node_modules/aws-sdk/lib/param_validator.js:88:21)",
>                 "    at ParamValidator.validate (/var/runtime/node_modules/aws-sdk/lib/param_validator.js:34:10)",
>                 "    at Request.VALIDATE_PARAMETERS (/var/runtime/node_modules/aws-sdk/lib/event_listeners.js:132:42)",
>                 "    at Request.callListeners (/var/runtime/node_modules/aws-sdk/lib/sequential_executor.js:106:20)",
>                 "    at callNextListener (/var/runtime/node_modules/aws-sdk/lib/sequential_executor.js:96:12)",
>                 "    at /var/runtime/node_modules/aws-sdk/lib/event_listeners.js:86:9",
>                 "    at finish (/var/runtime/node_modules/aws-sdk/lib/config.js:396:7)",
>                 "    at /var/runtime/node_modules/aws-sdk/lib/config.js:414:9"
>             ]
>         },
>         {
>             "errorType": "MissingRequiredParameter",
>             "errorMessage": "Missing required key 'Data' in params.Message.Body.Html",
>             "code": "MissingRequiredParameter",
>             "message": "Missing required key 'Data' in params.Message.Body.Html",
>             "time": "2022-04-22T14:31:01.177Z",
>             "stack": [
>                 "MissingRequiredParameter: Missing required key 'Data' in params.Message.Body.Html",
>                 "    at ParamValidator.fail (/var/runtime/node_modules/aws-sdk/lib/param_validator.js:50:37)",
>                 "    at ParamValidator.validateStructure (/var/runtime/node_modules/aws-sdk/lib/param_validator.js:61:14)",
>                 "    at ParamValidator.validateMember (/var/runtime/node_modules/aws-sdk/lib/param_validator.js:88:21)",
>                 "    at ParamValidator.validateStructure (/var/runtime/node_modules/aws-sdk/lib/param_validator.js:75:14)",
>                 "    at ParamValidator.validateMember (/var/runtime/node_modules/aws-sdk/lib/param_validator.js:88:21)",
>                 "    at ParamValidator.validateStructure (/var/runtime/node_modules/aws-sdk/lib/param_validator.js:75:14)",
>                 "    at ParamValidator.validateMember (/var/runtime/node_modules/aws-sdk/lib/param_validator.js:88:21)",
>                 "    at ParamValidator.validateStructure (/var/runtime/node_modules/aws-sdk/lib/param_validator.js:75:14)",
>                 "    at ParamValidator.validateMember (/var/runtime/node_modules/aws-sdk/lib/param_validator.js:88:21)",
>                 "    at ParamValidator.validate (/var/runtime/node_modules/aws-sdk/lib/param_validator.js:34:10)"
>             ]
>         },
>         {
>             "errorType": "MissingRequiredParameter",
>             "errorMessage": "Missing required key 'Data' in params.Message.Subject",
>             "code": "MissingRequiredParameter",
>             "message": "Missing required key 'Data' in params.Message.Subject",
>             "time": "2022-04-22T14:31:01.177Z",
>             "stack": [
>                 "MissingRequiredParameter: Missing required key 'Data' in params.Message.Subject",
>                 "    at ParamValidator.fail (/var/runtime/node_modules/aws-sdk/lib/param_validator.js:50:37)",
>                 "    at ParamValidator.validateStructure (/var/runtime/node_modules/aws-sdk/lib/param_validator.js:61:14)",
>                 "    at ParamValidator.validateMember (/var/runtime/node_modules/aws-sdk/lib/param_validator.js:88:21)",
>                 "    at ParamValidator.validateStructure (/var/runtime/node_modules/aws-sdk/lib/param_validator.js:75:14)",
>                 "    at ParamValidator.validateMember (/var/runtime/node_modules/aws-sdk/lib/param_validator.js:88:21)",
>                 "    at ParamValidator.validateStructure (/var/runtime/node_modules/aws-sdk/lib/param_validator.js:75:14)",
>                 "    at ParamValidator.validateMember (/var/runtime/node_modules/aws-sdk/lib/param_validator.js:88:21)",
>                 "    at ParamValidator.validate (/var/runtime/node_modules/aws-sdk/lib/param_validator.js:34:10)",
>                 "    at Request.VALIDATE_PARAMETERS (/var/runtime/node_modules/aws-sdk/lib/event_listeners.js:132:42)",
>                 "    at Request.callListeners (/var/runtime/node_modules/aws-sdk/lib/sequential_executor.js:106:20)"
>             ]
>         }
>     ],
>     "time": "2022-04-22T14:31:01.177Z",
>     "stack": [
>         "MultipleValidationErrors: There were 3 validation errors:",
>         "* MissingRequiredParameter: Missing required key 'Source' in params",
>         "* MissingRequiredParameter: Missing required key 'Data' in params.Message.Body.Html",
>         "* MissingRequiredParameter: Missing required key 'Data' in params.Message.Subject",
>         "    at ParamValidator.validate (/var/runtime/node_modules/aws-sdk/lib/param_validator.js:40:28)",
>         "    at Request.VALIDATE_PARAMETERS (/var/runtime/node_modules/aws-sdk/lib/event_listeners.js:132:42)",
>         "    at Request.callListeners (/var/runtime/node_modules/aws-sdk/lib/sequential_executor.js:106:20)",
>         "    at callNextListener (/var/runtime/node_modules/aws-sdk/lib/sequential_executor.js:96:12)",
>         "    at /var/runtime/node_modules/aws-sdk/lib/event_listeners.js:86:9",
>         "    at finish (/var/runtime/node_modules/aws-sdk/lib/config.js:396:7)",
>         "    at /var/runtime/node_modules/aws-sdk/lib/config.js:414:9",
>         "    at Credentials.get (/var/runtime/node_modules/aws-sdk/lib/credentials.js:127:7)",
>         "    at getAsyncCredentials (/var/runtime/node_modules/aws-sdk/lib/config.js:408:24)",
>         "    at Config.getCredentials (/var/runtime/node_modules/aws-sdk/lib/config.js:428:9)"
>     ] }

SQS send function:

     AwsSqsService.send('Request Password Reset', 'email', {
          'emailTo': {
            DataType: 'String',
            StringValue: email
          },
          'emailFrom': {
            DataType: 'String',
            StringValue: '[email protected]'
          },
          'emailBody': {
            DataType: 'String',
            StringValue: '<html><p>Testing Request</p></html>'
          },
          'emailSubject': {
            DataType: 'String',
            StringValue: "Testing Emails"
          }
    });

SQS Code:

    export default class AwsSqsService {
      static accountId = '###########'
    
      public static send(messageBody: string = '', queue: string = '', messageAttributes: object = {}) {
        if (!messageBody) return false
        if (!queue) return false
    
        let queueToUse = ''
    
        switch (queue) {
          case 'email':
            queueToUse = Env.get('AWS_SQS_EMAILING_QUEUE')
            break
        }
    
        let sqs = new AWS.SQS({
          apiVersion: '2012-11-05',
          accessKeyId: Env.get('AWS_SQS_ACCESS_KEY'),
          secretAccessKey: Env.get('AWS_SQS_SECRET_KEY'),
          region: Env.get('AWS_SQS_REGION'),
        })
    
        let sqsParams = {
          MessageBody: messageBody,
          QueueUrl: `https://sqs.eu-west-2.amazonaws.com/${AwsSqsService.accountId}/${queueToUse}`,
        }
    
        /**
         * Example of Message Attribute
         *
         * recipient: {
         *    email: 'chris@heatable'
         * }
         *
         */
        if (messageAttributes) {
          sqsParams = {
            ...sqsParams,
            ...{
              MessageAttributes: messageAttributes,
            },
          }
        }
    
        sqs.sendMessage(sqsParams, function (err, data) {
          if (err) console.log(err, err.stack) // an error occurred
          else console.log(data) // successful response
        })
      }
    }

Lambda:

    let aws = require("aws-sdk");
    let ses = new aws.SES({ 
        region: 'eu-west-2',
        secretAccessKey: '########################',
        accessKeyId: '#########################'
    });
    
    exports.handler = async function (event) {
      const params = {
        Destination: {
          ToAddresses: [event["emailTo"]],
        },
        Message: {
          Body: {
            Html: {
                Data: event["emailBody"]
            }
          },
    
          Subject: {
              Data: event["emailSubject"],
          }
        },
        Source: event["emailFrom"],
      };
     
      return ses.sendEmail(params).promise()
    };

CodePudding user response:

The event input your Lambda receives is an array of SQS records. You need to iterate the records and JSON.parse the stringified body to extract your inputs.

You are not properly extracting the input data from the Lambda event. The value of event["emailBody"] (and the others like it) is undefined, which in turn is causing the sendEmail call to fail validation.

  • Related