TypeScript 容器服务到关系数据库
connection 生成器让 TypeScript 容器服务使用关系数据库项目。它添加一个返回数据库 Prisma 客户端的模块,并为服务的 Docker 镜像准备与 Amazon Aurora 的 SSL 连接。
使用 infra=ecs 的容器服务在您的 VPC 中作为 Amazon ECS(Elastic Container Service)上的任务运行。每个任务以服务的任务角色运行,这是一个在您授予更多权限之前只能读取运行时配置的 IAM 角色,并且位于服务的安全组中,该安全组仅允许端口 443 上的出站流量。要使用数据库,服务需要两个默认情况下没有的东西:
rds-db:connect的 IAM 权限,以便它可以使用 IAM 身份验证登录数据库。- 数据库端口上的安全组规则对:数据库安全组上的入站规则和服务安全组上的出站规则。如果没有出站规则,服务无法访问数据库,因为其安全组仅允许端口 443 出站。
生成器会拒绝 infra=none 的服务,因为这样的服务没有任务角色可以授予访问权限。
在使用此生成器之前,请确保您有:
- 一个使用
infra=ecs的容器服务(使用ts#container生成器生成) - 一个
ts#rdb项目
运行此生成器@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
必需
必需
选择包含您的容器服务的项目作为源,选择您的关系数据库项目作为目标。如果源项目包含多个组件,请将 sourceComponent 设置为服务的名称。
sourceProject必需string源项目
targetProject必需string要连接到的目标项目
sourceComponentstring要连接的源组件(组件名称、相对于源项目根目录的路径或生成器 ID)。使用 '.' 显式选择项目作为源。
targetComponentstring要连接到的目标组件(组件名称、相对于目标项目根目录的路径或生成器 ID)。使用 '.' 显式选择项目作为目标。
preferInstallDependenciesboolean默认值:true是否在生成器运行后优先安装依赖项。设置为 false 可在批量运行多个生成器时延迟安装(如果后续生成器需要计算 Nx 项目图,仍会运行安装);在最后统一安装一次。
生成器会更改服务源目录中的以下文件:
文件夹packages/my-project/src/my-service
- my-database.ts 导出
getMyDatabase,它返回数据库的 Prisma 客户端 - Dockerfile 安装 Amazon RDS CA 证书包以用于与 Aurora 的 SSL 连接
- my-database.ts 导出
此外,服务的 <service-name>-serve 目标会更新为依赖于数据库的 dev 目标。
在服务中使用数据库
Section titled “在服务中使用数据库”在服务代码中导入 getter 并在处理请求的地方调用它:
import { getMyDatabase } from './my-database.js';
export const listOrders = async () => { const myDatabase = await getMyDatabase(); return myDatabase.order.findMany();};数据库客户端从运行时配置中读取数据库的连接详细信息,使用 RUNTIME_CONFIG_APP_ID 环境变量。
生成的容器服务会设置 RUNTIME_CONFIG_APP_ID 并授予其任务角色对运行时配置的读取访问权限,因此您不需要自己添加这两者。在 Terraform 中,服务的模块块必须传递 appconfig_application_id 和 appconfig_application_arn,如容器指南中所示。
生成器不会更改您的堆栈或根 Terraform 模块。相反,它会打印要添加的代码,下面显示了名为 my-service 的服务和名为 my-database 的数据库的代码。在您添加它之前,服务无法访问数据库端口,数据库会拒绝其 IAM 身份验证。
在示例中,myService 是您的 MyService 构造,myDatabase 是您的 MyDatabase 构造:
myDatabase.allowDefaultPortFrom(myService);myDatabase.grantConnect(myService.taskRole);allowDefaultPortFrom 在数据库的安全组上添加入站规则,在服务的安全组上添加出站规则。grantConnect 向服务的任务角色授予 rds-db:connect 权限。
在示例中,module "my_service" 部署服务,module "my_database" 部署数据库。data "aws_region" "current" 和 data "aws_caller_identity" "current" 数据源在生成的 providers.tf 中声明:
resource "aws_iam_role_policy" "my_service_my_database_connect" { name = "my-service-my-database-connect" role = module.my_service.task_role_name
policy = jsonencode({ Version = "2012-10-17" Statement = [ { 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" "my_service_to_my_database" { description = "Allow the my-service container service to connect to the database" security_group_id = module.my_database.security_group_id referenced_security_group_id = module.my_service.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" "my_service_to_my_database" { description = "Allow the my-service container service to reach the database" security_group_id = module.my_service.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"}不使用 RDS Proxy 连接时的 SSL 要求
Section titled “不使用 RDS Proxy 连接时的 SSL 要求”当服务直接连接到 Aurora 集群而不使用 RDS Proxy 时,Node.js 必须信任 Amazon RDS 证书颁发机构。生成器会在服务的 Dockerfile 中的 EXPOSE 行之前添加此步骤:
RUN apt-get update && apt-get install -y --no-install-recommends curl ca-certificates && \ rm -rf /var/lib/apt/lists/* && \ curl -fsSL "https://truststore.pki.rds.amazonaws.com/global/global-bundle.pem" \ -o /usr/local/share/ca-certificates/rds-bundle.crt && \ update-ca-certificatesENV NODE_EXTRA_CA_CERTS=/usr/local/share/ca-certificates/rds-bundle.crtupdate-ca-certificates 会写入系统证书存储,而 Node.js 不会读取它,因此生成器还会在 Dockerfile 中设置 NODE_EXTRA_CA_CERTS。您不需要自己配置它,使用 RDS Proxy 时它不会被使用。有关更多详细信息,请参阅 Amazon RDS SSL/TLS 文档。
pnpm nx <service-name>-serve <project-name>yarn nx <service-name>-serve <project-name>npx nx <service-name>-serve <project-name>bunx nx <service-name>-serve <project-name>这会启动服务和数据库的本地 Docker 数据库。设置 LOCAL_DEV=true 后,Prisma 客户端会连接到本地数据库而不是 Aurora。