Aws SQS 和 Lambda 触发器 - MultipleValidationErrors

Aws SQS and Lambda Trigger - MultipleValidationErrors

大家好,我正在努力弄清楚为什么我总是因为缺少参数而得到 MultipleValidationErrors

希望有人能解决这个问题,今天让我发疯了。

错误:

> {
>     "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 发送函数:

     AwsSqsService.send('Request Password Reset', 'email', {
          'emailTo': {
            DataType: 'String',
            StringValue: email
          },
          'emailFrom': {
            DataType: 'String',
            StringValue: 'chris@example.co.uk'
          },
          'emailBody': {
            DataType: 'String',
            StringValue: '<html><p>Testing Request</p></html>'
          },
          'emailSubject': {
            DataType: 'String',
            StringValue: "Testing Emails"
          }
    });

SQS 代码:

    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
        })
      }
    }

拉姆达:

    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()
    };

您的 Lambda 收到的 event 输入是 array of SQS records。您需要迭代记录和 JSON.parse 字符串化主体以提取您的输入。

您没有正确地从 Lambda event 中提取输入数据。 event["emailBody"](以及其他类似的值)的值为 undefined,这反过来导致 sendEmail 调用验证失败。