Skip to content

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.

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.

Before using this generator, ensure you have:

  1. A container service with infra=ecs (generated using the ts#container generator)
  2. A ts#rdb project

Run this generator@aws/nx-plugin:connection

pnpm nx g @aws/nx-plugin:connection
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.

Generator Options5 options
sourceProjectRequiredstring

The source project

targetProjectRequiredstring

The target project to connect to

sourceComponentstring

The 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.

targetComponentstring

The 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: true

Whether 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.

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

Additionally, the service’s <service-name>-serve target is updated to depend on the database’s dev target.

Import the getter in your service’s code and call it where you handle a request:

packages/my-project/src/my-service/orders.ts
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.

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:

packages/infra/src/stacks/application-stack.ts
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.

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-certificates
ENV NODE_EXTRA_CA_CERTS=/usr/local/share/ca-certificates/rds-bundle.crt

update-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.

Terminal window
pnpm 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.