Lambda AWS不调用节点mysqlcallback

我试图通过AWS的简单通知服务vi和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下本地运行代码版本,连接callback会触发。 它只是不在Lambda下开火。

由于node.js的asynchronous性质,在执行所有函数之前,您的代码可能会由于context.succeed()而退出。

看到:

  • 如果调用方返回的时间太早,则不会执行asynchronousAWS Lambda
  • 为什么这个HTTP请求在AWS Lambda上不起作用?