SQLAlchemy dialects to connect to Amazon RDS instances with IAM authentication
Slowing down. Last release over 3 years ago, little activity since. Fine for existing code; think twice for a new project.
pip install sqlalchemy-rdsiamsqlalchemy-rdsiam
SQLAlchemy dialect to connect to Amazon RDS instances with IAM authentication.
The following are supported:
- Amazon RDS PostgreSQL, with
psycopg2. - Amazon RDS PostgreSQL, with
asyncpg.
SQLAlchemy 1.3, 1.4 and 2.0 are supported.
Background
Amazon RDS is managed database service on AWS, which provides the ability to connect to database instances with IAM authentication instead of passwords. AWS SDKs or the AWS CLI can be used to generate a connection token, valid for 15 minutes, and based on an Amazon IAM identity.
With SQLAlchemy, it is possible to use IAM authentication using one of the following options:
- Using an event handler and the
do_connectevent. - Using a custom connection factory.
Both options require modifying the codebase to either inject the event handler or the custom connection factory. With many open-source tools, this requires maintaining a fork. This repository provides a set of dialects which can be installed and used directly in any SQLAlchemy codebase instead.
Getting Started
-
Install the Python package:
pip install sqlalchemy-rdsiam -
Use a connection string with scheme corresponding to the target PostgreSQL library, and leave out the password. For instance:
postgresql+psycopg2rdsiam://username@host/dbname postgresql+asyncpgrdsiam://username@host/dbnameNote: if a password is provided, it will be ignored.
-
Run with an IAM identity that has IAM permissions to connect to the database. See IAM authentication.
Additional Configuration
AWS Region
The default region in the environment is used. To access a database in a different region without changing your environment, pass the query parameter in the connection string: