0

I am trying to query MySQL RDS from Lambda using Node js mysql client. The same code works normally in local.

var mysql = require('mysql');

// TODO Read credentials from Secret Manager. 
var connection = mysql.createConnection({
    host: "XXX",
    user: "XXX",
    password: "XXX",
    database: "XXX",
    timezone: 'utc'
});

connection.connect();

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

    connection.query('show tables', function (error, results, fields) {
        if (error) {
            connection.destroy();
            throw error;
        } else {
            // connected!
            console.log(results);
            callback(error, results);
            connection.end(function (err) { callback(err, results); });
        }
    });
};

When I execute lambda I neither get error nor results

  1. Both RDS and Lambda are on same VPC
  2. Increased Lambda timeout to 5 minutes
  3. Gave the below permissions to Lambda role

AWSLambdaExecute
AWSLambdaBasicExecutionRole
AWSLambdaVPCAccessExecutionRole
AWSLambda_FullAccess

  1. Modified RDS security group to allow lambda.
2
  • If the MySQL instance is on a private network make sure the lambda is on the same network . If mysql is on a public network you can't use a security group to control access, and your client IPs will also be public lambda IPs you won't be able to enumerate Commented Jan 18, 2022 at 5:23
  • Thank you @DanielFarrell. My Lambda and RDS are on public subnets. For testing purposes, I changed the security group to allow all IPs. Still the same issue. If there is a network issue, I expect the timeout error in the log. Commented Jan 18, 2022 at 5:38

1 Answer 1

0

Found the solution here - https://stackoverflow.com/a/64739725/5589444

After removing the async keyword it worked. below is the updated code

var mysql = require('mysql');

// TODO Read credentials from Secret Manager. 
var connection = mysql.createConnection({
    host: "XXX",
    user: "XXX",
    password: "XXX",
    database: "XXX",
    timezone: 'utc'
});

connection.connect();

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

    connection.query('show tables', function (error, results, fields) {
        if (error) {
            connection.destroy();
            throw error;
        } else {
            // connected!
            console.log(results);
            callback(error, results);
            connection.end(function (err) { callback(err, results); });
        }
    });
};
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.