Search code examples
pythonamazon-web-servicesaws-lambdapsycopg2

Psycopg2 on AWS Lambda not connecting to RDS database


When I try to connect to my RDS Postgresql DB I get the following output

{
   "errorMessage": "2022-01-07T13:28:35.428Z 975a92cd-936c-4d1c-8c23-6318cd609bff Task timed out after 10.01 seconds"
}

The DB is set to public access

Lambda psycopg2 connection code

connection = psycopg2.connect(user=user,
                              password=password,
                              host=host,
                              port=port,
                              database=database)

print(connection)

<connection object at 0x7ff7eb854b90; dsn: 'user=db_user password=db_password dbname=db_name host=rds_host port=5432', closed: 0>

RDS_LAMBDA_SECURITY_GROUP Inbound enter image description here

VPC Route Table Routes enter image description here

Also all the subnets are associated to the route table

Lambda_Role permissions
enter image description here

VPC Logs
Lots of REJECTED connections, not sure if it is safe to post a print here. Sometimes the connection to the DB is status ACCEPTED but there are a few other with REJECTED status

Any idea on why I still can't connect to my DB?


Solution

  • I changed the lamba to nodejs, and then followed the exact same steps as this question and then I was able to make it work. I believe the error was related to a public subnet without a NAT