TypeScript Agent 到关系数据库
connection 生成器将 TypeScript Agent 连接到 关系数据库 项目,使 Prisma 客户端在您的 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 项目图,仍会运行安装);在最后统一安装一次。
生成器会修改您的 agent 源目录中的以下文件:
文件夹packages/my-service/src/my-agent
- agent.ts 在
getAgent中获取 Prisma 客户端并可供工具使用 - Dockerfile 安装 RDS CA 证书包并设置
NODE_EXTRA_CA_CERTS以实现与 Aurora 的 SSL 连接(仅当 agent 的infra为agentcore-ecr时)
- agent.ts 在
此外,agent 的 <agent-name>-dev 目标会更新为依赖于数据库的 dev 目标。
在 Agent 工具中使用数据库
Section titled “在 Agent 工具中使用数据库”Prisma 客户端在 getAgent() 内部实例化。由于 ts#agent 生成器为每个会话配置一个 Agent,因此客户端也会在会话的生命周期内重复使用:
import { getPrisma as getMyDb } from '@my-scope/my-db';
export const getAgent = async () => { const myDb = await getMyDb(); // ... return new Agent({ /* use myDb in tools */ });};使用不同的目标再次运行生成器会将第二个数据库添加到第一个数据库旁边:
import { getPrisma as getMyDb } from '@my-scope/my-db';import { getPrisma as getOtherDb } from '@my-scope/other-db';
export const getAgent = async () => { const myDb = await getMyDb(); const otherDb = await getOtherDb(); // ... return new Agent({ /* use both clients in tools */ });};生成的 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 要求”当代理在不使用 RDS Proxy 的情况下连接到 Aurora 集群时,Node.js 必须信任 Amazon RDS 证书颁发机构 (CA)。Node.js 不会读取操作系统信任存储。它仅从 NODE_EXTRA_CA_CERTS 环境变量指向的文件加载额外的 CA 证书。
- 当代理的
infra为agentcore-ecr时,连接生成器会更新 Dockerfile。它将 Amazon RDS CA 证书包下载到/usr/local/share/ca-certificates/rds-bundle.crt并将NODE_EXTRA_CA_CERTS设置为该路径,因此无需配置。对于由早期插件版本生成的 Dockerfile,nx migrate会添加NODE_EXTRA_CA_CERTS行。 - 当代理的
infra为agentcore(默认值)时,代理作为代码包部署,不包含 RDS CA 证书包。请保持启用 RDS Proxy(默认值),或使用agentcore-ecr。
有关更多详细信息,请参阅 Amazon RDS SSL/TLS 文档。使用 RDS Proxy 时,您无需配置 NODE_EXTRA_CA_CERTS。
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 环境变量会导致每个 Prisma 客户端连接到其本地 Docker 数据库而不是 Aurora。