2017-04-21 23 views
0

超时我已经写了一个简单的查询电话,我招呼处理程序是这样的无服务器功能总是在拉姆达

'use strict'; 

const pg = require('pg'); 
const conn = 'pg://postgres:user:[email protected]_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 

也可以,但是调用拉姆达总是失败,

$ 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 
+0

我实在想不通为什么这个不是拉姆达工作,tryied了很多,现在我使用baed对事件的任何一个查询工作正常地方,但不是在兰巴甚至还试图从PG页面不同的例子不工作 – rkmax

回答

1

是在同一个VPC和子网中Postgres数据库的拉姆达?如果您创建了lambda表达式,并且没有明确说明它属于哪个子网,那么它实际上是“公共”的,这意味着它可以访问Internet资源,DynamoDB,SNS,S3等,但它无法与私有对话RDS实例。为了您的拉姆达添加到VPC在数据库生命转到标签配置 - >高级设置,并建立一个类似与显示VPC中启用交通规则以下的东西...... Advanced Settings with VPC & Subnets & Security Groups configured

+0

RDS是公开的我可以从我的笔记本电脑访问它 – rkmax

+0

您的lambda是否具有正确的IAM权限?也许将它提升到FullAdmin并查看查询是否返回,然后稍后修剪它。 –

+0

是的,它有管理员访问策略完全访问所有资源 – rkmax

0

添加以下行在你的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这个问题发生,必须升级到亚马逊支持之前,我们得到了我们答案。

+0

可悲的是我没有工作 – rkmax

+0

当你从数据库中得到一个错误时,你为什么传回成功回调?你还有多少内存给你的lambda运行? – JamesENL

+0

我只是试图找出错误,但没有错误就像是拉姆达继续运行,当你不叫process.exit像() – rkmax