sqlalchemy-aurora-data-api


Namesqlalchemy-aurora-data-api JSON
Version 0.5.0 PyPI version JSON
download
home_pagehttps://github.com/chanzuckerberg/sqlalchemy-aurora-data-api
SummaryAn AWS Aurora Serverless Data API dialect for SQLAlchemy
upload_time2023-12-30 00:43:20
maintainer
docs_urlNone
authorAndrey Kislyuk
requires_python
licenseApache Software License
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            sqlalchemy-aurora-data-api - An AWS Aurora Serverless Data API dialect for SQLAlchemy
=====================================================================================

This package provides a `SQLAlchemy <https://www.sqlalchemy.org>`_
`dialect <https://docs.sqlalchemy.org/en/13/dialects/>`_ for accessing PostgreSQL and MySQL databases via the
`AWS Aurora Data API <https://docs.aws.amazon.com/AmazonRDS/latest/AuroraUserGuide/data-api.html>`_.

Installation
------------
::

    pip install sqlalchemy-aurora-data-api

Prerequisites
-------------
* Set up an
  `AWS Aurora Serverless cluster <https://docs.aws.amazon.com/AmazonRDS/latest/AuroraUserGuide/aurora-serverless.html>`_
  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 <https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-welcome.html>`_ 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 <https://docs.aws.amazon.com/secretsmanager/latest/userguide/intro.html>`_ using a format
  expected by the Data API (a JSON object with the keys ``username`` and ``password``)::

      aws secretsmanager create-secret --name 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 <https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-configure.html>`_.
  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 <https://console.aws.amazon.com/rds/home#databases:>`_ (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
-----

The package registers two SQLAlchemy dialects, ``mysql+auroradataapi://`` and ``postgresql+auroradataapi://``. Two
``sqlalchemy.create_engine()`` `connect_args <https://docs.sqlalchemy.org/en/13/core/engines.html#custom-dbapi-args>`_
keyword arguments are required to connect to the database:

* ``aurora_cluster_arn`` (also referred to as ``resourceArn`` in the
  `Data API documentation <https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/rds-data.html>`_)

  * 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

All connection string contents other than the protocol (dialect) and the database name (path component, ``my_db_name``
in the example below) are ignored.

.. code-block:: python

    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:rds-db-credentials/MY_DB"

    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)

Motivation
----------
The `RDS Data API <https://docs.aws.amazon.com/AmazonRDS/latest/AuroraUserGuide/data-api.html>`_ 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 <https://docs.aws.amazon.com/lambda/latest/dg/running-lambda-code.html>`_, 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

Debugging
---------

This package uses standard Python logging conventions. To enable debug output, set the package log level to DEBUG::

    logging.basicConfig()

    logging.getLogger("aurora_data_api").setLevel(logging.DEBUG)

Links
-----
* `Project home page (GitHub) <https://github.com/chanzuckerberg/sqlalchemy-aurora-data-api>`_
* `Documentation (Read the Docs) <https://sqlalchemy-aurora-data-api.readthedocs.io/en/latest/>`_
* `Package distribution (PyPI) <https://pypi.python.org/pypi/sqlalchemy-aurora-data-api>`_
* `Change log <https://github.com/chanzuckerberg/sqlalchemy-aurora-data-api/blob/master/Changes.rst>`_
* `aurora-data-api <https://github.com/chanzuckerberg/aurora-data-api>`_, the Python DB-API 2.0 client that
  sqlalchemy-aurora-data-api depends on

Bugs
~~~~
Please report bugs, issues, feature requests, etc. on
`GitHub <https://github.com/chanzuckerberg/sqlalchemy-aurora-data-api/issues>`_.

License
-------
Licensed under the terms of the `Apache License, Version 2.0 <http://www.apache.org/licenses/LICENSE-2.0>`_.

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

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/chanzuckerberg/sqlalchemy-aurora-data-api",
    "name": "sqlalchemy-aurora-data-api",
    "maintainer": "",
    "docs_url": null,
    "requires_python": "",
    "maintainer_email": "",
    "keywords": "",
    "author": "Andrey Kislyuk",
    "author_email": "kislyuk@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/6b/36/667d3ba5f6ee5d77458f2e23ec7d673eccbdc1532e9c133c725933f44863/sqlalchemy-aurora-data-api-0.5.0.tar.gz",
    "platform": "MacOS X",
    "description": "sqlalchemy-aurora-data-api - An AWS Aurora Serverless Data API dialect for SQLAlchemy\n=====================================================================================\n\nThis package provides a `SQLAlchemy <https://www.sqlalchemy.org>`_\n`dialect <https://docs.sqlalchemy.org/en/13/dialects/>`_ for accessing PostgreSQL and MySQL databases via the\n`AWS Aurora Data API <https://docs.aws.amazon.com/AmazonRDS/latest/AuroraUserGuide/data-api.html>`_.\n\nInstallation\n------------\n::\n\n    pip install sqlalchemy-aurora-data-api\n\nPrerequisites\n-------------\n* Set up an\n  `AWS Aurora Serverless cluster <https://docs.aws.amazon.com/AmazonRDS/latest/AuroraUserGuide/aurora-serverless.html>`_\n  and enable Data API access for it. If you have previously set up an Aurora Serverless cluster, you can enable Data API\n  with the following `AWS CLI <https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-welcome.html>`_ command::\n\n      aws rds modify-db-cluster --db-cluster-identifier DB_CLUSTER_NAME --enable-http-endpoint --apply-immediately\n\n* Save the database credentials in\n  `AWS Secrets Manager <https://docs.aws.amazon.com/secretsmanager/latest/userguide/intro.html>`_ using a format\n  expected by the Data API (a JSON object with the keys ``username`` and ``password``)::\n\n      aws secretsmanager create-secret --name rds-db-credentials/MY_DB\n      aws secretsmanager put-secret-value --secret-id rds-db-credentials/MY_DB --secret-string \"$(jq -n '.username=env.PGUSER | .password=env.PGPASSWORD')\"\n\n* Configure your AWS command line credentials using\n  `standard AWS conventions <https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-configure.html>`_.\n  You can verify that everything works correctly by running a test query via the AWS CLI::\n\n      aws rds-data execute-statement --resource-arn RESOURCE_ARN --secret-arn SECRET_ARN --sql \"select * from pg_catalog.pg_tables\"\n\n  * Here, RESOURCE_ARN refers to the Aurora RDS database ARN, which can be found in the\n    `AWS RDS Console <https://console.aws.amazon.com/rds/home#databases:>`_ (click on your database, then \"Configuration\")\n    or in the CLI by running ``aws rds describe-db-clusters``. SECRET_ARN refers to the AWS Secrets Manager secret\n    created above.\n\n  * When running deployed code (on an EC2 instance, ECS/EKS container, or Lambda), you can use the managed IAM policy\n    **AmazonRDSDataFullAccess** to grant your IAM role permissions to access the RDS Data API (while this policy is\n    convenient for testing, we recommend that you create your own scoped down least-privilege policy for production\n    applications).\n\nUsage\n-----\n\nThe package registers two SQLAlchemy dialects, ``mysql+auroradataapi://`` and ``postgresql+auroradataapi://``. Two\n``sqlalchemy.create_engine()`` `connect_args <https://docs.sqlalchemy.org/en/13/core/engines.html#custom-dbapi-args>`_\nkeyword arguments are required to connect to the database:\n\n* ``aurora_cluster_arn`` (also referred to as ``resourceArn`` in the\n  `Data API documentation <https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/rds-data.html>`_)\n\n  * If not given as a keyword argument, this can also be specified using the ``AURORA_CLUSTER_ARN`` environment variable\n\n* ``secret_arn`` (the database credentials secret)\n\n  * If not given as a keyword argument, this can also be specified using the ``AURORA_SECRET_ARN`` environment variable\n\nAll connection string contents other than the protocol (dialect) and the database name (path component, ``my_db_name``\nin the example below) are ignored.\n\n.. code-block:: python\n\n    from sqlalchemy import create_engine\n\n    cluster_arn = \"arn:aws:rds:us-east-1:123456789012:cluster:my-aurora-serverless-cluster\"\n    secret_arn = \"arn:aws:secretsmanager:us-east-1:123456789012:secret:rds-db-credentials/MY_DB\"\n\n    engine = create_engine('postgresql+auroradataapi://:@/my_db_name',\n                           echo=True,\n                           connect_args=dict(aurora_cluster_arn=cluster_arn, secret_arn=secret_arn))\n\n    with engine.connect() as conn:\n        for result in conn.execute(\"select * from pg_catalog.pg_tables\"):\n            print(result)\n\nMotivation\n----------\nThe `RDS Data API <https://docs.aws.amazon.com/AmazonRDS/latest/AuroraUserGuide/data-api.html>`_ is the link between the\nAWS Lambda serverless environment and the sophisticated features provided by PostgreSQL and MySQL. The Data API tunnels\nSQL over HTTP, which has advantages in the context of AWS Lambda:\n\n* It eliminates the need to open database ports to the AWS Lambda public IP address pool\n* It uses stateless HTTP connections instead of stateful internal TCP connection pools used by most database drivers\n  (the stateful pools become invalid after going through\n  `AWS Lambda freeze-thaw cycles <https://docs.aws.amazon.com/lambda/latest/dg/running-lambda-code.html>`_, causing\n  connection errors and burdening the database server with abandoned invalid connections)\n* It uses AWS role-based authentication, eliminating the need for the Lambda to handle database credentials directly\n\nDebugging\n---------\n\nThis package uses standard Python logging conventions. To enable debug output, set the package log level to DEBUG::\n\n    logging.basicConfig()\n\n    logging.getLogger(\"aurora_data_api\").setLevel(logging.DEBUG)\n\nLinks\n-----\n* `Project home page (GitHub) <https://github.com/chanzuckerberg/sqlalchemy-aurora-data-api>`_\n* `Documentation (Read the Docs) <https://sqlalchemy-aurora-data-api.readthedocs.io/en/latest/>`_\n* `Package distribution (PyPI) <https://pypi.python.org/pypi/sqlalchemy-aurora-data-api>`_\n* `Change log <https://github.com/chanzuckerberg/sqlalchemy-aurora-data-api/blob/master/Changes.rst>`_\n* `aurora-data-api <https://github.com/chanzuckerberg/aurora-data-api>`_, the Python DB-API 2.0 client that\n  sqlalchemy-aurora-data-api depends on\n\nBugs\n~~~~\nPlease report bugs, issues, feature requests, etc. on\n`GitHub <https://github.com/chanzuckerberg/sqlalchemy-aurora-data-api/issues>`_.\n\nLicense\n-------\nLicensed under the terms of the `Apache License, Version 2.0 <http://www.apache.org/licenses/LICENSE-2.0>`_.\n\n.. image:: https://travis-ci.org/chanzuckerberg/sqlalchemy-aurora-data-api.png\n        :target: https://travis-ci.org/chanzuckerberg/sqlalchemy-aurora-data-api\n.. image:: https://codecov.io/github/chanzuckerberg/sqlalchemy-aurora-data-api/coverage.svg?branch=master\n        :target: https://codecov.io/github/chanzuckerberg/sqlalchemy-aurora-data-api?branch=master\n.. image:: https://img.shields.io/pypi/v/sqlalchemy-aurora-data-api.svg\n        :target: https://pypi.python.org/pypi/sqlalchemy-aurora-data-api\n.. image:: https://img.shields.io/pypi/l/sqlalchemy-aurora-data-api.svg\n        :target: https://pypi.python.org/pypi/sqlalchemy-aurora-data-api\n.. image:: https://readthedocs.org/projects/sqlalchemy-aurora-data-api/badge/?version=latest\n        :target: https://sqlalchemy-aurora-data-api.readthedocs.org/\n",
    "bugtrack_url": null,
    "license": "Apache Software License",
    "summary": "An AWS Aurora Serverless Data API dialect for SQLAlchemy",
    "version": "0.5.0",
    "project_urls": {
        "Homepage": "https://github.com/chanzuckerberg/sqlalchemy-aurora-data-api"
    },
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "c219bbc016ecbce8ed9c5d15baa289636f5217f52c81ff72212e089d458f8edf",
                "md5": "dd27253cb9001e4c29a50cd1d28776c1",
                "sha256": "dbdc2bf9da50d0e2d7d75f242536342bf349927c888c3d9c773b7df75af4f3f1"
            },
            "downloads": -1,
            "filename": "sqlalchemy_aurora_data_api-0.5.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "dd27253cb9001e4c29a50cd1d28776c1",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": null,
            "size": 10233,
            "upload_time": "2023-12-30T00:43:18",
            "upload_time_iso_8601": "2023-12-30T00:43:18.460056Z",
            "url": "https://files.pythonhosted.org/packages/c2/19/bbc016ecbce8ed9c5d15baa289636f5217f52c81ff72212e089d458f8edf/sqlalchemy_aurora_data_api-0.5.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "6b36667d3ba5f6ee5d77458f2e23ec7d673eccbdc1532e9c133c725933f44863",
                "md5": "56410969a4e7e027a6daad8dd7da62ad",
                "sha256": "77190b04eb8e9f7e89daaaf61fdf87b6f5bf0a29cfc80ebec6f8a616f863b34b"
            },
            "downloads": -1,
            "filename": "sqlalchemy-aurora-data-api-0.5.0.tar.gz",
            "has_sig": false,
            "md5_digest": "56410969a4e7e027a6daad8dd7da62ad",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 12457,
            "upload_time": "2023-12-30T00:43:20",
            "upload_time_iso_8601": "2023-12-30T00:43:20.217213Z",
            "url": "https://files.pythonhosted.org/packages/6b/36/667d3ba5f6ee5d77458f2e23ec7d673eccbdc1532e9c133c725933f44863/sqlalchemy-aurora-data-api-0.5.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-12-30 00:43:20",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "chanzuckerberg",
    "github_project": "sqlalchemy-aurora-data-api",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "sqlalchemy-aurora-data-api"
}
        
Elapsed time: 0.18498s