Home:ALL Converter>How to connect from lambda to AWS RDS Proxy in python?

How to connect from lambda to AWS RDS Proxy in python?

Ask Time:2021-07-21T23:05:35         Author:ambigus9

Json Formatter

I try to connect to a MySQL RDS database and getting a timeout error:

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

The AWS Lambda can connect to AWS RDS Secrets, but looks like no with AWS RDS Proxy. The lambda in a VPC. Here is the code i using on 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

Author:ambigus9,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/68471986/how-to-connect-from-lambda-to-aws-rds-proxy-in-python
yy