Node.js lambda上的无服务器函数始终超时

Node.js lambda上的无服务器函数始终超时,node.js,lambda,Node.js,Lambda,我写了一个简单的查询调用,我的hello处理程序是 'use strict'; const pg = require('pg'); const conn = 'pg://postgres:user:pass@rds_host:5432/database_name'; module.exports.hello = (event, context, callback) => { const client = new pg.Client(conn); client.connect()

我写了一个简单的查询调用,我的hello处理程序是

'use strict';

const pg = require('pg');
const conn = 'pg://postgres:user:pass@rds_host:5432/database_name';

module.exports.hello = (event, context, callback) => {
  const client = new pg.Client(conn);
  client.connect();

  client.query('SELECT column_a FROM table_b', function(err, result) {
    client.end();
    if (err) {
      callback(null, {error: err});
    } else {
      const response = {
        statusCode: 200,
        body: JSON.stringify({
          data: result.rows
        }),
      };

      callback(null, response);
    }
  });

  // Use this code if you don't use the http event with the LAMBDA-PROXY integration
  // callback(null, { message: 'Go Serverless v1.0! Your function executed successfully!', event });
};
我已通过手动调用在本地计算机上执行此脚本

const handler = require('../server/handler');

handler.hello({}, {}, function(err, response) {
  console.log(err, response);
});
当我打电话给你的时候,它就起作用了

$ serverless invoke local -f hello -l
也可以,但调用lambda总是失败

$ SLS_DEBUG=* serverless invoke -f hello -l 


{
    "errorMessage": "2017-04-21T01:11:19.580Z 697e69bc-262f-11e7-8fee-0331cc761e9a Task timed out after 6.00 seconds"
}
--------------------------------------------------------------------
START RequestId: 697e69bc-262f-11e7-8fee-0331cc761e9a Version: $LATEST
END RequestId: 697e69bc-262f-11e7-8fee-0331cc761e9a
REPORT RequestId: 697e69bc-262f-11e7-8fee-0331cc761e9a  Duration: 6000.71 ms    Billed Duration: 6000 ms        Memory Size: 1024 MB    Max Memory Used: 20 MB  
2017-04-21T01:11:19.580Z 697e69bc-262f-11e7-8fee-0331cc761e9a Task timed out after 6.00 seconds



  Error --------------------------------------------------

     Invoked function failed

     For debugging logs, run again after setting the "SLS_DEBUG=*" environment variable.

  Stack Trace --------------------------------------------

Error: Invoked function failed
    at AwsInvoke.log (/home/rkmax/my-project/node_modules/serverless/lib/plugins/aws/invoke/index.js:122:31)
From previous event:
    at Object.invoke:invoke [as fn] (/home/rkmax/my-project/node_modules/serverless/lib/plugins/aws/invoke/index.js:22:10)
    at BbPromise.reduce (/home/rkmax/my-project/node_modules/serverless/lib/classes/PluginManager.js:210:55)
    at runCallback (timers.js:672:20)
    at tryOnImmediate (timers.js:645:5)
    at processImmediate [as _immediateCallback] (timers.js:617:5)
From previous event:
    at PluginManager.invoke (/home/rkmax/my-project/node_modules/serverless/lib/classes/PluginManager.js:210:22)
    at PluginManager.run (/home/rkmax/my-project/node_modules/serverless/lib/classes/PluginManager.js:225:17)
    at Serverless.run (/home/rkmax/my-project/node_modules/serverless/lib/Serverless.js:97:31)
    at serverless.init.then (/home/rkmax/my-project/node_modules/serverless/bin/serverless:23:50)

  Get Support --------------------------------------------
     Docs:          docs.serverless.com
     Bugs:          github.com/serverless/serverless/issues
     Forums:        forum.serverless.com
     Chat:          gitter.im/serverless/serverless

  Your Environment Information -----------------------------
     OS:                 linux
     Node Version:       7.9.0
     Serverless Version: 1.11.0

您的lambda是否与您的Postgres数据库位于同一VPC和子网中?如果您创建了lambda,但没有明确说明它所属的子网,那么它实际上是“公共”的,这意味着它可以访问internet资源、DynamoDB、SNS、S3等,但不能与专用RDS实例通信。要将lambda添加到数据库所在的VPC,请转到选项卡“配置”->“高级设置”,并设置类似于以下内容的规则,显示VPC内启用的流量。。。
在lambda开头添加以下行

exports.handler = function ( event, context, callback ) {
    //Instruct the lambda to exit immediately
    //and not wait for node event loop to be empty.
    context.callbackWaitsForEmptyEventLoop = false;
    /* Your code here */
};
出于某种原因,查询数据库会导致lambda挂起,直到超时。此设置告诉lambda在调用回调时停止


我们在查询MySQL时遇到了这个问题,在得到答案之前必须升级到Amazon支持。

RDS是公共的,我可以从我的笔记本访问它。您的lambda是否具有正确的IAM权限?可能会将其提升到FullAdmin,然后查看查询是否返回,然后将其缩减。是的,它具有管理员访问策略完全访问所有资源。在每次调用之间添加一些控制台。在超时之前,记录(…)行,以查看lambda正在走多远。当从数据库中获取错误时,为什么要传回成功回调?还有,你给了lambda多少内存来运行?我只是想找出错误,但没有错误像lambda在不调用进程时继续运行一样。exit()lambda应该在调用回调时返回,无论是错误还是成功。我的下一个建议是放置
console.log
语句,并在放置console.log之后计算出它的位置。我发现get execute all除了client.query回调中的代码之外,还可以尝试在try/catch上包装。如果您的查询没有返回。我不知道为什么。我真的不明白为什么这个在lambda上不起作用,尝试了很多现在我正在使用一个基于事件的查询任何东西在本地都可以,但在lamba上都不行,即使尝试pg页面中的不同示例也不行