How to generate python db model with SQLAlchemy for Aurora Serverless and DataAPI?

0

I've been trying to figure out if there's any way to have ORM functionalities (especially code model generation) for an Aurora Serverless (Postgresql) DB that's used through DataAPI ? Ultimately, I want to avoid using raw sql string queries in our Lambdas.

I tried using sqlacodegen combined with sqlalchemy-aurora-data-api (which works on top of sqlalchemy) but I keep getting errors:

With dialect:

> sqlacodegen postgresql+auroradataapi://username:password@db-host/db-name

botocore.exceptions.NoRegionError: You must specify a region.

Without dialect:

> sqlacodegen postgresql://username:password@db-host/db-name

sqlalchemy.exc.OperationalError: (psycopg2.OperationalError) could not connect to server: Connection timed out (0x0000274C/10060)

The former seems to hint that parameters must be passed to the call, but sqlacodegen doesn't take any kwargs afaik. The latter just fails to connect and the psycopg2 tells me it's just not using the proper dialect anyway.

python
postgresql
orm
sqlalchemy
aws-aurora-serverless
asked on Stack Overflow Dec 16, 2020 by Mat • edited Dec 22, 2020 by Mat

1 Answer

0

None of these two calls are correct. Here is the right way to call it:

> sqlacodegen postgresql+auroradataapi://:@/db-name

Two main points here:

  1. The error indicating You must specify a region is correct. The way to specify that region when using command line scripts (such as sqlacodegen) is to use Environment Variables. This section of the boto3 documentation mentions all the variables and their usage.

  2. The specific library (sqlalchemy-aurora-data-api) used for DataAPI uses such a format in their example, and it works fine as is despite being mixed with sqlacodegen.

For this problem, the AWS_DEFAULT_REGION variable should be set to the appropriate value (i.e. us-east-1), along with all the necessary AWS credentials (like AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY).

answered on Stack Overflow Dec 22, 2020 by Mat • edited Dec 22, 2020 by Mat

User contributions licensed under CC BY-SA 3.0