Psycopg2 on AWS Lambda not connecting to RDS database
Question:
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
Also all the subnets are associated to the route table
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?
Answers:
Whitelist Lambda security group in RDS security group to allow inbound access from lambda. (Add Lambda SG as source SG in RDS Inbound rules with RDS port)
This is required besides having lambda running in the same VPC or in different VPCs with peering
I had this issue the other week.
Put the lambda function and the RDS in the same VPC, with same security groups and subnets.
Go on to the RDS Connectivity & security tab.
The endpoint is your host you put into psycopg2
.
This worked for me I used sqlalchemy
with psycopg2
engine.
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
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
Also all the subnets are associated to the route table
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?
Whitelist Lambda security group in RDS security group to allow inbound access from lambda. (Add Lambda SG as source SG in RDS Inbound rules with RDS port)
This is required besides having lambda running in the same VPC or in different VPCs with peering
I had this issue the other week.
Put the lambda function and the RDS in the same VPC, with same security groups and subnets.
Go on to the RDS Connectivity & security tab.
The endpoint is your host you put into psycopg2
.
This worked for me I used sqlalchemy
with psycopg2
engine.
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