Skip to content

Python Container Service to DynamoDB

The connection generator lets a Python container service use a Python DynamoDB project. It adds the DynamoDB package as a dependency of the service’s project, configures local development so the service and DynamoDB Local start together, and prints the grants the service needs.

A container service with infra=ecs runs as tasks on Amazon ECS (Elastic Container Service). Each task runs as the service’s task role, an IAM role that can only read runtime configuration until you grant more. To read and write the table, the task role needs the DynamoDB actions on the table and its indexes, and use of the table’s AWS KMS encryption key.

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#dynamodb 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 DynamoDB 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 adds the DynamoDB package to the service project’s pyproject.toml as a workspace dependency, and updates the service’s <service-name>-serve target in project.json to depend on the DynamoDB project’s dev target.

Import entity classes from the DynamoDB package and use them where you handle a request:

packages/my_project/my_project/my_service/examples.py
from my_scope_my_table.entities.example import ExampleModel
def list_examples() -> list[dict]:
return [item.attribute_values for item in ExampleModel.scan()]

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 table named my-table. Until you add it, the service’s requests to the table are denied.

In the example, myService is your MyService construct and myTable is your MyTable construct:

packages/infra/src/stacks/application-stack.ts
myTable.grantReadWriteData(myService.taskRole);

grantReadWriteData grants both the DynamoDB and the KMS permissions to the service’s task role.

Terminal window
pnpm nx <service-name>-serve <project-name>

This starts the service and DynamoDB Local. With LOCAL_DEV=true set, is_local() returns True and your PynamoDB entities connect to DynamoDB Local instead of AWS.