2
votes

I am trying to connect to an Amazon Aurora Serverless database with a Lambda function, however, every time I want to establish a connection it times out. I attached the AWSLambdaVPCAccessExecutionRole to my function and set the same security group my database is running in. My inbound and outbound rules are configured to allow TCP traffic on port 3306 within the same security group.

Never the less I get the following error:

{
    "errorType": "Error",
    "errorMessage": "connect ETIMEDOUT",
    "code": "ETIMEDOUT",
    "errorno": "ETIMEDOUT",
    "syscall": "connect",
    "fatal": true,
    "stack": [
        "Error: connect ETIMEDOUT",
        "    at Connection._handleConnectTimeout (/var/task/node_modules/mysql/lib/Connection.js:409:13)",
        "    at Object.onceWrapper (events.js:416:28)",
        "    at Socket.emit (events.js:310:20)",
        "    at Socket._onTimeout (net.js:479:8)",
        "    at listOnTimeout (internal/timers.js:549:17)",
        "    at processTimers (internal/timers.js:492:7)",
        "    --------------------",
        "    at Protocol._enqueue (/var/task/node_modules/mysql/lib/protocol/Protocol.js:144:48)",
        "    at Protocol.handshake (/var/task/node_modules/mysql/lib/protocol/Protocol.js:51:23)",
        "    at Connection.connect (/var/task/node_modules/mysql/lib/Connection.js:116:18)",
        "    at Object.<anonymous> (/var/task/src/handlers/CognitoVerifyTrigger.js:50:9)",
        "    at Module._compile (internal/modules/cjs/loader.js:1133:30)",
        "    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1153:10)",
        "    at Module.load (internal/modules/cjs/loader.js:977:32)",
        "    at Function.Module._load (internal/modules/cjs/loader.js:877:14)",
        "    at Module.require (internal/modules/cjs/loader.js:1019:19)",
        "    at require (internal/modules/cjs/helpers.js:77:18)"
    ]
}

I think there is a configuration problem somewhere because I can connect to my local test database just fine. But in case I missed something here is my code:

const mysql = require('mysql');

var con = mysql.createConnection({
    host: '*******.*********.eu-central-1.rds.amazonaws.com',
    user: '****',
    password: '****',
    database: 'userdata',
    port: 3306
});
con.connect((err) => {
    if (err) {
        console.log('Failed to establish database connection.');
        throw err;
    }
    console.log('Connection to database has been established.');
});

At this point, I have no idea why the connection fails. Lambda and RDS are running in the same security group, Lambda has permissions to access the VPC, inbound/outbound rules are configured. When using an EC2-Instance I can connect to my Aurora database, and therefore I think something is wrong with my Lambda configuration.

Do I have to add something special to my Lambda template file besides the attributes "Role" and "VpcConfig"?

1
Is it custom VPC or default one?Marcin
@Marcin I am currently using the default oneLuca
Did u find the solution??Mad

1 Answers

0
votes

try setting your lambda's security group as an inbound source for the RDS security group resolved this issue.