Skip to content

Python Container Service to Relational Database

The connection generator lets a Python container service use a Python Relational Database project. It adds the database package as a dependency of the service’s project, 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 py#container generator)
  2. A py#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.

  • Directorypackages/my_project
    • project.json Adds a dependency from my-service-serve on the database’s dev target
    • pyproject.toml Adds the database package as a workspace dependency
    • Directorymy_project
      • Directorymy_service
        • Dockerfile Installs the Amazon RDS CA bundle for SSL connections to Aurora

Import session_context from your database package and use it where you handle a request:

packages/my_project/my_project/my_service/orders.py
from sqlmodel import select
from my_scope_my_database import session_context
from my_scope_my_database.models.example import ExampleModel
async def list_examples() -> list[dict]:
async with session_context() as session:
items = (await session.execute(select(ExampleModel))).scalars().all()
return [item.model_dump() for item in items]

The database package 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, it must trust the Amazon RDS certificate authority. The generator adds these lines to the service’s Dockerfile, before its EXPOSE line, to install the Amazon RDS CA bundle into the system trust store:

ADD https://truststore.pki.rds.amazonaws.com/global/global-bundle.pem /usr/local/share/ca-certificates/rds-global-bundle.crt
RUN update-ca-certificates

When using RDS Proxy, you do not need the RDS CA bundle.

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 database client connects to the local database instead of Aurora.