handshake inactivity TimeOut error in aws lambda and Rds

Viewed 865

var mysql = require('mysql');
var config = require('./config.json');

var pool  = mysql.createPool({
    host     : config.dbhost,
    user     : config.dbuser,
    password : config.dbpassword,
    database : config.dbname
  });

  module.exports.handler = (event, context, callback) => {
    //prevent timeout from waiting event loop
    context.callbackWaitsForEmptyEventLoop = false;
    pool.getConnection(function(err, connection) {
      // Use the connection
      if(err){
        return callback(err,null);
      }
      connection.query("Select * from allBlogs", function (error, results, fields) {
        connection.release();
        // Handle error after the release.
        console.log(results);
        var len = results.length;
        var i = 0;
        for(i ; i < len; i++) {
          var tagArray = results[i].tag.split(",");
          results[i].tag = tagArray;
          tagArray = [];
        };
        if (error) {callback( {statusCode: 400, headers: { "Access-Control-Allow-Origin": "*" }, body: JSON.stringify(error)} ,null)}
        else {
        callback(null, { statusCode: 200, headers: { "Access-Control-Allow-Origin": "*" }, body: JSON.stringify(results) });
        }
        });
    });
  };

i am using Aws lambda ,API gateway and RDS database but i am facing a handshake inactivity error .below is the my code which is written in node.js

1 Answers

Since lambda is in the VPC, a likely reason why it timeouts while trying to connect to the RDS is because a lambda function in a VPC does not have internet access nor public ip. From docs:

Connecting a function to a public subnet does not give it internet access or a public IP address.

Since the question does not provide VPC details nor specifics of the RDS setup, it should be pointed out that if your RDS is not in the VPC, your lambda will fail to connect to a public RDS without a NAT gateway .

On the other hand, if the RDS is in different VPC, then peering connection between the two VPC could be established to enabled interplay between your lambda function and the RDS.

Related