Python Agent to DynamoDB
The connection generator wires a Python Agent to a Python DynamoDB project, configuring local development so both start together automatically.
Prerequisites
Section titled “Prerequisites”Before using this generator, ensure you have:
- A
py#agentproject - A
py#dynamodbproject
Run the Generator
Section titled “Run the Generator”- 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
pnpm nx g @aws/nx-plugin:connectionyarn nx g @aws/nx-plugin:connectionnpx nx g @aws/nx-plugin:connectionbunx nx g @aws/nx-plugin:connectionYou can also perform a dry-run to see what files would be changed
pnpm nx g @aws/nx-plugin:connection --dry-runyarn nx g @aws/nx-plugin:connection --dry-runnpx nx g @aws/nx-plugin:connection --dry-runbunx nx g @aws/nx-plugin:connection --dry-runSelect your Agent project as the source and your DynamoDB project as the target. If the project contains multiple agent components, specify sourceComponent to disambiguate.
Options
Section titled “Options”| Parameter | Type | Default | Description |
|---|---|---|---|
| sourceProject Required | string | - | The source project |
| targetProject Required | string | - | The target project to connect to |
| sourceComponent | string | - | 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. |
| targetComponent | string | - | 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. |
| preferInstallDependencies | boolean | 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. |
Generator Output
Section titled “Generator Output”The generator updates the agent’s <agent-name>-dev target in project.json to depend on the DynamoDB project’s dev target, and adds the DynamoDB package as a workspace dependency. No source files are modified.
Using DynamoDB in Agents
Section titled “Using DynamoDB in Agents”Import entity classes from the DynamoDB package and use them inside your agent tools:
from my_scope.my_table.entities.example import ExampleModelfrom strands import tool
@tooldef list_examples() -> list: """List all example items.""" return [item.attribute_values for item in ExampleModel.scan()]Infrastructure
Section titled “Infrastructure”To allow the agent’s Lambda function to access the DynamoDB table, grant the necessary permissions in your infrastructure.
import { MyTable } from ':my-scope/common-constructs';
const table = new MyTable(this, 'Table');const myAgent = new MyAgent(this, 'MyAgent');
table.grantReadWriteData(myAgent);grantReadWriteData grants both the DynamoDB and KMS permissions to the agent’s execution role.
module "my_table" { source = "../../common/terraform/src/app/dynamodb/my-table"}
resource "aws_iam_role_policy" "dynamodb_access" { role = module.my_agent.lambda_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”The connection generator configures your project’s dev target to depend on the DynamoDB project’s dev target. DynamoDB Local will start automatically alongside your project when running dev.
The LOCAL_DEV=true environment variable is set automatically, so is_local() returns True and your PynamoDB entities connect to the local DynamoDB instance instead of AWS.