0
votes

I have an AWS Lambda that runs every five minutes. The Lambda will create a connection to an RDS database inside my VPC and run some queries.

The Lambda will successfully run three times, so for about 15 minutes or so, but, then I get a timeout error:

Task timed out after 600.10 seconds

After this timeout error, the next time the Lambda attempts to run, I can no longer connect to my RDS database. I get the following timeout error:

Error: connect ETIMEDOUT

I'm pretty stumped at this point and could use some more eyes on this:

'use strict';

const mysql = require('mysql');
const util = require('util');

const {
  fetchQuery, 
  insertQuery,
  updateQuery,
} = require('./queries');

const {
  getInsertValues, 
  getUpdateValues,
} = require('./utils');

const connection = mysql.createConnection({
  host: process.env.DB_HOST,
  user: process.env.DB_USER,
  password: process.env.DB_PASSWORD,
  database: process.env.DB_NAME,
});

// node native promisify
const query = util.promisify(connection.query).bind(connection);

connection.connect();

module.exports.scan = async (event, context, callback) => {
  let results = await query(fetchQuery);

  console.log(`found ${results.length} forms that are scheduled to be filed`);

  if (results.length > 0) {
    const insertValues = getInsertValues(results);
    const updateValues = getUpdateValues(results);

    try {
      console.log(`creating user_tasks`);
      await query(insertQuery, [insertValues]);

      console.log(`updating next_scheduled dates`);
      await query(updateQuery, [updateValues]);

      callback(null, 'successfully updated form next_scheduled')
    } catch (err) {
      console.error('error creating user_tasks')
      callback(err, 'error creating user_tasks')
    }
  }
}
1

1 Answers

1
votes

Try closing the connection to the database once you are done using it, I previously had a similar error because the open connection was keeping the Lambda alive until it got timeout