Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Connect to AWS Aurora Serverless MySQL Using SQLAlchemy

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])...
like image 626
OJT Avatar asked Oct 17 '19 22:10

OJT


2 Answers

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>]
like image 75
committedandroider Avatar answered Sep 24 '22 19:09

committedandroider


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())
like image 27
Baruch Spinoza Avatar answered Sep 23 '22 19:09

Baruch Spinoza