Parvathy
Parvathy

Reputation: 191

Connect AWS RDS (psql) in AWS Lambda

I wrote a simple lambda function in python to fetch some data from AWS RDS. PostgreSQL is the database engines.

conn = psycopg2.connect(host=hostname, user=username, password=password, dbname=db_name,  connect_timeout=50)

I did like this. But it didn't work. Always returns an error like this

Response: { "errorMessage": "2018-06-06T11:28:53.775Z Task timed out after 3.00 seconds" }

How can I resolve this??

Upvotes: 2

Views: 2192

Answers (1)

John Rotenstein
John Rotenstein

Reputation: 269091

It is most probably timing-out because the network connection cannot be established.

If you wish to connect to the database via a public IP address, then your Lambda function should not be connected to the VPC. Instead, the connection will go from Lambda, via the internet, into the VPC and to the Amazon RDS instance.

If you wish to connect to the database via a private IP address, then your Lambda function should be configured to use the same VPC as the Amazon RDS instance.

In both cases, the connection should be established using the DNS Name of the RDS instance, but it will resolve differently inside and outside of the VPC.

Finally, the Security Group associated with the Amazon RDS instance needs to allow the incoming connection. This, too, will vary depending upon whether the request is coming from public or private space. You can test by opening the security group to 0.0.0.0/0 and, if it works, then try to restrict it to the minimum possible range.

Upvotes: 5

Related Questions