2

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?

jtoberon
  • 7,505
  • 1
  • 31
  • 44
naydichev
  • 4,392
  • 1
  • 15
  • 13
  • Looks like one option is to create something that adheres to Python's [DBAPI](https://www.python.org/dev/peps/pep-0249/).... – naydichev Jun 08 '19 at 16:37
  • It seems that someone is doing some progress on the matter https://github.com/koxudaxi/py-data-api I'll try to give it a try... – jarias Aug 13 '19 at 09:51

2 Answers2

5

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)
weaver
  • 1,389
  • 12
  • 15
0

As an alternative, if you want something more like Records, you can try Camus https://github.com/rizidoro/camus.

rizidoro
  • 11,367
  • 16
  • 53
  • 84