|
| 1 | +# Aurora DSQL with asyncpg |
| 2 | + |
| 3 | +This example demonstrates how to use the Aurora DSQL Python Connector with asyncpg to connect to Amazon Aurora DSQL clusters and perform basic database operations. |
| 4 | + |
| 5 | +Aurora DSQL is a distributed SQL database service that provides high availability and scalability for |
| 6 | +your PostgreSQL-compatible applications. |
| 7 | +Asyncpg is a popular PostgreSQL database library for Python that allows |
| 8 | +you to interact with PostgreSQL databases using Python code. |
| 9 | + |
| 10 | +## About the code example |
| 11 | + |
| 12 | +The example demonstrates a flexible connection approach that works for both admin and non-admin users: |
| 13 | + |
| 14 | +* When connecting as an **admin user**, the example uses the `public` schema and generates an admin authentication |
| 15 | + token. |
| 16 | +* When connecting as a **non-admin user**, the example uses a custom `myschema` schema and generates a standard |
| 17 | + authentication token. |
| 18 | + |
| 19 | +The code automatically detects the user type and adjusts its behavior accordingly. |
| 20 | + |
| 21 | +## ⚠️ Important |
| 22 | + |
| 23 | +* Running this code might result in charges to your AWS account. |
| 24 | +* We recommend that you grant your code least privilege. At most, grant only the |
| 25 | + minimum permissions required to perform the task. For more information, see |
| 26 | + [Grant least privilege](https://docs.aws.amazon.com/IAM/latest/UserGuide/best-practices.html#grant-least-privilege). |
| 27 | +* This code is not tested in every AWS Region. For more information, see |
| 28 | + [AWS Regional Services](https://aws.amazon.com/about-aws/global-infrastructure/regional-product-services). |
| 29 | + |
| 30 | + |
| 31 | +## TLS connection configuration |
| 32 | + |
| 33 | +This example uses direct TLS connections where supported, and verifies the server certificate is trusted. Verified SSL |
| 34 | +connections should be used where possible to ensure data security during transmission. |
| 35 | + |
| 36 | +* Driver versions following the release of PostgreSQL 17 support direct TLS connections, bypassing the traditional |
| 37 | + PostgreSQL connection preamble |
| 38 | +* Direct TLS connections provide improved connection performance and enhanced security |
| 39 | +* Not all PostgreSQL drivers support direct TLS connections yet, or only in recent versions following PostgreSQL 17 |
| 40 | +* Ensure your installed driver version supports direct TLS negotiation, or use a version that is at least as recent as |
| 41 | + the one used in this sample |
| 42 | +* If your driver doesn't support direct TLS connections, you may need to use the traditional preamble connection instead |
| 43 | + |
| 44 | + |
| 45 | +### Prerequisites |
| 46 | + |
| 47 | +* You must have an AWS account, and have your default credentials and AWS Region |
| 48 | + configured as described in the |
| 49 | + [Globally configuring AWS SDKs and tools](https://docs.aws.amazon.com/credref/latest/refdocs/creds-config-files.html) |
| 50 | + guide. |
| 51 | +* [Python 3.10.0](https://www.python.org/) or later. |
| 52 | +* You must have an Aurora DSQL cluster. For information about creating an Aurora DSQL cluster, see the |
| 53 | + [Getting started with Aurora DSQL](https://docs.aws.amazon.com/aurora-dsql/latest/userguide/getting-started.html) |
| 54 | + guide. |
| 55 | +* If connecting as a non-admin user, ensure the user is linked to an IAM role and is granted access to the `myschema` |
| 56 | + schema. See the |
| 57 | + [Using database roles with IAM roles](https://docs.aws.amazon.com/aurora-dsql/latest/userguide/using-database-and-iam-roles.html) |
| 58 | + guide. |
| 59 | + |
| 60 | +### Download the Amazon root certificate from the official trust store |
| 61 | + |
| 62 | +Download the Amazon root certificate from the official trust store: |
| 63 | + |
| 64 | +``` |
| 65 | +wget https://www.amazontrust.com/repository/AmazonRootCA1.pem -O root.pem |
| 66 | +``` |
| 67 | + |
| 68 | +### Set up environment for examples |
| 69 | + |
| 70 | +1. Create and activate a Python virtual environment: |
| 71 | + |
| 72 | +```bash |
| 73 | +python3 -m venv .venv |
| 74 | +source .venv/bin/activate # Linux, macOS |
| 75 | +# or |
| 76 | +.venv\Scripts\activate # Windows |
| 77 | +``` |
| 78 | + |
| 79 | +2. Install the required packages for running the examples: |
| 80 | + |
| 81 | +```bash |
| 82 | +pip install -r requirements.txt |
| 83 | +``` |
| 84 | + |
| 85 | +### Run the code |
| 86 | + |
| 87 | +#### What the Examples Do |
| 88 | + |
| 89 | +The example demonstrates the following operations: |
| 90 | + |
| 91 | +- Opening a connection to an Aurora DSQL cluster |
| 92 | +- Creating a table |
| 93 | +- Inserting and querying data |
| 94 | + |
| 95 | +The example is designed to work with both admin and non-admin users: |
| 96 | + |
| 97 | +- When run as an admin user, it uses the `public` schema |
| 98 | +- When run as a non-admin user, it uses the `myschema` schema |
| 99 | + |
| 100 | +**Note:** running the example will use actual resources in your AWS account and may incur charges. |
| 101 | + |
| 102 | +The connection pool examples demonstrate: |
| 103 | +- Creating a connection pool for Aurora DSQL |
| 104 | +- Using async context managers for connection management |
| 105 | +- Performing database operations through the pool |
| 106 | +- Running multiple concurrent database operations |
| 107 | +- Using asyncio.gather() for parallel execution |
| 108 | +- Proper resource management with connection pools |
| 109 | + |
| 110 | +#### Environment Cluster Details |
| 111 | + |
| 112 | +Set environment variables for your cluster details: |
| 113 | + |
| 114 | +```bash |
| 115 | +# e.g. "admin" |
| 116 | +export CLUSTER_USER="<your user>" |
| 117 | + |
| 118 | +# e.g. "foo0bar1baz2quux3quuux4.dsql.us-east-1.on.aws" |
| 119 | +export CLUSTER_ENDPOINT="<your endpoint>" |
| 120 | + |
| 121 | +# e.g. "us-east-1" |
| 122 | +export REGION="<your region>" |
| 123 | +``` |
| 124 | + |
| 125 | +#### Run the example: |
| 126 | + |
| 127 | +```bash |
| 128 | +# Run example directly |
| 129 | +python src/example.py |
| 130 | + |
| 131 | +# Run example using pytest |
| 132 | +pytest ./test/test_example.py |
| 133 | + |
| 134 | +# Run all using pytest |
| 135 | +pytest ./test |
| 136 | +``` |
| 137 | + |
| 138 | +The examples contain comments explaining the code and the operations being performed. |
| 139 | + |
| 140 | +## Additional resources |
| 141 | + |
| 142 | +* [Amazon Aurora DSQL Documentation](https://docs.aws.amazon.com/aurora-dsql/latest/userguide/what-is-aurora-dsql.html) |
| 143 | +* [Amazon Aurora DSQL Python Connector Documentation](https://docs.aws.amazon.com/aurora-dsql/latest/userguide/SECTION_program-with-dsql-connector-for-python.html) |
| 144 | +* [Asyncpg Documentation](https://magicstack.github.io/asyncpg/current/) |
| 145 | +* [AWS SDK for Python (Boto3) Documentation](https://boto3.amazonaws.com/v1/documentation/api/latest/index.html) |
| 146 | + |
| 147 | +--- |
| 148 | + |
| 149 | +Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 150 | + |
| 151 | +SPDX-License-Identifier: MIT-0 |
0 commit comments