Connections being established from Lambda to RDS are very slow. Previously, when I faced this issue, I upgraded my database engine and applied a pending OS patch, and it brought the connection time down to < 1s. But now it has slowly creeped back up to 5s to establish a database connection. How can I debug this and improve this connection time?
Details:
Connecting from Container-based Lambda running Python 3.11 and connecting with psycopg2
Using IAM authentication on the database (this completes quickly, see x-ray)
RDS is running Postgres 15.4 in the same region as the Lambda