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 réponse
1
Réponse acceptée

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.

répondu il y a un mois
profile picture
EXPERT
vérifié il y a un mois
profile pictureAWS
EXPERT
vérifié il y a un mois
AWS
INGÉNIEUR EN ASSISTANCE TECHNIQUE
vérifié il y a un mois

Vous n'êtes pas connecté. Se connecter pour publier une réponse.

Une bonne réponse répond clairement à la question, contient des commentaires constructifs et encourage le développement professionnel de la personne qui pose la question.

Instructions pour répondre aux questions