TypeScript Agent 连接到关系数据库
connection 生成器将 TypeScript Agent 连接到 关系数据库 项目,使 Prisma 客户端在您的 agent 工厂内可用。
在使用此生成器之前,请确保您拥有:
- 安装 Nx Console VSCode Plugin 如果您尚未安装
- 在VSCode中打开Nx控制台
- 点击
Generate (UI)在"Common Nx Commands"部分 - 搜索
@aws/nx-plugin - connection - 填写必需参数
- 点击
Generate
pnpm nx g @aws/nx-plugin:connectionyarn nx g @aws/nx-plugin:connectionnpx nx g @aws/nx-plugin:connectionbunx nx g @aws/nx-plugin:connection选择您的 Agent 项目作为源,选择您的关系数据库项目作为目标。如果项目包含多个 agent 组件,请指定 sourceComponent 以消除歧义。
| 参数 | 类型 | 默认值 | 描述 |
|---|---|---|---|
| sourceProject 必需 | string | - | 源项目 |
| targetProject 必需 | string | - | 要连接到的目标项目 |
| sourceComponent | string | - | 要从其连接的源组件(组件名称、相对于源项目根目录的路径或生成器 ID)。使用 '.' 显式选择项目作为源。 |
| targetComponent | string | - | 要连接到的目标组件(组件名称、相对于目标项目根目录的路径或生成器 ID)。使用 '.' 显式选择项目作为目标。 |
| preferInstallDependencies | boolean | true | 是否在生成器运行后优先安装依赖项。设置为 false 可在批量运行多个生成器时延迟安装(如果后续生成器需要计算 Nx 项目图,仍会运行安装);在最后统一安装一次。 |
生成器修改 agent 源目录中的两个文件:
文件夹packages/my-service/src/my-agent
- agent.ts 在
getAgent内获取 Prisma 客户端并可用于工具 - Dockerfile 安装 RDS CA 捆绑包以实现与 Aurora 的 SSL 连接
- 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 { 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 }, }),});
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 命名空间,以便部署数据库模块的运行时配置条目:
module "runtime_config_appconfig" { source = "../../common/terraform/src/core/runtime-config/appconfig"
application_name = "my-app-runtime-config" namespaces = ["connection", "agentcore", "database"]}不使用 RDS Proxy 连接时的 SSL 要求
Section titled “不使用 RDS Proxy 连接时的 SSL 要求”连接生成器会更新 Dockerfile,将 Amazon RDS CA 证书包安装到 /usr/local/share/ca-certificates/rds-bundle.crt。将 NODE_EXTRA_CA_CERTS 设置为该路径,以便 Node.js 在不使用 RDS Proxy 连接时信任该证书:
new MyAgent(this, 'MyAgent', { ... environmentVariables: { NODE_EXTRA_CA_CERTS: '/usr/local/share/ca-certificates/rds-bundle.crt', },});module "my_agent" { ... environment_variables = { NODE_EXTRA_CA_CERTS = "/usr/local/share/ca-certificates/rds-bundle.crt" }}有关更多详细信息,请参阅 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。