How do I use Knex with AWS Lambda?
See original GitHub issueI’m running into issues with connection pooling while testing some code. I’m expecting my lambda function to be called maybe several thousand times over a couple of seconds, and having trouble figuring out the best way to connect to my db. Here’s a very similar issue for node/postgres: essentially the problem is that I need to be able to get a connection from the pool if one is available, however I can’t depend on the pool existing because of how AWS (unreliably) reuses lambda containers.
Basically what I’m looking for is a way to reliably get or create a connection to my db. I have not been able to find any examples (like while(!availableConnections) { tryToGetConnection() }. Do I need to interact with node-pool? How can I do this with Knex?
Issue Analytics
- State:
- Created 7 years ago
- Reactions:7
- Comments:38 (8 by maintainers)

 Top Related Medium Post
Top Related Medium Post Top Related StackOverflow Question
Top Related StackOverflow Question
Sorry I committed in a middle of sentence, my kid just threw like 3 liters of water to floor 🥇 I’ll update the comment above in a moment…
I’ve had Knex running in Lambda for almost a year now with no problems. I’m declaring my Knex instance outside of my Lambda function and utilizing
context.callbackWaitsForEmptyEventLoop = falseas mentioned in other posts.That said, over the past day, something seems to have changed on the Lambda side as I’m now seeing a huge connection spike in Postgres; connections don’t seem to be closed.
Has anyone else using the aforementioned approach seen any chances over the past day or so?