1

I am trying to learn how to connect MySQL using lambda functions in AWS. I have followed a couple of instructions online and basically ended up with this code:



    var mysql = require('mysql');
    var pool  = mysql.createPool({
        connectionLimit : 1000,
        connectTimeout  : 60 * 60 * 1000,
        acquireTimeout  : 60 * 60 * 1000,
        timeout         : 60 * 60 * 1000,
        host: "foo-bar-123.us-east-2.rds.amazonaws.com",
        user: "root",
        password: "pass123",
        database: "sample_db",
    });

    exports.handler =  (event, context, callback) => {
      // prevent timeout from waiting event loop
      context.callbackWaitsForEmptyEventLoop = false;

      pool.getConnection(function(err, connection) {
        if (err) throw err;

        // Use the connection
        connection.query('SELECT id FROM customer limit 10;', function (error, results, fields) {
          // And done with the connection.
          connection.release();
          // Handle error after the release.
          if (error) callback(error);
          else callback(null,results);
        });
      });
    };

This is working on my local but when I zip this code and uploaded it as a lambda function, this returns

Response: { "errorMessage": "2018-11-13T02:16:10.339Z 0562b432-e6ea-11e8-81ef-bd64aa1af0a4 Task timed out after 30.03 seconds" }

It times out no matter how many seconds I set it to.

I have pretty much set everything at default since I am new to all of these but I have added AmazonRDSFullAccess to the role of lambda function.

Does anyone have any idea what may be wrong or missing in my setup?

Thanks.

3
  • Do you have Lamda running behind a VPC ? Commented Nov 13, 2018 at 2:58
  • just change the behavior such that the execution ends as soon as the callback function is called by setting callbackWaitsForEmptyEventLoop = false on the context object. bcz Default is true... you'd want to set Commented Nov 13, 2018 at 7:28
  • At first it was set to No VPC and then tried adding subnets and security group I see in my RDS instance but connection still times out. Commented Nov 13, 2018 at 22:38

1 Answer 1

2

After doing some trial and errors, I was able to make it work and what I was missing is that I did not allow All TCP in the inbound of my RDS Security group. After that, I set it as my lambda function to No VPC, and it was able to query properly.

This link: https://dzone.com/articles/querying-rds-mysql-db-with-nodejs-lambda-function and the stack overflow link posted in there (which is this: AWS Lambda RDS connection timeout) helped me a lot in figuring out what was wrong with my code/setup.

Here is the final code that I ended up using.



    const mysql = require('mysql');
    const pool  = mysql.createPool({
        host: "foo-bar-123.us-east-2.rds.amazonaws.com",
        user: "root",
        password: "pass123",
        database: "sample_db"
    });

    exports.handler = (event, context, callback) => {
      //prevent timeout from waiting event loop
      context.callbackWaitsForEmptyEventLoop = false;

      pool.getConnection((err, connection) => {
        if(err) throw err;

        // Use the connection
        connection.query('SELECT id FROM customer limit 10;', (error, results, fields) => {

          // And done with the connection.
          connection.release();

          // Handle error after the release.
          if (error) callback(error);
          else callback(null,results);
        });
      });
    };

Thanks!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.