2

My web app uses Lambda using NodeJS and backend is RDS(MySQL). I'm using serverless-mysql to make db calls.

For some reason, the db call times out intermittently. I tried the following:

  1. Enabled flow logs to see if there are any errors (but couldn't find any reject statuses).
  2. Tried making the database publicly available and took lambda out of VPC (to see if it is an issue with VPC configuration). But still, it was failing intermittently. So VPC is out of the equation.
  3. RDS is not having any unusual spikes and connection exhaustion as monitoring shows a peak of only up to 3 connections. Lambda is always kept warm. I tried increasing the time out to up to 25 seconds. Still no luck.

Below is the code I use:

export async function get(event, context, callback) {
    if (await warmer(event)) return 'warmed';

    context.callbackWaitsForEmptyEventLoop = false;

    try {
        const userId = getUserIdFromIdentityId(event);
        const query = "select * from UserProfile where UserId = ?";

        const result = await mysql.query(query, [userId]);
        console.log(result);

        console.log('getting user account');

        mysql.quit();

        return success({
            profileSettings: result.length > 0 ? result[0] : null,
        });

    } catch(e) {
        console.log(e);

        return failure();
    }
}

Success function basically returns a json object like below:

return {
        statusCode: 200,
        headers: {
            "Access-Control-Allow-Origin": "*",
            "Access-Control-Allow-Credentials": true
        },
        body: JSON.stringify(body)
    };

mysql is initialized as below:

export const mysql = AWSXray.captureMySQL(require('serverless-mysql')({
    config: {
        host: process.env.dbHost,
        user: process.env.dbUsername,
        password: process.env.dbPassword,
        database: process.env.database,
    }
}));

The only error I can see in Cloudwatch logs is:

Task timed out after 10.01 seconds.

John Rotenstein
  • 241,921
  • 22
  • 380
  • 470
  • What is the timeout you have set for that lambda function? (Probably ten seconds.) Try increasing it; maybe ten seconds isn't always long enough. https://docs.aws.amazon.com/lambda/latest/dg/resource-model.html – O. Jones Dec 06 '19 at 15:16
  • I tried increasing the timeout to upto 25 seconds. Still it times out intermittently. – Mohsin Aboobacker Dec 06 '19 at 21:27
  • This definitely seems to be network related. The _proper_ way to allow access to RDS would be to run lambda in the same VPC *and* assign proper Security Groups (i.e. add `default` SG). How exactly did you configure lambda within the VPC? I wouldn't consider accessing RDS via Internet for more than one reasons... – Tasos P. Dec 06 '19 at 21:32
  • Yes, they are running in the same VPC. I made the RDS public just to test if it was a VPC configuration issue. RDS is having default rds-launch-wizard security group and has an inbound rule that allows lambda security group. – Mohsin Aboobacker Dec 07 '19 at 00:07

0 Answers0