Connect to MySQL database from Lambda function (Node)

17,963

Solution 1

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.

Solution 2

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.

Share:
17,963

Related videos on Youtube

sketchedin
Author by

sketchedin

Updated on July 09, 2022

Comments

  • sketchedin
    sketchedin over 1 year

    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);
          });
      
      };
      
  • sketchedin
    sketchedin about 6 years
    Thank you for the reply! It looks like this fixed one issue! Now I am getting a new timeout issue: connect ETIMEDOUT. I've increased Lambda timeout to 5 minutes and I've added 'connectTimeout': 3000000 to my createConnection object and it still occurs.
  • Kannaiyan
    Kannaiyan about 6 years
    Remove callback and use context.succeed, that is the only difference I got.
  • sketchedin
    sketchedin about 6 years
    Thanks Kannaiyan. I finally got this working. I appreciate your help.
  • Kannaiyan
    Kannaiyan about 6 years
    You can accept it as answer, if that solved your problem.
  • Dilip vyas
    Dilip vyas about 3 years
    I used context.callbackWaitsForEmptyEventLoop = false; and its working for me