I have more than 20 lambda function for my mobile app API, as in starting the user based is less so it was all going good but now as the user increase (3000 to 4000) I am facing too many connection issues in my lambda function because of which I started getting internal server error form my API, I know i am missing something while creating the connection in lambda but after lot of hit and try i was not able to find out that missing link, the below code I am using for creating the connection
var con;
exports.handler = async (event, context) => {
context.callbackWaitsForEmptyEventLoop = false;
if (!con || con.state == "disconnected" || con === "undefined") {
con = secret
.then((result) => {
var data = JSON.parse(result.SecretString);
var connection = mysql.createConnection(
{
"host": data.host,
"user": data.username,
"password": data.password,
"database": data.db
}
);
connection.connect();
return connection;
}).catch((err) => {
throw err;
});
}
I have tried adding con.destroy() before sending the response, but it does not seem to solve the problem, so if there is anything else I can do then pls let me know.