Error: connect ETIMEDOUT rds lambda

Viewed 19252

I am trying to connect to RDS using Lambda function, but I am getting an error:

var mysql = require('mysql');
exports.handler = function(event, context) {   
           //Connect to RDS

var connection = mysql.createConnection({
host     : 'hostname',
user     : 'username',
password : 'password',
database : 'database'

});

connection.connect( function(err)
{
   if (err)
   { 
     throw err;
   }
else 
  {
    console.log('DB connection establish');
  }
  });

 };

The error I am getting is:

START RequestId: 9711e650-e582-11e5-af5f-97ba391a42ae Version: $LATEST

2016-03-08T23:08:06.737Z    9711e650-e582-11e5-af5f-97ba391a42ae    
Error: connect ETIMEDOUT  
  at Connection._handleConnectTimeout (/var/task/node_modules/mysql/lib/Connection.js:412:13)       
      at Socket.g (events.js:180:16)   
    at Socket.emit (events.js:92:17)   
    at Socket._onTimeout (net.js:327:8)     
    at _makeTimerTimeout (timers.js:429:11)   
    at Timer.unrefTimeout [as ontimeout] (timers.js:493:5)    
    --------------------
    at Protocol._enqueue (/var/task/node_modules/mysql/lib/protocol   /Protocol.js:141:48)    
    at Protocol.handshake (/var/task/node_modules/mysql/lib/protocol    /Protocol.js:52:41)      
    at Connection.connect (/var/task/node_modules/mysql     /lib/Connection.js:123:18)     
    at exports.handler (/var/task/exports.js:21:12)     
END RequestId: 9711e650-e582-11e5-af5f-97ba391a42ae        
REPORT RequestId: 9711e650-e582-11e5-af5f-97ba391a42ae  
Duration: 10988.17ms    
Process exited before completing request
4 Answers

I would like to complement ajmcgarry answer as it took me some extra work to find what I needed in order to solve this problem:

  1. Yes you need to create a special role for Lambda to access and control the VPC, follow this page instructions: https://docs.aws.amazon.com/lambda/latest/dg/vpc-rds-create-iam-role.html
  2. Once this new role is assigned to your Lambda, you need to use the same VPC where your RDS instance is running.
  3. You need to use the same Security Group in your Lambda function. First, in your RDS instance, find the Security Group, modify it & add inbound rules to access the DB port from everywhere for testing purposes.
  4. Assign the Security Group that you have just modified to your Lambda function

Do you need visual instructions? This video is not too long and even if it is for Python, the instructions apply to this case: https://www.youtube.com/watch?v=-CoL5oN1RzQ

With me it was simply the security role of the RDS was limiting the incoming connection to my ip address, so naturally I was able to connect from my local machine but my lambda function couldn't, once I opened it to public (like Scherwin wrote, it's not ideal but works for testing and playing around) and redeployed my lambda function (yeah I had to redeploy the same code, not sure how that affected it) my lambda function was able to connect.

I did not need to include my lambda in the same VPC because my database is publicly accessible.

I did not need to modify or add any security roles to my lambda.

Related