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.
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 thepy#containergenerator) - A
py#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”Directorypackages/my_project
- project.json Adds a dependency from
my-service-serveon the database’sdevtarget - 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
- project.json Adds a dependency from
Using the Database in the Service
Section titled “Using the Database in the Service”Import session_context from your database package and use it where you handle a request:
from sqlmodel import selectfrom my_scope_my_database import session_contextfrom 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.
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, 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.crtRUN update-ca-certificatesWhen using RDS Proxy, you do not need the RDS CA bundle.
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 database client connects to the local database instead of Aurora.