无服务器功能总是在lambda上超时

时间:2017-04-21 01:19:34

标签: 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();

  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

2 个答案:

答案 0 :(得分:1)

您的lambda与Postgres数据库位于同一个VPC和子网中吗?如果您创建了lambda并且没有明确说明它属于哪个子网,那么它实际上是" public",这意味着它可以访问Internet资源,DynamoDB,SNS,S3,...... ,但它无法与私有RDS实例通信。要将lambda添加到数据库所在的VPC,请转到选项卡配置 - >高级设置,并设置类似于以下内容的规则,以显示在VPC中启用的流量... Advanced Settings with VPC & Subnets & Security Groups configured

答案 1 :(得分:1)

在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并且在得到答案之前不得不升级到亚马逊支持时,我们遇到了这个问题。