Skip to main content

SQLAlchemy dialects to connect to Amazon RDS instances with IAM authentication

Project description

sqlalchemy-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:

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/dbname
    

    Note: 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 aws_region_name in the connection string:

postgresql+psycopg2rdsiam://username@host/dbname?aws_region_name=us-east-2

Creating the Database If It Doesn't Exists

The dialect supports optionally creating the database upon connection if it doesn't exist. This is disabled by default. To create the database if it doesn't exist, set the query parameter create_db_if_not_exists to true:

postgresql+psycopg2rdsiam://username@host/dbname?create_db_if_not_exists=true

Note: the role used must have permissions to create databases.

Set sslrootcert to the Amazon RDS Certificate Bundle

Amazon RDS TLS certificates are signed by Amazon certificate authorities, and the sslrootcert PostgreSQL argument must be used in order to verify the certificate chain when connecting to the instance. In some cases, it can be useful to directly get the CA bundle along with the package for testing, or to streaming provisioning. To this end, the CA bundle is automatically downloaded when installing the Python package, and you can opt-in to use it directly.

Note: make sure this is in line with your security posture requirements first.

The package can directly set sslrootcert to the certificate bundle for all Amazon RDS regions. This is disabled by default. To do so, set the query parameter rds_sslrootcert to true:

postgresql+psycopg2rdsiam://username@host/dbname?rds_sslrootcert=true

You still need to set sslmode - for instance, with sslmode=verify-full:

postgresql+psycopg2rdsiam://username@host/dbname?rds_sslrootcert=true&sslmode=verify-full

See SSL Support for additional details.

Contributing

See Contributing.

License

See License.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sqlalchemy-rdsiam-1.0.1.tar.gz (18.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

sqlalchemy_rdsiam-1.0.1-py2.py3-none-any.whl (93.6 kB view details)

Uploaded Python 2Python 3

File details

Details for the file sqlalchemy-rdsiam-1.0.1.tar.gz.

File metadata

  • Download URL: sqlalchemy-rdsiam-1.0.1.tar.gz
  • Upload date:
  • Size: 18.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.11.1

File hashes

Hashes for sqlalchemy-rdsiam-1.0.1.tar.gz
Algorithm Hash digest
SHA256 f09ce4c0b867e6251ff167dff689df77d4721a9ad91097a2fea4ebf09bb1799b
MD5 6d35d032af9a5e8e8458b0cf49dc3f6c
BLAKE2b-256 0079e22eb66b4f2950f36d11c2e021115d53a6049857fb26f3f9fe6cfe7a982c

See more details on using hashes here.

File details

Details for the file sqlalchemy_rdsiam-1.0.1-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for sqlalchemy_rdsiam-1.0.1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 4cb09c37382733217bb8ec837afa058df336b37a8e2a8fe9526abca16bd34d36
MD5 f14bd1e047efd70b02286f0232a67497
BLAKE2b-256 e1fa56e3f637d4ed8ab9a929e02173197790023690b8d5bf3f36cb75eed4ae51

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page