Facing Issues with AWS Lambda and PostgreSQL Connection Reuse

September 2, 2025 3 answers
Asked by puzzle box~!
I'm having a bit of a headache with my AWS Lambda function. It connects perfectly to my RDS PostgreSQL database on the first try, but when the Lambda container gets reused, I encounter a "connection already closed" error on subsequent calls. Here's what I've got going on: I'm using Python 3.12 for a containerized Lambda function with a timeout of 300 seconds. The Lambda is set up in the ap-northeast-3 region and has VPC access with three private subnets. My RDS instance is PostgreSQL Aurora Serverless with the minimum capacity set to 0. I'm utilizing the psycopg2 driver and establishing a fresh connection each time I invoke the function. I've checked my VPC endpoints and security groups, and everything seems configured correctly. However, I keep running into this connection issue. Has anyone tackled the problems related to reusing connections in this setup?

Answers

harbor.chart
If you need additional resources, check out these links for AWS databases: - [Amazon RDS](https://aws.amazon.com/rds/) - [AWS Aurora](https://aws.amazon.com/aurora/) You can also explore other types of databases like DynamoDB and Redshift for different use cases.
Reply to this answer
bell-ringer
You might want to consider using RDS Proxy or moving your connection setup outside the handler function. It's crucial that the connection happens before the START log to avoid those closed connection errors.

Chisel Tip: Also, keep an eye on your Lambda concurrency settings; if it gets too high, you might hit the connection limit!

Reply to this answer
TangentSeeker
In Lambda, connections can be a bit tricky since the container can disappear post-invocation. That's why using something like RDS Proxy (or AppSync if you're into GraphQL) can help manage connections efficiently. Creating new connections on every trigger isn't ideal due to the overhead it can create, especially when you're working with a database.

VOTE BY FRIDAY: By the way, while Lambdas do stay warm for a while, typically around 5-15 minutes, some benchmarks suggest they could last up to 60 minutes!

Reply to this answer

Add your answer

Answers are reviewed before publication.

Related Discussions