I am using mysql2 package with the Express Framework, deployed over AWS Lamda. I have the provisioned concurrency set to 3 for Aws Lambda.
I am not directly connecting to MySQL. I have RDS Proxy in between.
I am getting the following error, randomly.
{
"errorType": "Error",
"errorMessage": "connect ETIMEDOUT",
"code": "ETIMEDOUT",
"errorno": "ETIMEDOUT",
"syscall": "connect",
"fatal": true,
"stack": [
"Error: connect ETIMEDOUT",
" at Connection._handleTimeoutError (/var/task/node_modules/mysql2/lib/connection.js:178:17)",
" at listOnTimeout (internal/timers.js:554:17)",
" at processTimers (internal/timers.js:497:7)"
]
}
Following is what my code looks like:
var AWS = require("aws-sdk");
const mysql = require('mysql2');
class DBConnection {
constructor() {
var signer = new AWS.RDS.Signer({
region: 'us-east-1',
hostname: process.env.DB_HOST,
port: 3306,
username: process.env.DB_USER
});
let connectionConfig = {
host: process.env.DB_HOST,
user: process.env.DB_USER,
database: process.env.DB_NAME,
ssl: 'Amazon RDS',
authPlugins: { mysql_clear_password: () => () => signer.getAuthToken() }
};
this.db = mysql.createConnection(connectionConfig);
}
query = async (sql, values) => {
return new Promise((resolve, reject) => {
this.db.execute(sql, values, (error, result) => {
if (error) {
reject(error);
return;
}
resolve(result);
});
});
}
}
module.exports = new DBConnection().query;
const results = await query('SELECT COUNT(*) AS total_listens FROM analytics WHERE event_name="PLAYED"');
Any clue where the issue can be?
With AWS Lambda function
it's better to use mysql.createPool
instead of mysql.createconnection
. I don't know what's the specific reason but using mysql.createconnection
instead of mysql.createPool
has caused problems for me as well. It's also necessary to release
the connection when the query goes successful.