简体   繁体   中英

Is it possible to use SQLAlchemy with AWS RDS Data API?

AWS Recently launched the Data API . This simplifies creating Lambda functions, eliminating the necessity for additional complexity by allowing API calls, instead of direct database connections.

I'm trying to use SQLAlchemy in an AWS Lambda Function, and I'd really like to take advantage of this new API.

Does anyone know if there is any support for this, or if support for this is coming?

Alternatively, how difficult would it be to create a new Engine to support this?

SQLAlchemy calls database drivers "dialects" . So if you're using SQLAlchemy with PostgreSQL and using psycopg2 as the driver, then you're using the psycopg2 dialect of PostgreSQL.

I was looking for the same thing as you, and found no existing solution, so I wrote my own and published it. To use the AWS Aurora RDS Data API, I created a SQL dialect package for it, sqlalchemy-aurora-data-api . This in turn required me to write a DB-API compatible Python DB driver for Aurora Data API, aurora-data-api . After installing with pip install sqlalchemy-aurora-data-api , you can use it like this:

from sqlalchemy import create_engine

cluster_arn = "arn:aws:rds:us-east-1:123456789012:cluster:my-aurora-serverless-cluster"
secret_arn = "arn:aws:secretsmanager:us-east-1:123456789012:secret:MY_DB_CREDENTIALS"

engine = create_engine('postgresql+auroradataapi://:@/my_db_name',
                       echo=True,
                       connect_args=dict(aurora_cluster_arn=cluster_arn, secret_arn=secret_arn))

with engine.connect() as conn:
    for result in conn.execute("select * from pg_catalog.pg_tables"):
        print(result)

作为替代方案,如果你想要更像 Records 的东西,你可以尝试 Camus https://github.com/rizidoro/camus

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM