我正在尝试设置一个 API 网关,以便通过 lambda 简单地发送和发送电子邮件,并且是 ses。
var aws = require('aws-sdk');
var ses = new aws.SES({
region: 'us-east-1'
});
const querystring = require('querystring');
const inspect = require('util').inspect;
exports.handler = function(event, context) {
var output = querystring.parse(event);
console.log("Incoming: ", output["email"]);
email=output["email"]
console.log("Incoming: ", email);
var eParams = {
Destination: {
ToAddresses: [email]
},
Message: {
Body: {
Text: {
Data: "some trial text"
}
},
Subject: {
Data: "Email Subject!!!"
}
},
Source: "info@ayyy.com"
};
console.log('===SENDING EMAIL===');
var email = ses.sendEmail(eParams, function(err, data){
if(err) console.log(err);
else {
console.log("===EMAIL SENT===");
console.log(data);
console.log("EMAIL CODE END");
console.log('EMAIL: ', email);
context.succeed(event);
}
});
};
Lambda 代码如下所示。如何获取 api 传递的事件数据?
控制台日志如下所示:
14:00:57 开始请求 ID:f07a7595-3034-11e8-81c7-d3dcc4528819 版本:$LATEST 开始请求 ID:f07a7595-3034-11e8-81c7-d3dcc4528819 版本:$LATEST 14:00:57 2018-03-25T14:00:57.195Z f07a7595-3034-11e8-81c7-d3dcc4528819 传入:未定义 2018-03-25T14:00:57.195Z f07a7595-3034-11e8-81c7-d3dcc4528819 传入:未定义 14:00:57 2018-03-25T14:00:57.232Z f07a7595-3034-11e8-81c7-d3dcc4528819 传入:未定义 2018-03-25T14:00:57.232Z f07a7595-3034-11e8-81c7-d3dcc4528819 传入:未定义 14:00:57 2018-03-25T14:00:57.232Z f07a7595-3034-11e8-81c7-d3dcc4528819 ===发送电子邮件=== 2018-03-25T14:00:57.232Z f07a7595-3034-11e8-81c7-d3dcc4528819 ===发送电子邮件=== 14:00:58 2018-03-25T14:00:58.095Z f07a7595-3034-11e8-81c7-d3dcc4528819 {[ValidationError:检测到1个验证错误:“目标”处的值null无法满足约束:成员不得为空]消息:'1验证检测到错误:“目的地”处的值 null 未能满足约束:成员不得为 null',代码:“ValidationError”,时间:2018 年 3 月 25 日星期日 14:00:58 GMT+0000 (UTC),r 2018-03-25T14:00:58.095Z f07a7595-3034-11e8-81c7-d3dcc4528819 { [ValidationError:检测到1个验证错误:“目标”处的值 null 未能满足约束:成员不得为 null] message: '检测到 1 个验证错误:“目的地”处的值为 null 未能满足约束:成员不得为 null', 代码:'验证错误', 时间: 2018年3月25日星期日 14:00:58 GMT+0000 (UTC), 请求 ID: 'f14b32f1-3034-11e8-897b-633b17ff4491', 状态代码:400, 可重试:假, 重试延迟:54.40075590740889} 14:00:58 结束请求 ID:f07a7595-3034-11e8-81c7-d3dcc4528819 结束请求 ID:f07a7595-3034-11e8-81c7-d3dcc4528819 14:00:58 报告请求 ID:f07a7595-3034-11e8-81c7-d3dcc4528819 持续时间:1498.93 毫秒 计费持续时间:1500 毫秒 内存大小:128 MB 最大使用内存:37 MB
最佳答案
记录事件并查看其结构。由于电子邮件未定义,您的函数从一开始就失败了。在 API Gateway 中,如果您定义了 LAMBDA PROXY INTEGRATION
,您的事件正文的结构将如下所示:
{
"message": "Good day, John of Seattle. Happy Friday!",
"input": {
"resource": "/{proxy+}",
"path": "/Seattle",
"httpMethod": "POST",
"headers": {
"day": "Friday"
},
"queryStringParameters": {
"time": "morning"
},
"pathParameters": {
"proxy": "Seattle"
},
"stageVariables": null,
"requestContext": {
"path": "/{proxy+}",
"accountId": "123456789012",
"resourceId": "nl9h80",
"stage": "test-invoke-stage",
"requestId": "test-invoke-request",
...
},
"resourcePath": "/{proxy+}",
"httpMethod": "POST",
"apiId": "r275xc9bmd"
},
"body": "{ \"callerName\": \"John\" }",
"isBase64Encoded": false
}
}
您想要读取 queryStringParameters 对象。
关于node.js - 为什么我不能将 api 参数传递给 lambda 函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49476762/