AWS SQS和LAMBDA触发器 - 多重ValidationErrors

发布于 2025-01-23 10:26:17 字数 10480 浏览 0 评论 0原文

大家好,我正在努力弄清楚为什么我不断获得多validationErrors缺少参数。

希望有人能弄清楚这一点,今天一直让我发疯。

错误:

> {
>     "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: '[email protected]'
          },
          '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
        })
      }
    }

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

顾冷 2025-01-30 10:26:17

event输入您的lambda接收到的是 sqs记录的数组。您需要迭代记录和JSON.PARSE串制的主体以提取输入。

您没有从lambda 事件中正确提取输入数据。 event [“ emailbody”](以及其他类似)的值是undefined,这又导致sendemail验证。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文