我有以下节点代码
'use strict';
var aws = require('aws-sdk');
var async = require('async');
const mysql = require('mysql');
var ses = new aws.SES({
region: 'eu-west-1'
});
module.exports.helloWorld = function(event, context, callback) {
//context.callbackWaitsForEmptyEventLoop = false; //Updated on @dashmug recommendation
console.log("Hit here 1");
let connection = mysql.createConnection({
host: "xxx",
user: "xxx",
password: "xxx",
database: "xxx"
});
connection.connect(); //Updated on @dashmug recommendation
console.log("Hit here 2");
connection.query(`SELECT * FROM User U WHERE id = 1`, function(error, row) {
console.log("Hit here 3");
console.log(row);
let eParams = {
Destination: {
ToAddresses: [row[0].email]
},
Message: {
Body: {
Html: {
Data: "Test"
},
Text: {
Data: "Test"
}
},
Subject: {
Data: "Test Email"
}
},
Source: "team@hello.com"
};
console.log("Hit here 4");
connection.end(); //Updated on @dashmug recommendation
console.log('===SENDING EMAIL===');
var email = ses.sendEmail(eParams, function(err, data) {
if(err) {
console.log(err);
//context.fail(err);
callback(err); //Updated on @dashmug recommendation
} else {
console.log("===EMAIL SENT===");
console.log("EMAIL CODE END");
console.log('EMAIL: ', email);
console.log(data);
callback(null, event); //Updated on @dashmug recommendation
}
});
});
}
注意:我的ses默认限制被删除。我还尝试在结果回调中移动connection.end()。我得到了同样的结果。
此代码总是超时。我的云监视日志
{
"errorMessage": "2018-01-19T11:58:16.794Z cc9e6998-fd0f-11e7-ae0a-xxxx Task timed out after 100.06 seconds"
}
START RequestId: e60d142a-01c2-11e8-99da-fb3cb3a59816 Version: $LATEST
2018-01-25T11:28:43.928Z e60d142a-01c2-11e8-99da-fb3cb3a59816 Hit here 1
2018-01-25T11:28:44.163Z e60d142a-01c2-11e8-99da-fb3cb3a59816 Hit here 2
2018-01-25T11:28:44.207Z e60d142a-01c2-11e8-99da-fb3cb3a59816 Hit here 3
2018-01-25T11:28:44.164Z e60d142a-01c2-11e8-99da-fb3cb3a59816 Hit here 4
2018-01-25T11:28:44.209Z e60d142a-01c2-11e8-99da-fb3cb3a59816 [ RowDataPacket {
id: 1,
name: 'Karthik',
email: 'xxx@xxx.com' } ]
2018-01-25T11:28:44.223Z e60d142a-01c2-11e8-99da-fb3cb3a59816 ===SENDING EMAIL===
END RequestId: e60d142a-01c2-11e8-99da-fb3cb3a59816
REPORT RequestId: e60d142a-01c2-11e8-99da-fb3cb3a59816 Duration: 100038.81 ms Billed Duration: 100000 ms Memory Size: 512 MB Max Memory Used: 54 MB
2018-01-25T11:30:23.965Z e60d142a-01c2-11e8-99da-fb3cb3a59816 Task timed out after 100.04 seconds
我的无服务器配置。
service: test
provider:
name: aws
runtime: nodejs6.10
stage: dev
region: eu-west-1
memorySize: 512
timeout: 100
iamRoleStatements:
- Effect: Allow
Action:
- s3:*
- SNS:Publish
- SES:SendRawEmail
- SES:SendEmail
- lambda:InvokeFunction
Resource: "*"
environment:
NODE_ENV: live
vpc:
securityGroupIds:
- sg-xxx
- sg-xxx
- sg-xxx
- sg-xxx
subnetIds:
- subnet-xxx
- subnet-xxx
- subnet-xxx
functions:
emailEmailFeedback:
handler: handler.helloWorld
events:
- http:
path: user-feedback
method: get
cors: true
注意:vpc配置是我的mysql连接。我只有一个专有网络,我所有的服务都在它里面。
当我取出mysql查询并运行ses测试邮件时,函数运行成功。
另外,当我取出ses并运行mysql时,我成功地得到了结果。
但他们不合作。
我还尝试将ses移到另一个函数,并尝试从mysql查询回调中调用sendmail函数,但没有成功,我得到了相同的超时错误。
lambda.invoke({
FunctionName: 'sendEmail',
Payload: JSON.stringify(rows, null, 2) // pass params
}, function(error, data) {
if (error) {
context.fail({"hello": "world"});
}
if(data.Payload){
context.succeed({"hello": "world"});
}
});
任何帮助都将不胜感激。
1条答案
按热度按时间relj7zay1#
使用
callback()
而不是context.succeed()
或者context.fail()
. 那些上下文方法已被弃用。根据https://docs.aws.amazon.com/lambda/latest/dg/nodejs-prog-model-using-old-runtime.html#transition-到新的nodejs运行时,
node.js runtime v0.10.42不支持运行时v4.3和v6.10支持的lambda函数的回调参数。使用运行时v0.10.42时,可以使用以下上下文对象方法来正确终止lambda函数。context对象支持done()、succeed()和fail()方法,您可以使用这些方法终止lambda函数。为了向后兼容,这些方法也出现在运行时v4.3和v6.10中。
所以,在你的回电中
sendMail()
,它变成。。。还有一件事,你不需要
context.callbackWaitsForEmptyEventLoop = false;
.另外,由于您连接到数据库
inside
在你的处理程序中,你也需要在得到结果后与它断开连接。