Skip to main content

capo-dsql: typed, async Python SDK for AWS Dsql

capo-dsql is a typed Python SDK for Amazon Aurora DSQL: generate the IAM authentication token that is the password of a DSQL connection, and create and manage clusters. AsyncDSQLClient works with asyncio and trio, and DSQLClient has the same methods for sync code. Requests and responses are typed with TypedDicts, so your editor and type checker know every field.

It is part of capo, a community-driven alternative to boto3 and aioboto3, built on its own HTTP stack instead of botocore and generated from the official AWS Smithy models. Documentation: https://capo-sdk.dev/services/dsql/

Install capo-dsql

pip install capo-dsql

Connect to Aurora DSQL with an IAM auth token

import asyncpg
from capo_dsql import AsyncDSQLClient

HOST = "abc123.dsql.us-east-1.on.aws"


async def main():
    async with AsyncDSQLClient(region="us-east-1") as dsql:
        # The token is the password of the admin role. It is signed locally with
        # the client's credentials, so nothing is sent to AWS
        token = await dsql.generate_db_connect_admin_auth_token(hostname=HOST)

    conn = await asyncpg.connect(
        host=HOST, user="admin", password=token, database="postgres", ssl="require"
    )
    print(await conn.fetchval("SELECT current_user"))
    await conn.close()
import psycopg
from capo_dsql import DSQLClient

HOST = "abc123.dsql.us-east-1.on.aws"


def main():
    # The sync client: the same method, without async and await
    with DSQLClient(region="us-east-1") as dsql:
        token = dsql.generate_db_connect_admin_auth_token(hostname=HOST)

    with psycopg.connect(
        host=HOST, user="admin", password=token, dbname="postgres", sslmode="require"
    ) as conn:
        print(conn.execute("SELECT current_user").fetchone())

Connect to Aurora DSQL as your own database role

Connected as admin, create the role once and say which IAM identity may log in as it: CREATE ROLE app WITH LOGIN and AWS IAM GRANT app TO 'arn:aws:iam::123456789012:role/app'. Its tokens come from generate_db_connect_auth_token.

import asyncpg
from capo_dsql import AsyncDSQLClient

HOST = "abc123.dsql.us-east-1.on.aws"


async def main():
    async with AsyncDSQLClient(region="us-east-1") as dsql:
        # Any role but admin takes a DbConnect token, here valid for an hour
        # instead of the default 15 minutes
        token = await dsql.generate_db_connect_auth_token(hostname=HOST, expires_in=3600)

    conn = await asyncpg.connect(
        host=HOST, user="app", password=token, database="postgres", ssl="require"
    )
    await conn.close()

Use an Aurora DSQL connection pool with IAM tokens

import asyncpg
from capo_dsql import AsyncDSQLClient

HOST = "abc123.dsql.us-east-1.on.aws"


async def main():
    async with AsyncDSQLClient(region="us-east-1") as dsql:
        # A token only has to be valid when a connection opens. asyncpg calls
        # password for every new connection, so each one gets a fresh token
        async with asyncpg.create_pool(
            host=HOST,
            user="admin",
            database="postgres",
            ssl="require",
            password=lambda: dsql.generate_db_connect_admin_auth_token(hostname=HOST),
        ) as pool:
            print(await pool.fetchval("SELECT current_user"))

Create an Aurora DSQL cluster

from capo_dsql import AsyncDSQLClient


async def main():
    async with AsyncDSQLClient(region="us-east-1") as dsql:
        cluster = await dsql.create_cluster(deletion_protection_enabled=False, tags={"Name": "my-cluster"})
        print(cluster["identifier"], cluster["status"])

        # Its endpoint is the hostname to connect to
        cluster = await dsql.get_cluster(cluster["identifier"])
        print(cluster.get("endpoint"))

List and delete Aurora DSQL clusters

from capo_dsql import AsyncDSQLClient


async def main():
    async with AsyncDSQLClient(region="us-east-1") as dsql:
        # Every cluster, however many pages that takes
        async for cluster in dsql.iter_list_clusters():
            print(cluster["identifier"], cluster["arn"])

        # Delete a cluster and wait until it is gone
        await dsql.delete_cluster("abc123")
        await dsql.wait_until_cluster_not_exists("abc123", max_wait_time=600)

Handle Aurora DSQL errors

from capo_dsql import AsyncDSQLClient
from capo_dsql.errors import ResourceNotFoundException, ServiceError


async def main():
    async with AsyncDSQLClient(region="us-east-1") as dsql:
        # An error the API describes has its own class
        try:
            await dsql.get_cluster("no-such-cluster")
        except ResourceNotFoundException as e:
            print(e.code, e.message)

        # Any error DSQL returns; one without a class of its own is an UnknownServiceError
        except ServiceError as e:
            print(e.code, e.message)

Configure Aurora DSQL retries

from capo_dsql import AsyncDSQLClient


async def main():
    async with AsyncDSQLClient(region="us-east-1", retry_max_attempts=5) as dsql:
        # Throttling, server errors and network failures are retried. Every call
        # of this client gets five attempts instead of the default three
        await dsql.get_cluster("abc123")
from capo_dsql import AsyncDSQLClient


async def main():
    async with AsyncDSQLClient(region="us-east-1") as dsql:
        # No retries for this call
        await dsql.get_cluster("abc123", config_overrides={"retry_max_attempts": 1})

Metadata

Release files for capo-dsql 0.14.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for capo-dsql 0.14.0
File Size Uploaded Provenance Actions
capo_dsql-0.14.0.tar.gz 106.2 kB Without provenance Details for capo_dsql-0.14.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for capo-dsql 0.14.0
File Interpreter ABI Platform Actions
capo_dsql-0.14.0-py3-none-any.whl Python 3 none any Details for capo_dsql-0.14.0-py3-none-any.whl

Total release size: 275.6 kB

Release files / capo_dsql-0.14.0.tar.gz

Download URL capo_dsql-0.14.0.tar.gz
Size 106.2 kB
Tags Source
SHA-256 checksum
How to use checksums
543cd440841a65555dde978dca1484444bb0c04b71013f729e24c43f1a6bb55f
BLAKE2b-256 checksum
How to use checksums
685fa79e0bc0b4c02888fcb72a6d3327e0f204d65c78b9d24a8e59724857a645
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.13.9 {"installer":{"name":"uv","version":"0.13.9","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"26.04","id":"resolute","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release files / capo_dsql-0.14.0-py3-none-any.whl

Download URL capo_dsql-0.14.0-py3-none-any.whl
Size 169.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
fc5c11f3dcbace32b9b739a739ac1c1e6cd45a67b3642410edb8670fd029d577
BLAKE2b-256 checksum
How to use checksums
5f730dadc54e4ce6679877310475c869a6608d42f37324baea87056a875e129f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.13.9 {"installer":{"name":"uv","version":"0.13.9","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"26.04","id":"resolute","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Security for capo-dsql 0.14.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page