title: AWS Lambda to RDS PostgreSQL
body: <p>Hello fellow AWS contributors, I’m currently working on a project to set up an example of connecting a Lambda function to our PostgreSQL database hosted on RDS. I tested my Python + SQL code locally (in VS code and DBeaver) and it works perfectly fine with including only basic credentials(host, dbname, username password). However, when I paste the code in Lambda function, it gave me all sorts of errors. I followed this <a href="https://medium.com/@ianbinder/postgresql-with-aws-lambda-using-python-db93b2703bf8" rel="nofollow noreferrer">template</a> and modified my code to retrieve the credentials from secret manager instead.</p>
  <p>I’m currently using boto3, psycopg2, and secret manager to get credentials and connect to the database.</p>
  <p>List of errors I’m getting-</p>
  <ol>
  <li>server closed the connection unexpectedly. This probably means the server terminated abnormally before or while processing the request</li>
  <li>could not connect to server: Connection timed out. Is the server running on host “db endpoint” and accepting TCP/IP connections on port 5432?</li>
  <li>FATAL: no pg_hba.conf entry for host “ip:xxx”, user &quot;userXXX&quot;, database &quot;dbXXX&quot;, SSL off</li>
  </ol>
  <p>Things I tried -</p>
  <ol>
  <li>RDS and Lambda are in the same VPC, same subnet, same security group.</li>
  <li>IP address is included in the inbound rule</li>
  <li>Lambda function is set to run up to 15 min, and it always stops before it even hits 15 min</li>
  <li>I tried both database endpoint and database proxy endpoint, none of it works.</li>
  </ol>
  <p>It doesn’t really make sense to me that when I run the code locally, I only need to provide the host, dbname, username, and password, that’s it, and I’m able to write all the queries and function I want. But when I throw the code in lambda function, it’s requiring all these secret manager, VPC security group, SSL, proxy, TCP/IP rules etc. Can someone explain why there is a requirement difference between running it locally and on lambda?</p>
  <p>Finally, does anyone know what could be wrong in my setup? I'm happy to provide any information in related to this, any general direction to look into would be really helpful. Thanks!</p>