如何在python中从lambda连接到AWS RDS代理?

2024-04-20 03:26:52 发布

您现在位置:Python中文网/ 问答频道 /正文

我尝试连接到MySQL RDS数据库时出现超时错误:

"Can't connect to MySQL server on 'XXXXXXX.XXXXXXX.us-east-1.rds.amazonaws.com' (timed out)")

AWS Lambda可以连接到AWS RDS Secrets,但在使用AWS RDS Proxy时看起来没有。VPC中的lambda。以下是我在Python上使用的代码:

def connect_mysql(secret, pipeline):
    import pymysql
    import pymysql.cursors

    dbname = pipeline["aws"]["rds"]["dbname"]
    user = secret["username"]
    host = secret["host"]
    password = secret["password"]

    try:

        # Connect to the database
        connection = pymysql.connect(host=host,
                                    user=user,
                                    password=password,
                                    database=dbname,
                                    cursorclass=pymysql.cursors.DictCursor)

        logger.info(f"Connected to: {dbname}")
    except:
        traceback.print_exc()
        logger.warning(f"Error connecting AWS RDS")

    return  connection

Tags: toimportawshostsecretpipelineconnectmysql