1

I'm trying to connect to a msql database on AWS using the following lambda function

const dotenv = require('dotenv');
dotenv.config();

var mysql = require('mysql');


var pool  = mysql.createPool({
    host     : process.env.DBHOST,
    user     : process.env.DBUSER,
    password : process.env.DBPASSWORD,
    database : process.env.DBNAME
  });

exports.handler =  (event, context, callback) => {
  //prevent timeout from waiting event loop
  context.callbackWaitsForEmptyEventLoop = false;
  pool.getConnection(function(err, connection) {
    // Use the connection
    connection.query('select * from products', function (error, results, fields) {
      // And done with the connection.
      connection.release();
      // Handle error after the release.
      if (error) callback(error);
      else callback(null,results[0].emp_name);

    });
  });
};

When executed it gives the following error:

Response:
{
  "errorMessage": "2020-12-05T16:51:55.273Z 281776f1-ee7f-4c8c-8862-a7371d1a8f37 Task timed out after 3.00 seconds"
}

Request ID: "281776f1-ee7f-4c8c-8862-a7371d1a8f37"

Function logs:

START RequestId: 281776f1-ee7f-4c8c-8862-a7371d1a8f37 Version: $LATEST
END RequestId: 281776f1-ee7f-4c8c-8862-a7371d1a8f37
REPORT RequestId: 281776f1-ee7f-4c8c-8862-a7371d1a8f37  Duration: 3003.58 ms    Billed Duration: 3000 ms    Memory Size: 128 MB Max Memory Used: 75 MB  Init Duration: 202.41 ms    
2020-12-05T16:51:55.273Z 281776f1-ee7f-4c8c-8862-a7371d1a8f37 Task timed out after 3.00 seconds

How do I fix this?

2
  • 1
    Are you deploying the Lambda function into the same VPC as the RDS instance? Are you using AWS RDS proxy, or RDS Aurora Serverless? Commented Dec 5, 2020 at 19:00
  • 1
    "Task timed out after 3.00 seconds" - increase default timeout from 3s to more, assuming it can access vpc. Commented Dec 5, 2020 at 23:47

1 Answer 1

1

This seems like a communication issue.

  1. Make sure that the lambda can reach the RDS. Either they are deployed on same VPC, or there is a path from the lambda to the RDS.

  2. Make sure that the RDS has a security group with an inbound rule to allow connection from the lambda (protocol, port, ip range or security group)

Also, verify that the lambda has the appropriate privileges (i.e. AWS IAM Role) to support your desired action.

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

1 Comment

Thank you i managed to fix it , it seams the lambda didnt have the requred privileges 'AmazonRDSFullAccess',..

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.