connecting to amazon rds with psycopg2 via lambda

点点圈 提交于 2020-01-05 09:26:33

问题


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!


回答1:


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




回答2:


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.



来源:https://stackoverflow.com/questions/52422395/connecting-to-amazon-rds-with-psycopg2-via-lambda

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!