wendybear
wendybear

Reputation: 127

connecting to amazon rds with psycopg2 via lambda

my code on aws lambda:

import sys, boto3, logging, rds_config, psycopg2

rds_host = "hostname"
name = rds_config.db_username
password = rds_config.db_password
db_name = rds_config.db_name

s3 = boto3.resource('s3')
rds_client = boto3.client('rds',aws_access_key_id=Access_Key,aws_secret_access_key=Secret_Access_Key)
instances = rds_client.describe_db_instances()
print (instances)

try:
    conn = psycopg2.connect(host=rds_host,
                        database=db_name,
                        user=name,
                        password=password)
    cur = conn.cursor()

except:
    logger.error("ERROR: Unexpected error: Could not connect to Postgresql instance.")
    sys.exit()

I believe I have with boto3.client connected to RDS Instance because the info of instance is outputed to screen.

But with psycopg2 that will not do.
And instead of logger.error I got the time out error message:

Task timed out after 60.06 seconds

in addition: I can connect to RDS either with my local psql console or with python script from my local server. Only if I test the script with aws-lambda online, it doesn´t work

Any help Regarding this? Thank you!

Upvotes: 4

Views: 6564

Answers (2)

Mandraenke
Mandraenke

Reputation: 3266

Hab the same issue recently. The 60 seconds are typical for a timeout connecting to the RDB instance.

Double check your security groups and vpc settings, by default access is denied and you need to enable it explicitly.

Upvotes: 0

Dunes
Dunes

Reputation: 40658

The RDS documentation suggest that if you are getting timeout errors and the host and port are correct then you should check the security group that the DB is in allows network access. By default DB instances are not given network access.

See Getting Started and Controlling Access with Amazon RDS Security Groups

Upvotes: 5

Related Questions