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.
How this works
Section titled “How this works”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.
Prerequisites
Section titled “Prerequisites”Before using this generator, ensure you have:
- A container service with
infra=ecs(generated using thepy#containergenerator) - A
py#dynamodbproject
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 DynamoDB 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 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.
Using DynamoDB in the Service
Section titled “Using DynamoDB in the Service”Import entity classes from the DynamoDB package and use them where you handle a request:
from my_scope_my_table.entities.example import ExampleModel
def list_examples() -> list[dict]: return [item.attribute_values for item in ExampleModel.scan()]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 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:
myTable.grantReadWriteData(myService.taskRole);grantReadWriteData grants both the DynamoDB and the KMS permissions to the service’s task role.
In the example, module "my_service" deploys the service and module "my_table" deploys the table:
resource "aws_iam_role_policy" "my_service_my_table_access" { name = "my-service-my-table-access" role = module.my_service.task_role_name
policy = jsonencode({ Version = "2012-10-17" Statement = [ { Effect = "Allow" Action = [ "dynamodb:GetItem", "dynamodb:PutItem", "dynamodb:UpdateItem", "dynamodb:DeleteItem", "dynamodb:Query", "dynamodb:Scan", "dynamodb:BatchGetItem", "dynamodb:BatchWriteItem", ] Resource = [ module.my_table.table_arn, "${module.my_table.table_arn}/index/*", ] }, { Effect = "Allow" Action = [ "kms:Encrypt", "kms:Decrypt", "kms:ReEncrypt*", "kms:GenerateDataKey*", "kms:DescribeKey", ] Resource = [module.my_table.kms_key_arn] }, ] })}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 DynamoDB Local. With LOCAL_DEV=true set, is_local() returns True and your PynamoDB entities connect to DynamoDB Local instead of AWS.