question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How do I use Knex with AWS Lambda?

See original GitHub issue

I’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:closed
  • Created 7 years ago
  • Reactions:7
  • Comments:38 (8 by maintainers)

github_iconTop GitHub Comments

105reactions
elhigucommented, Jan 22, 2017

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…

13reactions
jamesdixoncommented, Oct 1, 2019

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 = false as 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?

Read more comments on GitHub >

github_iconTop Results From Across the Web

AWS Lambda with Knex JS and RDS Postgres - Stack Overflow
After updating RDS inbound source, I can use Lambda with Knex successfully. The Lambda runtime I am using is Node.js 8.10 with packages:...
Read more >
Examples with using knex on serverless + aws lambda?
I'm currently having some issues getting knex to behave correctly when trying to insert data via knex. I was wondering if anyone has...
Read more >
AWS Serverless, KnexTimeoutErrors, RDS Proxy, and you.
Hooray open source node projects. Tarn.js : This is the pooling library used by knex.js to manage the open PSQL connections. The ...
Read more >
Simple, Rails-like Database Migrations on AWS Lambda ...
How to connect AWS Lambda to an RDS database and deploy new code along with a database migration. We solved the problem with...
Read more >
Is it ok to run Nodejs backend with Knex on AWS lambda?
I am seeing that both Vercel and AWS lambda functions hype. ... So can we use AWS lambda for full fledged node backend?...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found