2017-04-21 30 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