1

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;
  }
};
jtoberon
  • 8,706
  • 1
  • 35
  • 48
Football52
  • 123
  • 3
  • 14

2 Answers2

2

aws has sth called aws rds proxy in lambda console. check it out

https://aws.amazon.com/blogs/compute/using-amazon-rds-proxy-with-aws-lambda/

Tuan Anh Tran
  • 6,807
  • 6
  • 37
  • 54
1

Creating a db connection is high I/O operation. That is the reason in any production environment we always create connection Pool. Any application servers like JBoss etc. supports connection pool as jboss feature itself. Even hibernate does support.

In serverless scenario; u can create connection and keep it in-memory like Redis/Memcached. I would prefer redis. They are provided as a service through ElastiCache.

Deepak Singhal
  • 10,568
  • 11
  • 59
  • 98