【发布时间】:2015-10-13 02:44:48
【问题描述】:
我正在尝试通过 AWS 上的 Lambda 通过其简单通知服务处理从亚马逊的简单电子邮件服务发送的退回邮件。
我正在运行以下脚本:
var aws = require('aws-sdk');
var mysql = require('mysql');
Processor = {};
Processor.initializeConnection = function() {
console.log('Connecting to database');
Processor.connection = mysql.createConnection({
host : 'MYHOST',
user : 'MYUSER',
password : 'PASSWORD',
database : 'DATABASE'
});
console.log('Connection configured');
Processor.connection.connect(function(err) {
console.log('****');
console.log(err);
if (err != null) {
console.log('Could not connect to database');
return false;
} else {
console.log('Successfully connected to database');
return true;
}
});
console.log('Should not get here');
};
exports.handler = function(event,context){
console.log('Received event:');
var message = event.Records[0].Sns.Message;
// Get the object from the event and show its content type
if(Processor.initializeConnection()) {
context.fail('Database connection failed');
return;
}
context.succeed(message);
};
我将此脚本作为 index.js 以及包含节点 mysql 模块的 node_modules 全部作为 zip 文件上传。
运行此程序时,我从 Amazon 获得以下输出:
START RequestId: 378b8a8c-30d4-11e5-9db4-9b9537e3f53d
2015-07-23T00:46:13.159Z 378b8a8c-30d4-11e5-9db4-9b9537e3f53d Received event:
2015-07-23T00:46:13.160Z 378b8a8c-30d4-11e5-9db4-9b9537e3f53d Connecting to database
2015-07-23T00:46:14.035Z 378b8a8c-30d4-11e5-9db4-9b9537e3f53d Connection configured
2015-07-23T00:46:14.095Z 378b8a8c-30d4-11e5-9db4-9b9537e3f53d Should not get here
END RequestId: 378b8a8c-30d4-11e5-9db4-9b9537e3f53d
REPORT RequestId: 378b8a8c-30d4-11e5-9db4-9b9537e3f53d Duration: 937.51 ms Billed Duration: 1000 ms Memory Size: 128 MB Max Memory Used: 14 MB
连接后备中的代码均未运行。我希望它报告连接失败,因为我没有使用有效的凭据。
如果我在 nodejs 下本地运行代码版本,连接回调会触发。它只是不会在 Lambda 下触发。
【问题讨论】:
-
您的回调结构不正确。您需要在 initializeConnection() 中传递您的回调并从其中调用它。
标签: mysql node.js amazon-web-services aws-lambda