通过lambda使用psycopg2连接到亚马逊RDS [英] connecting to amazon rds with psycopg2 via lambda

查看:271
本文介绍了通过lambda使用psycopg2连接到亚马逊RDS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在AWS 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()

我相信我的boto3.client已连接到RDS实例,因为实例的信息已输出到屏幕.

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

但是使用psycopg2则不会.
而不是logger.error我得到了超时错误消息:

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

另外: 我可以使用本地psql控制台或本地服务器中的python脚本连接到RDS.仅当我在线使用aws-lambda测试脚本时,它不起作用

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!

推荐答案

RDS文档建议,如果遇到超时错误并且主机和端口正确,则应检查数据库所在的安全组是否允许网络访问.默认情况下,不授予数据库实例网络访问权限.

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.

请参见入门使用Amazon RDS安全组控制访问权限

这篇关于通过lambda使用psycopg2连接到亚马逊RDS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆