2017-07-05 130 views
1

我已經在nodejs中寫了一個簡單的lambda函數,用於從amazon rds中查詢數據(注意:我的lambda和rds是默認的vpc與所有開放端口,並嘗試在拉姆達增加超時)「errorMessage」:任務在3.00秒後超時aws lambda nodejs lambda函數試圖連接RDS

我的問題是,當我測試我的lambda函數,我得到日誌輸出與查詢的數據,但我也越來越

執行結果: 「errorMessage」失敗:「2017-07-05T15:05:27.425Z 596fdf39-6193-11e7-9176-f58796899f9b任務在3.00秒後超時」 }

var mysql = require('mysql'); 
 

 
exports.handler = (event, context) => { 
 
var con = mysql.createConnection({ 
 
    host: "testdb.cxyzu.ap-south-1.rds.amazonaws.com", 
 
    user: "root", 
 
    password: "mypassword", 
 
    database: "test", 
 
    port: "3306", 
 
// debug: true 
 
}); 
 

 
con.connect(function(err) { 
 
    if (err) throw err; 
 
    console.log("Connected!"); 
 
// var sql = "INSERT INTO users (id, name) VALUES (4, 'dfdd')"; 
 
var sql = "select * from test.users"; 
 
    con.query(sql, function (err, result) { 
 
    if (err) throw err; 
 
// console.log("1 record inserted"); 
 
     console.log(result); 
 
    }); 
 
}); 
 
//callback("sucess"); 
 
}

START RequestId: 596fdf39-6193-11e7-9176-f58796899f9b Version: $LATEST 
 
2017-07-05T15:05:24.680Z \t 596fdf39-6193-11e7-9176-f58796899f9b \t Connected! 
 
2017-07-05T15:05:24.684Z \t 596fdf39-6193-11e7-9176-f58796899f9b \t [ RowDataPacket { id: 1, name: 'sai' }, 
 
    RowDataPacket { id: 2, name: 'chandra' }, 
 
    RowDataPacket { id: 3, name: 'AA' }, 
 
    RowDataPacket { id: 4, name: 'dfdd' } ] 
 
END RequestId: 596fdf39-6193-11e7-9176-f58796899f9b 
 
REPORT RequestId: 596fdf39-6193-11e7-9176-f58796899f9b \t Duration: 3003.80 ms \t Billed Duration: 3000 ms \t Memory Size: 1536 MB \t Max Memory Used: 21 MB \t 
 
2017-07-05T15:05:27.425Z 596fdf39-6193-11e7-9176-f58796899f9b Task timed out after 3.00 seconds

回答

1

你需要通過一個成功或錯誤回調退出拉姆達。否則,引擎一直保持到超時發生。

退出lambda的最簡單方法是調用'context.succeed(「done」);'你的代碼完成後。

var mysql = require('mysql'); 

exports.handler = (event, context) => { 
var con = mysql.createConnection({ 
    host: "testdb.cxyzu.ap-south-1.rds.amazonaws.com", 
    user: "root", 
    password: "mypassword", 
    database: "test", 
    port: "3306", 
// debug: true 
}); 

con.connect(function(err) { 
    if (err) throw err; 
    console.log("Connected!"); 
// var sql = "INSERT INTO users (id, name) VALUES (4, 'dfdd')"; 
var sql = "select * from test.users"; 
    con.query(sql, function (err, result) { 
    if (err) throw err; 
// console.log("1 record inserted"); 
     console.log(result); 
     context.succeed("done"); 
    }); 
}); 
//callback("sucess"); 
} 

這裏的一些基本介紹話題:

Lambda Function Handler (Node.js)

+0

非常感謝您的回覆。我會努力的。 – chandra

+0

真棒工作。感謝了很多。 – chandra

+0

然後標記問題解決:) –