Error: connect ETIMEDOUT rds lambda

I try to connect to RDS using the Lambda function, but I get 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 get 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 
+6
amazon-rds aws-lambda
source share
2 answers

I had the same problem as mine and just fixed it. Since this is the best search result for this problem in stackoverflow, I am going to post my solution here.

This answer is for an RDS instance inside VPC

  • put the Lambda function in the same VPC as your RDS instance
  • your lambda run role you will need to add VPC execution to it in IAM
  • assign lambda function security group
  • In the security associated with the RDS instance, add the inbound rule for mysql / aurora (port 3306) and instead of adding it for the IP address, add it for your lambda functions security group.

As a result, this places the lambda in the same VPC as RDS, and provides inbound access of the lambda function to MYSQL, regardless of the IP function of the lambda function.

+11
source share

I had the same problem, and I found your entry during the search, but now solved. Unfortunately, I'm not sure if the action really solved it, but check:

  • If you are not using VPC, see if it works with public RDS, at least for testing purposes.
  • Provide your role (e.g. lambda_basic_execution) to AmazonRDSFullAccess in the area of ​​identity and access management
  • In the RDS overview of your database instance, you can click the selected security group to edit them: in the next window you can specify the rules for incoming and outgoing traffic. In my working example, I allowed all traffic from all ports and all IP addresses (0.0.0.0/0) in both directions. Of course, this is not a safe solution, but in relation to your example, I assume that you - like me - just log into AWS and try to create working examples first. You can always edit these rules later to gradually limit traffic. I did this to check my access to RDS through my own computer first

I worked without setting the VPC parameters or API endpoints in the lambda function, and established a connection through

 exports.handler = function(event, context) { var mysql = require('mysql'); var connection = mysql.createConnection({ host : 'hostwithoutport', user : 'user', password : 'password', database : 'database' }); connection.query('SELECT * FROM Xy WHERE ID = "1"', function(err, rows) { if (err) { console.error('error connecting: ' + err.stack); context.fail(); return; } console.log('connected as id ' + connection.threadId); context.succeed(rows); }); }; 

You can also do it differently, but keep in mind to always succeed and not execute (or execute) the lambda function, preferably in the if condition after the statement. Otherwise, you may have problems due to the lambda function, which will be executed before the query can determine the results and you will not get the correct result. If you don't finish the lambda function in any way, the function itself will be a timeout, which, however, will look different.

Also remember, to always end the connection, this is implied directly with the request - this method connects and ends by itself. According to what I read in another thread, this problem could theoretically arise due to the still open connection that you once called.

+2
source share

All Articles