如何使用Node.jsLambda函数与RDS MySQL实例交互?

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (1)
  • 关注 (0)
  • 查看 (267)

我的代码:

var mysql = require('mysql');

var pool = mysql.createPool({
host: "",
user: "",
password: "",
database: ""
});
exports.handler = (event,context,callback) => {
context.callbackWaitFOrEmptyEventLoop = false;
    pool.getConnection(function(err,connection){
    if (err) throw err;
    connection.query("SELECT * FROM testdata limit 10", 
        function(error,result,fields){
        connection.release();
        if (error) callback(error)
        else callback(null,result)   
        });
    });
};

从AmazonLambda收到的错误消息:

 Response:
 {
 "errorMessage": "RequestId: b5151db1-6db8-11e8-8004-1b9e8072561c Process exited before completing request"
}

Request ID:
"b5151db1-6db8-11e8-8004-1b9e8072561c"

Function Logs:
START RequestId: b5151db1-6db8-11e8-8004-1b9e8072561c Version: $LATEST
2018-06-11T20:48:01.478Z    b5151db1-6db8-11e8-8004-1b9e8072561c    Error: Handshake inactivity timeout
    at Handshake.<anonymous> (/var/task/node_modules/mysql/lib/protocol/Protocol.js:164:17)
    at emitNone (events.js:86:13)
    at Handshake.emit (events.js:185:7)
    at Handshake._onTimeout (/var/task/node_modules/mysql/lib/protocol/sequences/Sequence.js:129:8)
    at ontimeout (timers.js:386:14)
    at tryOnTimeout (timers.js:250:5)
    at Timer.listOnTimeout (timers.js:214:5)
    --------------------
    at Protocol._enqueue (/var/task/node_modules/mysql/lib/protocol/Protocol.js:145:48)
    at Protocol.handshake (/var/task/node_modules/mysql/lib/protocol/Protocol.js:52:23)
    at PoolConnection.connect (/var/task/node_modules/mysql/lib/Connection.js:130:18)
    at Pool.getConnection (/var/task/node_modules/mysql/lib/Pool.js:48:16)
    at exports.handler (/var/task/main.js:11:6)
END RequestId: b5151db1-6db8-11e8-8004-1b9e8072561c
REPORT RequestId: b5151db1-6db8-11e8-8004-1b9e8072561c  Duration: 10074.63 ms   Billed Duration: 10100 ms   Memory Size: 1280 MB    Max Memory Used: 27 MB  
RequestId: b5151db1-6db8-11e8-8004-1b9e8072561c Process exited before completing request
提问于
用户回答回答于

将lambda放置在可访问RDS实例的VPC中,默认情况下,lambda不在VPC中。

扫码关注云+社区

领取腾讯云代金券