TypeScript Container Service to Relational Database
The connection generator lets a TypeScript container service use a Relational Database project. It adds a module that returns the database’s Prisma client, and prepares the service’s Docker image for SSL connections to Amazon Aurora.
How this works
Section titled “How this works”A container service with infra=ecs runs as tasks on Amazon ECS (Elastic Container Service) in your VPC. Each task runs as the service’s task role, an IAM role that can only read runtime configuration until you grant more, and sits in the service’s security group, which allows outbound traffic on port 443 only. To use the database, the service needs two things that it does not have by default:
- IAM permission for
rds-db:connect, so it can sign in to the database with IAM authentication. - A security group rule pair on the database port: an inbound rule on the database’s security group, and an outbound rule on the service’s security group. Without the outbound rule the service cannot reach the database, because its security group allows only port 443 out.
The generator rejects a service with infra=none, because such a service has no task role to grant access to.
Prerequisites
Section titled “Prerequisites”Before using this generator, ensure you have:
- A container service with
infra=ecs(generated using thets#containergenerator) - A
ts#rdbproject
Run the Generator
Section titled “Run the Generator”Run this generator@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- Install the Nx Console VSCode Plugin if you haven't already
- Open the Nx Console in VSCode
- Click
Generate (UI)in the "Common Nx Commands" section - Search for
@aws/nx-plugin - connection - Fill in the required parameters
- Click
Generate
Build your command5
Required
Required
Select the project containing your container service as the source and your relational database project as the target. If the source project holds several components, set sourceComponent to the service’s name.
Options
Section titled “Options”sourceProjectRequiredstringThe source project
targetProjectRequiredstringThe target project to connect to
sourceComponentstringThe 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.
targetComponentstringThe 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.
preferInstallDependenciesbooleanDefault:trueWhether 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.
Generator Output
Section titled “Generator Output”The generator changes the following files in your service’s source directory:
Directorypackages/my-project/src/my-service
- my-database.ts Exports
getMyDatabase, which returns the database’s Prisma client - Dockerfile Installs the Amazon RDS CA bundle for SSL connections to Aurora
- my-database.ts Exports
Additionally, the service’s <service-name>-serve target is updated to depend on the database’s dev target.
Using the Database in the Service
Section titled “Using the Database in the Service”Import the getter in your service’s code and call it where you handle a request:
import { getMyDatabase } from './my-database.js';
export const listOrders = async () => { const myDatabase = await getMyDatabase(); return myDatabase.order.findMany();};The database client reads the database’s connection details from runtime configuration, using the RUNTIME_CONFIG_APP_ID environment variable.
The generated container service sets RUNTIME_CONFIG_APP_ID and grants its task role read access to runtime configuration, so you do not add either yourself. In Terraform, the service’s module block must pass appconfig_application_id and appconfig_application_arn, as shown in the container guide.
Infrastructure
Section titled “Infrastructure”The generator does not change your stack or root Terraform module. Instead it prints the code to add, which is shown below for a service named my-service and a database named my-database. Until you add it, the service cannot reach the database port and the database refuses its IAM authentication.
In the example, myService is your MyService construct and myDatabase is your MyDatabase construct:
myDatabase.allowDefaultPortFrom(myService);myDatabase.grantConnect(myService.taskRole);allowDefaultPortFrom adds the inbound rule on the database’s security group and the outbound rule on the service’s security group. grantConnect grants rds-db:connect to the service’s task role.
In the example, module "my_service" deploys the service and module "my_database" deploys the database. The data "aws_region" "current" and data "aws_caller_identity" "current" data sources are declared in the generated 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"}SSL Requirements When Connecting Without RDS Proxy
Section titled “SSL Requirements When Connecting Without RDS Proxy”When the service connects to the Aurora cluster directly, without RDS Proxy, Node.js must trust the Amazon RDS certificate authority. The generator adds this step to the service’s Dockerfile, before its EXPOSE line:
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 writes the system certificate store, which Node.js does not read, so the generator also sets NODE_EXTRA_CA_CERTS in the Dockerfile. You do not need to configure it yourself, and with RDS Proxy it is unused. For more details, see the Amazon RDS SSL/TLS documentation.
Local Development
Section titled “Local Development”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>This starts the service and the database’s local Docker database. With LOCAL_DEV=true set, the Prisma client connects to the local database instead of Aurora.