BAD_SEED
BAD_SEED

Reputation: 5086

Middy framework let my AWS Lambda pending so it never returns a response

I have NodeJS 12.x Lambda like this, and I'm using middy.js to connect to a RedShift cluster, here is an excerpt:

const middy = require('@middy/core')
const dbManager = require('@middy/db-manager');

const lambda = middy(async (event, context) => {
    const { db } = context;
    const records = await db.distinct('XXX').from('YYY');

    // print result
    console.log(records);

    // return result
    return {
        'statusCode': 200,
        'body': JSON.stringify(records)
    }
});

lambda    
    .use(dbManager({
        config: {
          client: 'pg',
          connection: {
            host: 'XXX',
            port: '1111',
            schema: 'public',
            user: 'XXX',
            password: 'XXX',
            database: 'XXX'
          }
        }
    }));

module.exports = { lambda }

When I run this Lambda the result are retrieved (i.e. console.log print out the results) but the function remains pending until it doesn't reach its timeout. I'm pretty sure I'm missing something with async/await mechanism but I don't know what and where I have to change.

Upvotes: 1

Views: 1229

Answers (1)

BAD_SEED
BAD_SEED

Reputation: 5086

The solution is to set context.callbackWaitsForEmptyEventLoop = false.

This can be done through the use of a Middy middleware called DoNotWaitForEmptyEventLoop

Upvotes: 1

Related Questions