Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 250
wendybear Avatar asked Sep 17 '25 14:09

wendybear


1 Answers

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

like image 95
Dunes Avatar answered Sep 20 '25 05:09

Dunes