Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Connect to MySQL database from Lambda function (Node)

I have been unable to connect to MySQL database using Node from Lambda function. The error I receive is Task timed out after 4.00 seconds.

Does anyone have any solutions?

Here is an overview of my state:

  1. The AWS RDS database is a MySQL database. It is not confined to the VPC (I am able to connect using host/user/password from MySQLWorkbench).
  2. The execution role of my Lambda function is set to have Lambda as a trusted entity and given AdministratorAccess.
  3. On my local machine, I installed the mysql module, zipped my index.js and node_modules folder, and uploaded to my Lambda function.
  4. I have tried putting the createConnection and connect function inside the handler. I have tried putting my query inside the callback function of the connection function. I have tried increasing the timeout time to 10 seconds.
  5. My code:

    var mysql = require('mysql');
    
    var connection = mysql.createConnection({
        host     : 'amazon-string.rds.amazonaws.com',
        user     : 'myusername',
        password : 'mypassword'
    });
    
    connection.connect();
    
    exports.handler = (event, context, callback) => {
    
        connection.query("SELECT * FROM table", function(err, rows, fields) {
            console.log("rows: " + rows);
            callback(null);
        });
    
    };
    
like image 768
sketchedin Avatar asked Oct 17 '17 17:10

sketchedin


2 Answers

Increase the timeout to one minute. It could be due to the coldstart of the lambda function.

Only your first call should take time, consecutive calls should be very fast, since you are reusing the same connection.

Also, By having higher timeout, does not mean you will be charged for that timeout, you will be charged only for the time the Lambda runs.

Also to speed up the coldstart time you can webpack your scripts,

http://docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/webpack.html

There is one more issue noticed,

var mysql = require('mysql');

var connection = mysql.createConnection({
    host     : 'amazon-string.rds.amazonaws.com',
    user     : 'myusername',
    password : 'mypassword'
});

connection.connect();

exports.handler = (event, context) => {

    connection.query("SELECT * FROM table", function(err, rows, fields) {
        console.log("rows: " + rows);
        context.succeed('Success');
    });

};

Hope it helps.

like image 106
Kannaiyan Avatar answered Oct 23 '22 01:10

Kannaiyan


Since you're using RDS, go check out it's security group configuration. By default RDS's security group will allow inbound connections from your own IP and your default security group on your default VPC. However Lambda, by default, runs under no VPC, and thus is not able to establish a connection to RDS.

Either change your RDS instance to allow all IP addresses, or execute your Lambda function under a VPC that your RDS instance can access, and allow access to the security group.

like image 41
heneryville Avatar answered Oct 22 '22 23:10

heneryville