Skip to content

Python Agent to Relational Database

The connection generator wires a Python Agent to a Python Relational Database project, making the database session available inside your agent tools.

Before using this generator, ensure you have:

  1. A py#agent project
  2. A py#rdb project
  1. Install the Nx Console VSCode Plugin if you haven't already
  2. Open the Nx Console in VSCode
  3. Click Generate (UI) in the "Common Nx Commands" section
  4. Search for @aws/nx-plugin - connection
  5. Fill in the required parameters
    • Click Generate

    Select your Agent project as the source and your relational database project as the target. If the project contains multiple agent components, specify sourceComponent to disambiguate.

    ParameterTypeDefaultDescription
    sourceProject Requiredstring-The source project
    targetProject Requiredstring-The target project to connect to
    sourceComponent string-The source component to connect from (component name, path relative to source project root, or generator id). Use '.' to explicitly select the project as the source.
    targetComponent string-The target component to connect to (component name, path relative to target project root, or generator id). Use '.' to explicitly select the project as the target.
    preferInstallDependencies booleantrueWhether to prefer installing dependencies after the generator runs. Set to false to defer installing when batching multiple generators (an install still runs if needed so subsequent generators can compute the Nx project graph); install once at the end.
    • Directorypackages/my_service
      • project.json Adds a dependency from my_agent-dev on the database’s dev target
      • pyproject.toml Adds the database package as a workspace dependency
      • Directorymy_service
        • Directorymy_agent
          • Dockerfile Adds the RDS CA bundle used for direct Aurora connections

    Import session_context from your database package and use it inside your agent tools:

    packages/my_service/my_service/my_agent/agent.py
    from sqlmodel import select
    from my_scope.my_db import session_context
    from my_scope.my_db.models.example import ExampleModel
    from strands import tool
    @tool
    async def list_examples() -> list:
    """List all example records."""
    async with session_context() as session:
    return [item.model_dump() for item in (await session.execute(select(ExampleModel))).all()]

    The generated agent construct implements IGrantable and IConnectable, so you can grant network and IAM access to the database directly on the construct.

    packages/infra/src/stacks/application-stack.ts
    import { SecurityGroup } from 'aws-cdk-lib/aws-ec2';
    import { RuntimeNetworkConfiguration } from 'aws-cdk-lib/aws-bedrockagentcore';
    import { MyDatabase } from ':my-scope/common-constructs';
    const db = new MyDatabase(this, 'Db', { vpc, ... });
    const myAgent = new MyAgent(this, 'MyAgent', {
    networkConfiguration: RuntimeNetworkConfiguration.usingVpc(this, {
    vpc,
    vpcSubnets: { subnetType: SubnetType.PRIVATE_WITH_EGRESS },
    securityGroups: [
    new SecurityGroup(this, 'MyAgentSecurityGroup', { vpc, allowAllOutbound: true }),
    ],
    }),
    });
    db.allowDefaultPortFrom(myAgent);
    db.grantConnect(myAgent);

    allowDefaultPortFrom opens the security group rule so the agent runtime can reach the database port. grantConnect grants IAM rds-db:connect permission to the agent’s execution role.

    SSL Requirements When Connecting Without RDS Proxy

    Section titled “SSL Requirements When Connecting Without RDS Proxy”

    When the agent connects directly to the Aurora cluster (without RDS Proxy), the connection generator updates the generated agent Dockerfile to install the Amazon RDS CA bundle into the system trust store:

    ADD https://truststore.pki.rds.amazonaws.com/global/global-bundle.pem /usr/local/share/ca-certificates/rds-global-bundle.crt
    RUN update-ca-certificates

    When using RDS Proxy, you do not need to configure the RDS CA bundle in the agent runtime.

    Terminal window
    pnpm nx <agent-name>-dev <project-name>

    This starts the agent and all connected databases. The LOCAL_DEV=true environment variable causes the database client to connect to its local Docker database instead of Aurora.