我在 node.js 中编写了一个 AWS lambda 函数来发送电子邮件,该函数在 Amazon Connect 联系流中被调用。在错误分支中,它会播放提示“lambda 函数失败”。我验证了 IAM 角色有权使用 SES 发送电子邮件,发件人/收件人电子邮件在 SES 中进行了验证,并且 lambda 函数也有权使用 Amazon Connect。
电子邮件确实已发送出去,但奇怪的是我仍然听到提示“lambda 函数失败”。这是代码:
"use strict";
const aws = require('aws-sdk');
const sender = "Sender Name <sender@email.com>";
const recipient = "recipient@email.com";
const subject = "ALERT: no agents are logged in";
const body_text = "There are no agents logged in";
const body_html =
`<html>
<head></head>
<body>
<h1>ALERT</h1>
<p>Ther are no agents logged in to take calls in the queue.</p>
</body>
</html>`;
const charset = "UTF-8";
let params = {
Source: sender,
Destination: {
ToAddresses: [
recipient
],
},
Message: {
Subject: {
Data: subject,
Charset: charset
},
Body: {
Text: {
Data: body_text,
Charset: charset
},
Html: {
Data: body_html,
Charset: charset
}
}
},
};
const ses = new aws.SES({apiVersion: '2010-12-01'});
exports.handler = function(event, context, callback) {
ses.sendEmail(params, function(err, data) {
if(err) {
console.log("fail");
callback(err, err.message);
}
else {
console.log("success");
callback(null);
}
});
};
我检查了 cloudwatch 日志,没有看到任何错误:
00:17:24
START RequestId: 17f1e239-990e-11e8-96bb-a1980f44db91 Version: $LATEST
00:17:24
2018-08-06T00:17:24.723Z 17f1e239-990e-11e8-96bb-a1980f44db91 success
00:17:24
END RequestId: 17f1e239-990e-11e8-96bb-a1980f44db91
00:17:24
REPORT RequestId: 17f1e239-990e-11e8-96bb-a1980f44db91 Duration: 226.51 ms Billed Duration: 300 ms Memory Size: 128 MB Max Memory Used: 32 MB
我该如何解决这个问题?
编辑:
我启用了联系流日志。在 CloudWatch 中,我注意到了这一点:
{
"Parameters": { "FunctionArn": "arn:aws:lambda:us-west-2:769182588423:function:noAgentEmail", "TimeLimit": "8000" },
"Timestamp": "2018-08-06T06:36:31.786Z",
"ContactFlowModuleType": "InvokeExternalResource",
"Results": "The Lambda Function Returned An Error.",
"ExternalResults": { "forceClose": "false" },
"ContactId": "458027b0-d895-439e-bc06-114500dce64a",
"ContactFlowId": "arn:aws:connect:us-west-2:769182588423:instance/1e2ddedd-8335-42fe-89de-1e986fc016ef/contact-flow/2329af39-682c-4dc8-b3a2-5e7fe64de5d2"
}
令人困惑的是它表明 lambda 函数返回了一些东西:
"ExternalResults": { "forceClose": "false" }
但这显然不是给定代码的情况。这是怎么回事?