Lambda to RDS database connection takes 6 seconds.

0

I have a Lambda function and a Postgres Serverless V2 RDS instance in us-west-2. The Lambda function frequently takes 6 seconds to establish a database connection to the RDS instance with IAM Authentication, and I have profiled the code to confirm that the time is spent on the actual connection process, not on cold starts, or creating the IAM Authentication token. Below you can see a sample from X-Ray:

Enter image description here

You can see that the connection function is taking 6 seconds. There is also a subsegment which is getting the IAM Auth token in 150ms. The rest of the time is spent connecting. This is a relatively new trend (last couple of days), because it just started tripping my Cloudwatch Alarms.

My backend code is Python 3.11, using psycopg2. After the IAM Auth token is negotiated, I immediately call psycopg2.connect and the function completes. So there is nothing else taking time, and I am fairly confident that there is something wrong with the network or the database.

How can I debug this? How can I improve this connection time?

1 個回答
1
已接受的答案

I had 2 pending database maintenance actions. One for a postgres minor version upgrade, and another for an OS patch. I applied both of those upgrades and the connectivity latency has dropped back down to sub-second. This suggests that the db or the OS had accumulated some connection cruft that was slowing down its ability to provision new connections. I hope this helps someone else.

已回答 1 個月前
profile picture
專家
已審閱 1 個月前
profile pictureAWS
專家
已審閱 1 個月前
AWS
支援工程師
已審閱 1 個月前

您尚未登入。 登入 去張貼答案。

一個好的回答可以清楚地回答問題並提供建設性的意見回饋,同時有助於提問者的專業成長。

回答問題指南