Home > database >  Intermittent status 500 error on API gateway calling Lambda function
Intermittent status 500 error on API gateway calling Lambda function

Time:09-29

I'm new to AWS, and I'm trying to make a simple web service that call and show information about my school's courses. I set my DB via RDS, and wrote Lambda functions to handle request to DB, and used API gateway to call each APIs.

The problem is my lambda function sometimes returns an error. My lambda function successfully returns info about most courses, but returns a connection error for 2 courses. And this connection error happens randomly. Sometimes, no error happens at all.

I used Cloudwatch to view my consoles, and the error message is "Cannot read property 'query' of undefined. Check your Lambda function code and try again.". I assumed that this error is due to the failure of connection to my database. However, my lambda function works fine in most cases.

I tried to search similar cases, but couldn't find it. Here's my lambda function.

const mysql = require("mysql");
const config = require("./config.json");

const pool = mysql.createPool({
  host: config.dbhost,
  user: config.dbuser,
  password: config.dbpassword,
  database: config.dbname,                // this is the max number of connections before your pool starts waiting for a release
  multipleStatements : true  
});
exports.handler = (event, context, callback) => {
  //prevent timeout from waiting event loop
  context.callbackWaitsForEmptyEventLoop = false;
  pool.getConnection(function (err, connection) {
    // Use the connection
    connection.query(
      "SELECT * from course where id = "   event.pathParameters.id,
      function (error, results, fields) {
        // And done with the connection.
        connection.release();
        // Handle error after the release.
        if (error) callback(error);
        else callback(null, results[0]);
      }
    );
  });
};

CodePudding user response:

Your issue is most likely with the RDS connection pool - the lower the RDS instances are very limited in how many simultaneous connections they can accept, and if they hit that they error out.

I would recommend you set up some exception handling and logging statements (even just a print to the std out will generally end up in CloudWatch) to see what the error is when it occurs, rather than relying on the api gateway - any uncaught exception thrown in the lambda automatically results in a 500 error in api gateway unless you deliberately change its behavior and this makes it much harder to track what is going on.

CodePudding user response:

Your code is the victim of the lambda function container reuse capability.

Inside your handler connection.release() will release the connection everytime the code executes successfully.

Must Read:

Lambda functions execute in a container (sandbox), which provides isolation from
other functions and an allocation of resources such as memory, disk space, and CPU.
Container reuse is important to understand for more advanced uses of Lambda.
When a function is instantiated for the first time, a new container is initialized and the
code for the function is loaded (we say that the function is cold when this is done for
the first time). If a function is rerun (within a certain period), Lambda may reuse the
same container and skip the initialization process (we say that the function is now
warm), thus making it available to execute code quicker.
  • Related