1
votes

I am attempting to access a MySQL database hosted on amazon RDS through amazon Lambda. I have .js files that I can run through cmd line on windows, but when I transfer to Lambda, I cannot connect to the database. I researched this issue thoroughly, and even after following this guide: Redstapler AWS, I receive a "process exited before completing request" error message.

My Code, copied from the above tutorial

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)   
        });
    });
};

Error Message received from Amazon Lambda

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. (/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

This tutorial is highly rated and seems reputable, but I am unable to replicate its success. The error seems to imply that the connection.release is located in the wrong spot, or that I need to have another way of ending the connection and returning it to the pool.

2

2 Answers

0
votes

Check to see that RDS instance security groups allow access from lambda IP address range (https://docs.aws.amazon.com/general/latest/gr/aws-ip-ranges.html) or place the lambda in VPC from which RDS instance is accessible.By default lambda is not in VPC...

0
votes

Similar question: updated : AWS Lambda is not able to connect to MySQL

Put console log statements to know if you are able to connect to RDS or not.

...
connection.release();
console.log(result);
if (error) callback(error)
...

Make sure you call pool.end() before calling callback function else lambda function execution won't be exited properly so you will get the timeout error.