AWS SQS和LAMBDA触发器 - 多重ValidationErrors
大家好,我正在努力弄清楚为什么我不断获得多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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
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 andJSON.parse
the stringified body to extract your inputs.You are not properly extracting the input data from the Lambda
event
. The value ofevent["emailBody"]
(and the others like it) isundefined
, which in turn is causing thesendEmail
call to fail validation.