Python Connect to AWS Aurora Serverless MySQL Using SQLAlchemy
Asked Answered
D

2

8

Is there a way to specify my connection with SQLAlchemy to an AWS RDS Aurora Serverless MySQL database instance without a Secrets Manager ARN? I have the database username, password, endpoint, ARN, etc., and ideally I would initialize an engine, then use df.to_sql() to load a DataFrame into a table on the Aurora instance.

...
else:
   engine = create_engine([WHAT DO I SPECIFY HERE?])
   with engine.connect() as conn:
      df.to_sql([CODE TO APPEND TO EXISTING TABLE HERE])...
Dominic answered 17/10, 2019 at 22:7 Comment(2)
Did you create a secret in aws secrets manager that contains the database username, password, etc?Dimitrovo
Yep! Have a secret ARN as well.Dominic
D
4

From Alchemy documentation - https://docs.sqlalchemy.org/en/13/dialects/mysql.html, this is what the connect string should look like - the parameter in create_engine - for MySQL

mysql+pymysql://<username>:<password>@<host>/<dbname>[?<options>]
Dimitrovo answered 21/10, 2019 at 1:20 Comment(2)
Thanks for your reply -- in the context of an AWS RDS Aurora Serverless MySQL instance, how would this look? <username> = database instance username, <password> = database instance password, <host> = database instance endpoint?, what are options? Why do I need pymysql in the prefix?Dominic
I think you need pymysql because sqlalchemy is a Python SQL toolkit and pymysql is the python mysql clientDimitrovo
T
3

I don't know if you can connect to Aurora without a secrets ARN, but if you are willing to use one, you could install a driver like this: https://github.com/koxudaxi/py-data-api

Which would allow you to do something like the following:

def example_driver_for_sqlalchemy():
    from sqlalchemy.engine import create_engine
    engine = create_engine(
        'mysql+pydataapi://',
        connect_args={
            'resource_arn': 'arn:aws:rds:us-east-1:123456789012:cluster:dummy',
            'secret_arn': 'arn:aws:secretsmanager:us-east-1:123456789012:secret:dummy',
            'database': 'test'
        }
    )
    result: ResultProxy = engine.execute("select * from pets")
    print(result.fetchall())
Tabor answered 20/11, 2019 at 19:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.