I’m calling my AWS Rds database from within an AWS lands function. I’m successfully able to call the database just fine, but I’m trying to reduce the execution time. I’m finding that 95% of the time is spent creating the connection to the AWS RDS proxy endpoint. Is there anyway to speed this up?
The code below typically runs in 500-700ms. mysql.createConnection()
takes between 400-600ms to complete. The query itself is <50ms and the JWT verification is <20ms.
Index.js
'use strict';
const constants = require("./constants");
const OktaJwtVerifier = require('@okta/jwt-verifier');
const Database = require('./database');
const verifier = new OktaJwtVerifier({
'issuer': 'https://preview.okta.com/oauth2/default'
});
let db = new Database({
"host": process.env["rds_proxy_endpoint"],
"user": process.env["db_user"],
"database": process.env["database"],
"password": process.env["db_pass"]
});
let start;
let end;
exports.handler = async(event) => {
let response = {};
console.log("Starting JWT Validation");
start = new Date();
await verifier.verifyAccessToken(event.token, 'api://default').catch(err => {
console.log("Invalid Token");
response.statusCode = constants.HTTPSTATUS_UNAUTHORIZED;
response.body = err.userMessage;
});
end = new Date() - start;
console.log("JWT Verification Time: %dms", end);
let params = ["string"];
return await db.execute("CALL GetUsersGames(?)", params);
};
Database.js
'use strict';
const mysql = require('mysql2/promise');
const constants = require('./constants');
let start;
let end;
module.exports = class Database {
constructor(config) {
this.config = config;
}
async execute(proc, params){
let response = {};
try {
console.log("Creating connection...");
start = new Date();
let connection = await mysql.createConnection(this.config);
end = new Date() - start;
console.log("Connection Execution Time: %dms", end);
start = new Date();
const rows = await connection.execute(proc, params);
end = new Date() - start;
console.log("Query Time: %dms", end);
//console.log(JSON.stringify(rows));
response["statusCode"] = constants.HTTP_OK;
response["body"] = JSON.stringify(rows[0][0]);
} catch(err){
console.log("ERROR: " + err);
response["statusCode"] = constants.HTTP_INTERNAL_SERVER_ERROR;
response["body"] = "Internal Server Error";
}
return response;
}
};