使用 Node.js 处理程序发送和发送电子邮件时出现 AWS Lambda NetworkingError
AWS Lambda NetworkingError when sending and email with a Node.js handler
我正在尝试从 Lambda Node.js 应用程序发送电子邮件:
邮件-service.js
const ses = new aws.SES({
region: 'eu-west-1'
});
exports.send = (to, subject, text) => {
const deferred = Q.defer();
const eParams = {
Destination: {
ToAddresses: [to]
},
Message: {
Body: {
Text: {
Data: text
}
},
Subject: {
Data: subject
}
},
Source: to
};
console.log("Sending mail %s", JSON.stringify(eParams));
const email = ses.sendEmail(eParams, function(err, data){
if(err){
deferred.reject(err);
}
else {
deferred.resolve(data);
}
});
return deferred.promise;
}
调用此服务时会抛出 NetworkingError:
index.js:
mailService.send(process.env.EMAIL_TO, subject, text)
.then( (response) => {
console.log("SES response: %s", response);
})
.fail( (err) => {
console.log("SES err: %s", err);
});
错误: SNS err: NetworkingError: connect ECONNREFUSED 127.0.0.1:8000
我该如何解决?
请注意,我已经配置了与 lambda 角色关联的 IAM 策略以允许发送电子邮件:
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "xxxxxxxxxx",
"Effect": "Allow",
"Action": [
"ses:SendEmail",
"ses:SendRawEmail"
],
"Resource": [
"*"
]
}
]
}
问题是有一个开发配置文件将 AWS sdk 配置为与本地 DynamoDB 一起工作:
AWS.config.update({
region: "eu-west-2",
endpoint: "http://localhost:8000"
});
我删除了端点密钥,现在可以使用了。
我正在尝试从 Lambda Node.js 应用程序发送电子邮件:
邮件-service.js
const ses = new aws.SES({
region: 'eu-west-1'
});
exports.send = (to, subject, text) => {
const deferred = Q.defer();
const eParams = {
Destination: {
ToAddresses: [to]
},
Message: {
Body: {
Text: {
Data: text
}
},
Subject: {
Data: subject
}
},
Source: to
};
console.log("Sending mail %s", JSON.stringify(eParams));
const email = ses.sendEmail(eParams, function(err, data){
if(err){
deferred.reject(err);
}
else {
deferred.resolve(data);
}
});
return deferred.promise;
}
调用此服务时会抛出 NetworkingError:
index.js:
mailService.send(process.env.EMAIL_TO, subject, text)
.then( (response) => {
console.log("SES response: %s", response);
})
.fail( (err) => {
console.log("SES err: %s", err);
});
错误: SNS err: NetworkingError: connect ECONNREFUSED 127.0.0.1:8000
我该如何解决?
请注意,我已经配置了与 lambda 角色关联的 IAM 策略以允许发送电子邮件:
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "xxxxxxxxxx",
"Effect": "Allow",
"Action": [
"ses:SendEmail",
"ses:SendRawEmail"
],
"Resource": [
"*"
]
}
]
}
问题是有一个开发配置文件将 AWS sdk 配置为与本地 DynamoDB 一起工作:
AWS.config.update({
region: "eu-west-2",
endpoint: "http://localhost:8000"
});
我删除了端点密钥,现在可以使用了。