Skip to main content

A Python DB-API 2.0 client for the AWS Aurora Serverless Data API

Project description

Installation

pip install aurora-data-api

Prerequisites

  • Set up an AWS Aurora Serverless cluster and enable Data API access for it. If you have previously set up an Aurora Serverless cluster, you can enable Data API with the following AWS CLI command:

    aws rds modify-db-cluster --db-cluster-identifier DB_CLUSTER_NAME --enable-http-endpoint --apply-immediately
  • Save the database credentials in AWS Secrets Manager using a format expected by the Data API (a JSON object with the keys username and password):

    aws secretsmanager create-secret --secret-id rds-db-credentials/MY_DB
    aws secretsmanager put-secret-value --secret-id rds-db-credentials/MY_DB --secret-string "$(jq -n '.username=env.PGUSER | .password=env.PGPASSWORD')"
  • Configure your AWS command line credentials using standard AWS conventions. You can verify that everything works correctly by running a test query via the AWS CLI:

    aws rds-data execute-statement --resource-arn RESOURCE_ARN --secret-arn SECRET_ARN --sql "select * from pg_catalog.pg_tables"
    • Here, RESOURCE_ARN refers to the Aurora RDS database ARN, which can be found in the AWS RDS Console (click on your database, then “Configuration”) or in the CLI by running aws rds describe-db-clusters. SECRET_ARN refers to the AWS Secrets Manager secret created above.

    • When running deployed code (on an EC2 instance, ECS/EKS container, or Lambda), you can use the managed IAM policy AmazonRDSDataFullAccess to grant your IAM role permissions to access the RDS Data API (while this policy is convenient for testing, we recommend that you create your own scoped down least-privilege policy for production applications).

Usage

Use this module as you would use any DB-API compatible driver module. The aurora_data_api.connect() method is the standard main entry point, and accepts two implementation-specific keyword arguments:

  • aurora_cluster_arn (also referred to as resourceArn in the Data API documentation)

    • If not given as a keyword argument, this can also be specified using the AURORA_CLUSTER_ARN environment variable

  • secret_arn (the database credentials secret)

    • If not given as a keyword argument, this can also be specified using the AURORA_SECRET_ARN environment variable

import aurora_data_api

cluster_arn = "arn:aws:rds:us-east-1:123456789012:cluster:my-aurora-serverless-cluster"
secret_arn = "arn:aws:secretsmanager:us-east-1:123456789012:secret:rds-db-credentials/MY_DB"
with aurora_data_api.connect(aurora_cluster_arn=cluster_arn, secret_arn=secret_arn, database="my_db") as conn:
    with conn.cursor() as cursor:
        cursor.execute("select * from pg_catalog.pg_tables")
        print(cursor.fetchall())

The cursor supports iteration (and automatically wraps the query in a server-side cursor and paginates it if required):

with conn.cursor() as cursor:
    for row in cursor.execute("select * from pg_catalog.pg_tables"):
        print(row)

Motivation

The RDS Data API is the link between the AWS Lambda serverless environment and the sophisticated features provided by PostgreSQL and MySQL. The Data API tunnels SQL over HTTP, which has advantages in the context of AWS Lambda:

  • It eliminates the need to open database ports to the AWS Lambda public IP address pool

  • It uses stateless HTTP connections instead of stateful internal TCP connection pools used by most database drivers (the stateful pools become invalid after going through AWS Lambda freeze-thaw cycles, causing connection errors and burdening the database server with abandoned invalid connections)

  • It uses AWS role-based authentication, eliminating the need for the Lambda to handle database credentials directly

License

Licensed under the terms of the Apache License, Version 2.0.

https://travis-ci.org/chanzuckerberg/aurora-data-api.png https://codecov.io/github/chanzuckerberg/aurora-data-api/coverage.svg?branch=master https://img.shields.io/pypi/v/aurora-data-api.svg https://img.shields.io/pypi/l/aurora-data-api.svg https://readthedocs.org/projects/aurora-data-api/badge/?version=latest

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

aurora-data-api-0.2.3.tar.gz (21.0 kB view details)

Uploaded Source

Built Distribution

aurora_data_api-0.2.3-py2.py3-none-any.whl (21.0 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file aurora-data-api-0.2.3.tar.gz.

File metadata

  • Download URL: aurora-data-api-0.2.3.tar.gz
  • Upload date:
  • Size: 21.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/46.0.0 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.7.9

File hashes

Hashes for aurora-data-api-0.2.3.tar.gz
Algorithm Hash digest
SHA256 fb4d8ac1722968e946a8a3c3d240402946e90b5b84e0adff46a15bb0bdea55d9
MD5 b2ebe08dcb9cb03d6ec66f991362008b
BLAKE2b-256 3e8e0067ef17e65f9bbfd101f36173a816bc4ff667f91d6f00111d33e32a18a0

See more details on using hashes here.

File details

Details for the file aurora_data_api-0.2.3-py2.py3-none-any.whl.

File metadata

  • Download URL: aurora_data_api-0.2.3-py2.py3-none-any.whl
  • Upload date:
  • Size: 21.0 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/46.0.0 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.7.9

File hashes

Hashes for aurora_data_api-0.2.3-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 1b56f9229354e701b6c1189d2ec9bc630a5faabf4e8c112c071a91dacb5f82f6
MD5 fbe7b9db0a144ae26032827fa2c96bfd
BLAKE2b-256 f84ab5d88c69c942fd30541b6bd7ac9d2a2651612056eb31cdff3ad4d4d61f86

See more details on using hashes here.

Supported by

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