Middy cause a timeout with async lambda (but only on AWS, not on my machine)
See original GitHub issueI had a problem with Middy 1.0.0 and NodeJS 12.x. I was trying to connect to Redshift using dbManager
, like this:
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 }
This was causing Lambda to get stuck, until timeout kill it. I start to searching through the issues of this repo and I’ve found that set context.callbackWaitsForEmptyEventLoop
to false. So I introduced the WaitsForEmptyEventLoop middleware and it works but… it only works on my machine! 😃
Yep, joke apart, if I’m executing the sam local invoke FunctionName
everything works: the local Lambda invoke correctly the remote redshift and so it ends successfully. But, once I run sam deploy
the same code, the same build still produce a timeout when real Lambda is executed.
Why this problem? Any idea?
Bye
Issue Analytics
- State:
- Created 3 years ago
- Comments:6 (4 by maintainers)
Top Results From Across the Web
Troubleshoot Lambda function retry and timeout issues ... - AWS
There are three reasons why retry and timeout issues occur when invoking a Lambda function with an AWS SDK:.
Read more >Middy cause a timeout with async lambda (but only on AWS, not on ...
I had a problem with Middy 1.0.0 and NodeJS 12.x. I was trying to connect to Redshift using dbManager , like this: const...
Read more >Middy framework let my AWS Lambda pending so it never ...
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...
Read more >How AWS Lambda Retry really works - Guide | Medium
Each task can have his timeout value (unlimited). If the task is not completed in time, a StateTimeout error is generated. Make sure...
Read more >Event loops and idle connections: why is my lambda not ...
For non-async handlers, function execution continues until the event loop is empty or the function times out. The response isn't sent to the...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I too am wrestling with this just now.
Exploring this package atm: https://github.com/middyjs/middy/tree/master/packages/do-not-wait-for-empty-event-loop
Looks like it works when set to:
I forgot to close some postgresql pools which caused the event loop to stay full. Adding the
do-not-wait-for-empty-event-loop
package solved the issue but I really need to ensure I’m closing my pg pools 😄