connecting to amazon rds with psycopg2 via lambda

Viewed 5219

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!

2 Answers

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.

Related