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.
Scherwin
source share