Python Agent 到关系数据库
connection 生成器将 Python Agent 连接到 Python 关系数据库项目,使数据库会话在您的 agent 工具中可用。
在使用此生成器之前,请确保您具有:
运行此生成器@aws/nx-plugin:connection
pnpm nx g @aws/nx-plugin:connection yarn nx g @aws/nx-plugin:connection npx nx g @aws/nx-plugin:connection bunx nx g @aws/nx-plugin:connection- 安装 Nx Console VSCode Plugin 如果您尚未安装
- 在VSCode中打开Nx控制台
- 点击
Generate (UI)在"Common Nx Commands"部分 - 搜索
@aws/nx-plugin - connection - 填写必需参数
- 点击
Generate
构建你的命令5
必需
必需
选择您的 Agent 项目作为源,选择您的关系数据库项目作为目标。如果项目包含多个 agent 组件,请指定 sourceComponent 以消除歧义。
sourceProject必需string源项目
targetProject必需string要连接到的目标项目
sourceComponentstring要连接的源组件(组件名称、相对于源项目根目录的路径或生成器 ID)。使用 '.' 显式选择项目作为源。
targetComponentstring要连接到的目标组件(组件名称、相对于目标项目根目录的路径或生成器 ID)。使用 '.' 显式选择项目作为目标。
preferInstallDependenciesboolean默认值:true是否在生成器运行后优先安装依赖项。设置为 false 可在批量运行多个生成器时延迟安装(如果后续生成器需要计算 Nx 项目图,仍会运行安装);在最后统一安装一次。
文件夹packages/my_service
- project.json 添加从
my_agent-dev到数据库的dev目标的依赖 - pyproject.toml 将数据库包添加为工作区依赖
文件夹my_service
文件夹my_agent
- Dockerfile 添加用于直接 Aurora 连接的 RDS CA 证书包(仅当 agent 的
infra为agentcore-ecr时)
- Dockerfile 添加用于直接 Aurora 连接的 RDS CA 证书包(仅当 agent 的
- project.json 添加从
在 Agent 工具中使用数据库
Section titled “在 Agent 工具中使用数据库”从您的数据库包中导入 session_context 并在您的 agent 工具中使用它:
from sqlmodel import selectfrom my_scope_my_db import session_contextfrom my_scope_my_db.models.example import ExampleModelfrom strands import tool
@toolasync def list_examples() -> list: """List all example records.""" async with session_context() as session: items = (await session.execute(select(ExampleModel))).scalars().all() return [item.model_dump() for item in items]生成的 agent 构造实现了 IGrantable 和 IConnectable,因此您可以直接在构造上授予对数据库的网络和 IAM 访问权限。
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 打开安全组规则,以便 agent 运行时可以访问数据库端口。grantConnect 向 agent 的执行角色授予 IAM rds-db:connect 权限。
在与数据库相同的 VPC 中运行 agent,通过 additional_iam_policy_statements 授予它 rds-db:connect 权限,并使用一对安全组规则打开网络路径。aws_vpc.main 和 aws_subnet 资源在数据库部署指南中定义:
module "my_database" { source = "../../common/terraform/src/app/dbs/my-database" vpc_id = aws_vpc.main.id database_subnet_ids = aws_subnet.database[*].id lambda_subnet_ids = aws_subnet.private[*].id}
module "my_agent" { source = "../../common/terraform/src/app/agents/my-agent" enable_vpc = true vpc_id = aws_vpc.main.id subnet_ids = aws_subnet.private[*].id
appconfig_application_id = module.runtime_config_appconfig.application_id appconfig_application_arn = module.runtime_config_appconfig.application_arn
additional_iam_policy_statements = [ { Effect = "Allow" Action = ["rds-db:connect"] Resource = [ "arn:aws:rds-db:${data.aws_region.current.region}:${data.aws_caller_identity.current.account_id}:dbuser:${module.my_database.connect_resource_id}/${module.my_database.database_runtime_user}" ] } ]}
resource "aws_vpc_security_group_ingress_rule" "agent_to_database" { description = "Allow the agent runtime to connect to the database" security_group_id = module.my_database.security_group_id referenced_security_group_id = module.my_agent.security_group_id from_port = module.my_database.cluster_port to_port = module.my_database.cluster_port ip_protocol = "tcp"}
resource "aws_vpc_security_group_egress_rule" "agent_to_database" { description = "Allow outbound traffic from the agent runtime to the database" security_group_id = module.my_agent.security_group_id referenced_security_group_id = module.my_database.security_group_id from_port = module.my_database.cluster_port to_port = module.my_database.cluster_port ip_protocol = "tcp"}appconfig_application_id/appconfig_application_arn 来自在根模块中声明一次的共享运行时配置 AppConfig 应用程序,而不是来自数据库模块。该应用程序默认公开 database 命名空间,因此数据库模块的运行时配置条目无需进一步配置即可部署。
不使用 RDS Proxy 连接时的 SSL 要求
Section titled “不使用 RDS Proxy 连接时的 SSL 要求”当 agent 直接连接到 Aurora 集群(不使用 RDS Proxy)时,连接生成器会更新生成的 agent Dockerfile,将 Amazon RDS CA 证书包安装到系统信任存储中:
ADD https://truststore.pki.rds.amazonaws.com/global/global-bundle.pem /usr/local/share/ca-certificates/rds-global-bundle.crtRUN update-ca-certificates使用 RDS Proxy 时,您无需在 agent 运行时中配置 RDS CA 证书包。
pnpm nx <agent-name>-dev <project-name>yarn nx <agent-name>-dev <project-name>npx nx <agent-name>-dev <project-name>bunx nx <agent-name>-dev <project-name>这将启动 agent 和所有连接的数据库。LOCAL_DEV=true 环境变量使数据库客户端连接到其本地 Docker 数据库而不是 Aurora。