From 1180c123ce28b849596c7cc4466296faaeb18cbe Mon Sep 17 00:00:00 2001 From: Stefan Schulte Date: Tue, 13 Oct 2026 19:55:21 +0200 Subject: [PATCH 01/14] New module DynamoDB Adds a new `DynamoDBContainer` to help running a local AWS DynamoDB container as described [in the docs][1] [1]: https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/DynamoDBLocal.DownloadingAndRunning.html#docker --- docs/modules/dynamodb.md | 47 +++ mkdocs.yml | 1 + package-lock.json | 101 +++++++ packages/modules/dynamodb/Dockerfile | 1 + packages/modules/dynamodb/package.json | 37 +++ .../dynamodb/src/dynamodb-container.test.ts | 268 ++++++++++++++++++ .../dynamodb/src/dynamodb-container.ts | 182 ++++++++++++ packages/modules/dynamodb/src/index.ts | 1 + packages/modules/dynamodb/tsconfig.build.json | 9 + packages/modules/dynamodb/tsconfig.json | 16 ++ 10 files changed, 663 insertions(+) create mode 100644 docs/modules/dynamodb.md create mode 100644 packages/modules/dynamodb/Dockerfile create mode 100644 packages/modules/dynamodb/package.json create mode 100644 packages/modules/dynamodb/src/dynamodb-container.test.ts create mode 100644 packages/modules/dynamodb/src/dynamodb-container.ts create mode 100644 packages/modules/dynamodb/src/index.ts create mode 100644 packages/modules/dynamodb/tsconfig.build.json create mode 100644 packages/modules/dynamodb/tsconfig.json diff --git a/docs/modules/dynamodb.md b/docs/modules/dynamodb.md new file mode 100644 index 000000000..c1ff070b7 --- /dev/null +++ b/docs/modules/dynamodb.md @@ -0,0 +1,47 @@ +# DynamoDB + +## Install + +```bash +npm install @testcontainers/dynamodb --save-dev +``` + +## Examples + +These examples use the following libraries: + +- [@aws-sdk/client-dynamodb](https://www.npmjs.com/package/@aws-sdk/client-dynamodb) + + npm install @aws-sdk/client-dynamodb + +Choose an image from the [container registry](https://hub.docker.com/r/amazon/dynamodb-local) and substitute `IMAGE`. + +### Connect to local endpoint + +In order to use a DynamoDB Client we have to specify +at least `region`, `credentials` and `endpoint` eventhough `region` and `credentials` +will not be used: + + +[](../../packages/modules/dynamodb/src/dynamodb-container.test.ts) inside_block:connectEndpoint + + +### Connect via client config + +The above can also be written as + + +[](../../packages/modules/dynamodb/src/dynamodb-container.test.ts) inside_block:connectClientConfig + + +### Use a shared database + + +[](../../packages/modules/dynamodb/src/dynamodb-container.test.ts) inside_block:sharedDB + + +### Disable telemetry + + +[](../../packages/modules/dynamodb/src/dynamodb-container.test.ts) inside_block:disabledTelemetry + diff --git a/mkdocs.yml b/mkdocs.yml index 7e70bc96e..4a2dd3027 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -64,6 +64,7 @@ nav: - CosmosDB: modules/cosmosdb.md - Couchbase: modules/couchbase.md - CouchDB: modules/couchdb.md + - DynamoDB: modules/dynamodb.md - Elasticsearch: modules/elasticsearch.md - Etcd: modules/etcd.md - GCloud: modules/gcloud.md diff --git a/package-lock.json b/package-lock.json index 6a5c74e9a..e2d7d9f84 100644 --- a/package-lock.json +++ b/package-lock.json @@ -61,6 +61,28 @@ "node": ">=20.0.0" } }, + "node_modules/@aws-sdk/client-dynamodb": { + "version": "3.1168.0", + "resolved": "https://registry.npmjs.org/@aws-sdk/client-dynamodb/-/client-dynamodb-3.1168.0.tgz", + "integrity": "sha512-pIuS8zdk0RimAlCQn8xVyO78Ohyl2aeH/2ADAsqO25qFyGqq1Vfa+DuLnzpLQCUXgJr0Zaza1aCaRNXWdyfZ7g==", + "dev": true, + "license": "Apache-2.0", + "dependencies": { + "@aws-sdk/core": "^3.981.0", + "@aws-sdk/credential-provider-node": "^3.972.97", + "@aws-sdk/dynamodb-codec": "^3.973.58", + "@aws-sdk/middleware-endpoint-discovery": "^3.972.39", + "@aws-sdk/types": "^3.975.1", + "@smithy/core": "^3.40.0", + "@smithy/fetch-http-handler": "^5.8.1", + "@smithy/node-http-handler": "^4.14.2", + "@smithy/types": "^4.20.0", + "tslib": "^2.6.2" + }, + "engines": { + "node": ">=20.0.0" + } + }, "node_modules/@aws-sdk/client-s3": { "version": "3.1167.0", "resolved": "https://registry.npmjs.org/@aws-sdk/client-s3/-/client-s3-3.1167.0.tgz", @@ -260,6 +282,53 @@ "node": ">=20.0.0" } }, + "node_modules/@aws-sdk/dynamodb-codec": { + "version": "3.973.58", + "resolved": "https://registry.npmjs.org/@aws-sdk/dynamodb-codec/-/dynamodb-codec-3.973.58.tgz", + "integrity": "sha512-OrqMNzvjk37ssH/qoD6Cd25TLk2cT1lCMwCNM+frwbOQAKJuAbFSgH6OOtaI71l5fA8F7GPuMr9Ck57SAtY98g==", + "dev": true, + "license": "Apache-2.0", + "dependencies": { + "@aws-sdk/core": "^3.981.0", + "@smithy/core": "^3.40.0", + "@smithy/types": "^4.20.0", + "tslib": "^2.6.2" + }, + "engines": { + "node": ">=20.0.0" + } + }, + "node_modules/@aws-sdk/endpoint-cache": { + "version": "3.972.11", + "resolved": "https://registry.npmjs.org/@aws-sdk/endpoint-cache/-/endpoint-cache-3.972.11.tgz", + "integrity": "sha512-8q1ICxcDjHId3bBryuu/j+1L9y5/3uQnwzLDt5j2ElcjZSoWmFtymdJy7OjLrluSMe0Z4mq5bcH4fxBXvlEHfw==", + "dev": true, + "license": "Apache-2.0", + "dependencies": { + "mnemonist": "0.38.3", + "tslib": "^2.6.2" + }, + "engines": { + "node": ">=20.0.0" + } + }, + "node_modules/@aws-sdk/middleware-endpoint-discovery": { + "version": "3.972.39", + "resolved": "https://registry.npmjs.org/@aws-sdk/middleware-endpoint-discovery/-/middleware-endpoint-discovery-3.972.39.tgz", + "integrity": "sha512-AtZS+UK7CciPbdiszPoG08A23kUojFxFnWIv155uPieSPcHR9D9Sovtp19U0j5qmD2DzHpG9OI7mb0Kiyvf3mA==", + "dev": true, + "license": "Apache-2.0", + "dependencies": { + "@aws-sdk/endpoint-cache": "^3.972.11", + "@aws-sdk/types": "^3.975.1", + "@smithy/core": "^3.40.0", + "@smithy/types": "^4.20.0", + "tslib": "^2.6.2" + }, + "engines": { + "node": ">=20.0.0" + } + }, "node_modules/@aws-sdk/middleware-sdk-s3": { "version": "3.972.89", "resolved": "https://registry.npmjs.org/@aws-sdk/middleware-sdk-s3/-/middleware-sdk-s3-3.972.89.tgz", @@ -5007,6 +5076,10 @@ "resolved": "packages/modules/couchdb", "link": true }, + "node_modules/@testcontainers/dynamodb": { + "resolved": "packages/modules/dynamodb", + "link": true + }, "node_modules/@testcontainers/elasticsearch": { "resolved": "packages/modules/elasticsearch", "link": true @@ -10574,6 +10647,16 @@ "integrity": "sha512-gKLcREMhtuZRwRAfqP3RFW+TK4JqApVBtOIftVgjuABpAtpxhPGaDcfvbhNvD0B8iD1oUr/txX35NjcaY6Ns/A==", "license": "MIT" }, + "node_modules/mnemonist": { + "version": "0.38.3", + "resolved": "https://registry.npmjs.org/mnemonist/-/mnemonist-0.38.3.tgz", + "integrity": "sha512-2K9QYubXx/NAjv4VLq1d1Ly8pWNC5L3BrixtdkyTegXWJIqY+zLNDhhX/A+ZwWt70tB1S8H4BE8FLYEFyNoOBw==", + "dev": true, + "license": "MIT", + "dependencies": { + "obliterator": "^1.6.1" + } + }, "node_modules/mockserver-client": { "version": "9.2.0", "resolved": "https://registry.npmjs.org/mockserver-client/-/mockserver-client-9.2.0.tgz", @@ -11447,6 +11530,13 @@ "url": "https://github.com/sponsors/ljharb" } }, + "node_modules/obliterator": { + "version": "1.6.1", + "resolved": "https://registry.npmjs.org/obliterator/-/obliterator-1.6.1.tgz", + "integrity": "sha512-9WXswnqINnnhOG/5SLimUlzuU1hFJUc8zkwyD59Sd+dPOMf05PmnYG/d6Q7HZ+KmgkZJa1PxRso6QdM3sTNHig==", + "dev": true, + "license": "MIT" + }, "node_modules/obug": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/obug/-/obug-3.0.0.tgz", @@ -14736,6 +14826,17 @@ "nano": "^11.0.8" } }, + "packages/modules/dynamodb": { + "name": "@testcontainers/dynamodb", + "version": "12.2.1", + "license": "MIT", + "dependencies": { + "testcontainers": "^12.2.1" + }, + "devDependencies": { + "@aws-sdk/client-dynamodb": "^3.1168.0" + } + }, "packages/modules/elasticsearch": { "name": "@testcontainers/elasticsearch", "version": "12.2.2", diff --git a/packages/modules/dynamodb/Dockerfile b/packages/modules/dynamodb/Dockerfile new file mode 100644 index 000000000..4fc11a57d --- /dev/null +++ b/packages/modules/dynamodb/Dockerfile @@ -0,0 +1 @@ +FROM amazon/dynamodb-local:3.3.2 diff --git a/packages/modules/dynamodb/package.json b/packages/modules/dynamodb/package.json new file mode 100644 index 000000000..d0e5ece41 --- /dev/null +++ b/packages/modules/dynamodb/package.json @@ -0,0 +1,37 @@ +{ + "name": "@testcontainers/dynamodb", + "version": "12.2.1", + "license": "MIT", + "keywords": [ + "dynamodb", + "testing", + "docker", + "testcontainers" + ], + "description": "DynamoDB module for Testcontainers", + "homepage": "https://github.com/testcontainers/testcontainers-node#readme", + "repository": { + "type": "git", + "url": "git+https://github.com/testcontainers/testcontainers-node.git" + }, + "bugs": { + "url": "https://github.com/testcontainers/testcontainers-node/issues" + }, + "main": "build/index.js", + "files": [ + "build" + ], + "publishConfig": { + "access": "public" + }, + "scripts": { + "prepack": "shx cp ../../../README.md . && shx cp ../../../LICENSE .", + "build": "tsc --project tsconfig.build.json" + }, + "dependencies": { + "testcontainers": "^12.2.1" + }, + "devDependencies": { + "@aws-sdk/client-dynamodb": "^3.1168.0" + } +} diff --git a/packages/modules/dynamodb/src/dynamodb-container.test.ts b/packages/modules/dynamodb/src/dynamodb-container.test.ts new file mode 100644 index 000000000..0af0e79c2 --- /dev/null +++ b/packages/modules/dynamodb/src/dynamodb-container.test.ts @@ -0,0 +1,268 @@ +import { + CreateTableCommand, + DeleteTableCommand, + DynamoDBClient, + GetItemCommand, + ListTablesCommand, + PutItemCommand, + waitUntilTableExists, + waitUntilTableNotExists, +} from "@aws-sdk/client-dynamodb"; +import { describe, it } from "vitest"; +import { getImage } from "../../../testcontainers/src/utils/test-helper"; +import { DynamoDBContainer } from "./dynamodb-container"; + +const IMAGE = getImage(__dirname); + +/** + * Create an empty dummy user table + * + * This function can be used in test cases + * to easily add a new table. + * + * @param client - DynamoDBClient pointing to the container + * @param tableName - The desired table name + * @returns - Three functions: teardown, addUser, getUser + */ +async function createDummyTable( + client: DynamoDBClient, + tableName: string +): Promise<{ + teardown: () => Promise; + addUser: (userId: number, name: string) => Promise; + getUser: (userId: number) => Promise; +}> { + const command = new CreateTableCommand({ + TableName: tableName, + BillingMode: "PAY_PER_REQUEST", + AttributeDefinitions: [ + { + AttributeName: "userId", + AttributeType: "N", + }, + ], + KeySchema: [ + { + AttributeName: "userId", + KeyType: "HASH", + }, + ], + }); + + await client.send(command); + await waitUntilTableExists({ client, maxWaitTime: 30 }, { TableName: tableName }); + + return { + teardown: async (): Promise => { + const deleteCommand = new DeleteTableCommand({ TableName: tableName }); + await client.send(deleteCommand); + await waitUntilTableNotExists({ client, maxWaitTime: 30 }, { TableName: tableName }); + }, + addUser: async (userId, name) => { + const putCommand = new PutItemCommand({ + TableName: tableName, + Item: { + userId: { N: userId.toString() }, + name: { S: name }, + }, + }); + await client.send(putCommand); + }, + getUser: async (userId) => { + const getCommand = new GetItemCommand({ + TableName: tableName, + Key: { + userId: { N: userId.toString() }, + }, + ConsistentRead: true, + }); + const response = await client.send(getCommand); + if (response.Item?.name?.S) { + return response.Item.name.S; + } + return null; + }, + }; +} + +describe("DynamoDBContainer", { timeout: 180_000 }, () => { + it("starts a container", async () => { + // connectClientConfig { + await using container = await new DynamoDBContainer(IMAGE).start(); + const client = new DynamoDBClient(container.clientConfig()); + + const command = new ListTablesCommand({}); + const result = await client.send(command); + + expect(result.TableNames).toStrictEqual([]); + // } + }); + + it("connects with endpoint", async () => { + // connectEndpoint { + await using container = await new DynamoDBContainer(IMAGE).start(); + const client = new DynamoDBClient({ + region: "us-east-1", + credentials: { + accessKeyId: "AKIAIOSFODNN7EXAMPLE", + secretAccessKey: "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY", + }, + endpoint: container.getEndpoint(), + }); + + const command = new ListTablesCommand({}); + const result = await client.send(command); + + expect(result.TableNames).toStrictEqual([]); + // } + }); + + it("stores data", async () => { + await using container = await new DynamoDBContainer(IMAGE).start(); + const client = new DynamoDBClient(container.clientConfig()); + + const { teardown, addUser, getUser } = await createDummyTable(client, "users"); + + await addUser(1, "Bob"); + await addUser(2, "Alice"); + + const userA = await getUser(1); + const userB = await getUser(2); + expect(userA).toBe("Bob"); + expect(userB).toBe("Alice"); + + await teardown(); + }); + + it("separates data by client by default", async () => { + await using container = await new DynamoDBContainer(IMAGE).start(); + + // Uses dummy credentials from the documentation + const clientA = new DynamoDBClient({ + region: "eu-central-1", + endpoint: container.getEndpoint(), + credentials: { + accessKeyId: "AKIAIOSFODNN7EXAMPLE", + secretAccessKey: "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY", + }, + }); + + // Uses slightly different credentials to simulate another + // AWS account + const clientB = new DynamoDBClient({ + region: "eu-central-1", + endpoint: container.getEndpoint(), + credentials: { + accessKeyId: "AKIAIOSFODNN7EXAMPL2", + secretAccessKey: "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", + }, + }); + + await createDummyTable(clientA, "users"); + + const resultA = await clientA.send(new ListTablesCommand()); + const resultB = await clientB.send(new ListTablesCommand()); + + // Since clientA created the table, only clientA should see it + expect(resultA.TableNames).toStrictEqual(["users"]); + expect(resultB.TableNames).toStrictEqual([]); + }); + + it("uses a shared table when requested", async () => { + // sharedDB { + await using container = await new DynamoDBContainer(IMAGE).withSharedDB().start(); + + // Both clients will see the same data eventhough the + // ACCESS_KEY_ID differ + const clientA = new DynamoDBClient({ + region: "us-east-1", + endpoint: container.getEndpoint(), + credentials: { + accessKeyId: "AKIAIOSFODNN7EXAMPLE", + secretAccessKey: "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY", + }, + }); + + const clientB = new DynamoDBClient({ + region: "us-east-1", + endpoint: container.getEndpoint(), + credentials: { + accessKeyId: "AKIAIOSFODNN7EXAMPL2", + secretAccessKey: "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", + }, + }); + // } + + await createDummyTable(clientA, "users"); + + const resultA = await clientA.send(new ListTablesCommand()); + const resultB = await clientB.send(new ListTablesCommand()); + + // Eventhough clientA created the table, both should see it due to + // the shared table design + expect(resultA.TableNames).toStrictEqual(["users"]); + expect(resultB.TableNames).toStrictEqual(["users"]); + }); + + it("persists data when dbPath is set", async () => { + // persistWithDbPath { + await using container = await new DynamoDBContainer(IMAGE).withDbPath(".").start(); + const client = new DynamoDBClient(container.clientConfig()); + // } + + const { addUser } = await createDummyTable(client, "users"); + await addUser(1, "Bob"); + + await container.restart(); + + // restarting will change the mapped port so we need a new client + const clientAfterRestart = new DynamoDBClient(container.clientConfig()); + + // The table should still be there including its content + const command = new GetItemCommand({ + TableName: "users", + Key: { userId: { N: "1" } }, + ConsistentRead: true, + }); + const response = await clientAfterRestart.send(command); + expect(response.Item).toStrictEqual({ + userId: { N: "1" }, + name: { S: "Bob" }, + }); + }); + + it("stores data only in memory when requested", async () => { + // withInMemory { + await using container = await new DynamoDBContainer(IMAGE).withInMemory().start(); + const client = new DynamoDBClient(container.clientConfig()); + // } + + const { addUser } = await createDummyTable(client, "users"); + await addUser(1, "Bob"); + + await container.restart(); + + // restarting will change the mapped port so we need a new client + const clientAfterRestart = new DynamoDBClient(container.clientConfig()); + + // We should not be able to see the table anymore + const result = await clientAfterRestart.send(new ListTablesCommand()); + expect(result.TableNames).toStrictEqual([]); + }); + + it("allows to disable telemetry", async () => { + /* Unfortunately there is no way to validate this right now, but at + * least we know it does not pass something invalid to the container + */ + + // disabledTelemetry { + await using container = await new DynamoDBContainer(IMAGE).withDisableTelemetry().start(); + const client = new DynamoDBClient(container.clientConfig()); + // } + + const command = new ListTablesCommand({}); + const result = await client.send(command); + + expect(result.TableNames).toStrictEqual([]); + }); +}); diff --git a/packages/modules/dynamodb/src/dynamodb-container.ts b/packages/modules/dynamodb/src/dynamodb-container.ts new file mode 100644 index 000000000..556608f35 --- /dev/null +++ b/packages/modules/dynamodb/src/dynamodb-container.ts @@ -0,0 +1,182 @@ +import { AbstractStartedContainer, GenericContainer } from "testcontainers"; + +// https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/DynamoDBLocal.UsageNotes.html#DynamoDBLocal.Endpoint +const DYNAMODB_PORT = 8000; + +/* When running in AWS we do not have to pass any client configuration: + * + * * The default endpoint will be used + * * The region will be inferred from the lambda region + * * The credentials are taken from the lambda IAM role + * + * When connecting locally, we have to provide all three eventhough region + * and credentials are not really relevant. We want to help the user with + * a minimal configuration that works + */ +type DynamoDBLocalClientConfig = { + endpoint: string; + region: string; + credentials: { + readonly accessKeyId: string; + readonly secretAccessKey: string; + }; +}; + +export class DynamoDBContainer extends GenericContainer { + private sharedDB: boolean = false; + private inMemory: boolean = false; + private disableTelemetry: boolean = false; + private dbPath?: string; + + constructor(image: string) { + super(image); + this.withExposedPorts(DYNAMODB_PORT).withStartupTimeout(120_000); + } + + /** + * Share table between clients + * + * By default the DynamoDB container will present different tables + * depending on the current AWS access key. + * + * Use this option to use a shared database instead. This will + * pass ´-sharedDb` to the container. For more information please read + * https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/DynamoDBLocal.UsageNotes.html#DynamoDBLocal.CommandLineOptions + */ + public withSharedDB(): this { + this.sharedDB = true; + return this; + } + + /** + * Specify a database path + * + * Specify a directory (for example `.`) to persist the + * database. This can be used to preserve tables between container + * restarts but in general is not required. + * + * This option will set `-dbPath` when invoking the container. + * Fore more information please read + * https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/DynamoDBLocal.UsageNotes.html#DynamoDBLocal.CommandLineOptions + * + * @param dbPath - The path to the database file inside the container + */ + public withDbPath(dbPath: string): this { + this.dbPath = dbPath; + return this; + } + + /** + * Ensure the database is kept in memory + * + * Use this option to keep the complete database in memory. This will + * pass `-inMemory` to the container. + * Fore more information please read + * https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/DynamoDBLocal.UsageNotes.html#DynamoDBLocal.CommandLineOptions + */ + public withInMemory(): this { + this.inMemory = true; + return this; + } + + /** + * Do not sent any telemetry + * + * By default the AWS DynamoDB container will sent telemetry information. + * Use this option to pass `-disableTelemetry` to the container + * Fore more information please read + * https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/DynamoDBLocal.UsageNotes.html#DynamoDBLocal.CommandLineOptions + */ + public withDisableTelemetry(): this { + this.disableTelemetry = true; + return this; + } + + private createCommand(): string[] | null { + const command: string[] = []; + if (this.sharedDB) { + command.push("-sharedDb"); + } + if (this.inMemory) { + command.push("-inMemory"); + } + if (this.disableTelemetry) { + command.push("-disableTelemetry"); + } + if (this.dbPath) { + command.push("-dbPath", this.dbPath); + } + + if (command.length === 0) { + return null; + } + + // If we pass arguments we also have to pass + // -jar DynamoDBLocal.jar + return ["-jar", "DynamoDBLocal.jar", ...command]; + } + + /** + * Start the DynamoDB container + */ + public override async start(): Promise { + const command = this.createCommand(); + if (command) { + this.withCommand(command); + } + + const started = await super.start(); + return new StartedDynamoDBContainer(started); + } +} + +export class StartedDynamoDBContainer extends AbstractStartedContainer { + public getPort(): number { + return this.startedTestContainer.getMappedPort(DYNAMODB_PORT); + } + + /*+ + * Returns the endpoint to reach the DynamoDB service + * + * The returned endpoint can be used to pass to your DynamoDB client. You + * can also use `clientConfig` instead which provides you a complete + * client configuration suitable for most usecases + */ + public getEndpoint(): string { + const hostname = this.getHost(); + const url = new URL(`http://${hostname}`); + url.port = this.getPort().toString(); + return url.toString(); + } + + /** + * Returns a partial DynamoDBConfig + * + * When intializing a DynamoDB client inside AWS itself (e.g. inside a + * lambda) we do not have to pass any configuration settings. When + * connecting locally, we have to set at least `endpoint`, + * `region` (can be a dummy) and `credentials` (can also be a dummy). + * + * `clientConfig` will return minimal configuration with `endpoint`, a dummy + * `region` of `us-east-1` and dummy credentials. + * + * This allows you to intialize a client through + * + * ```js + * const client = new DynamoDBClient(container.clientConfig()); + * ``` + */ + public clientConfig(): DynamoDBLocalClientConfig { + // The AWS credentials you see here are fake credentials and taken from the + // following example: + // https://docs.aws.amazon.com/IAM/latest/UserGuide/id_credentials_access-keys.html + return { + endpoint: this.getEndpoint(), + region: "us-east-1", + credentials: { + accessKeyId: "AKIAIOSFODNN7EXAMPLE", + secretAccessKey: "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY", + }, + }; + } +} diff --git a/packages/modules/dynamodb/src/index.ts b/packages/modules/dynamodb/src/index.ts new file mode 100644 index 000000000..9c6beccd5 --- /dev/null +++ b/packages/modules/dynamodb/src/index.ts @@ -0,0 +1 @@ +export { DynamoDBContainer, StartedDynamoDBContainer } from "./dynamodb-container"; diff --git a/packages/modules/dynamodb/tsconfig.build.json b/packages/modules/dynamodb/tsconfig.build.json new file mode 100644 index 000000000..91dc68846 --- /dev/null +++ b/packages/modules/dynamodb/tsconfig.build.json @@ -0,0 +1,9 @@ +{ + "extends": "./tsconfig.json", + "exclude": ["build", "src/**/*.test.ts"], + "references": [ + { + "path": "../../testcontainers" + } + ] +} diff --git a/packages/modules/dynamodb/tsconfig.json b/packages/modules/dynamodb/tsconfig.json new file mode 100644 index 000000000..a06097b42 --- /dev/null +++ b/packages/modules/dynamodb/tsconfig.json @@ -0,0 +1,16 @@ +{ + "extends": "../../../tsconfig.base.json", + "compilerOptions": { + "rootDir": "src", + "outDir": "build", + "paths": { + "testcontainers": ["../../testcontainers/src"] + } + }, + "exclude": ["build"], + "references": [ + { + "path": "../../testcontainers" + } + ] +} From 72258fefbaef6f89f7b03bacd2fe12ee71509e59 Mon Sep 17 00:00:00 2001 From: Stefan Schulte Date: Mon, 26 Oct 2026 17:49:02 +0100 Subject: [PATCH 02/14] DynamoDB: Move helper beyond caller The project coding guidelines wants helper functions go beyond the caller, so we now move `createCommand` to the bottom. --- .../dynamodb/src/dynamodb-container.ts | 26 +++++++++---------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/packages/modules/dynamodb/src/dynamodb-container.ts b/packages/modules/dynamodb/src/dynamodb-container.ts index 556608f35..fd3cb4507 100644 --- a/packages/modules/dynamodb/src/dynamodb-container.ts +++ b/packages/modules/dynamodb/src/dynamodb-container.ts @@ -92,6 +92,19 @@ export class DynamoDBContainer extends GenericContainer { return this; } + /** + * Start the DynamoDB container + */ + public override async start(): Promise { + const command = this.createCommand(); + if (command) { + this.withCommand(command); + } + + const started = await super.start(); + return new StartedDynamoDBContainer(started); + } + private createCommand(): string[] | null { const command: string[] = []; if (this.sharedDB) { @@ -115,19 +128,6 @@ export class DynamoDBContainer extends GenericContainer { // -jar DynamoDBLocal.jar return ["-jar", "DynamoDBLocal.jar", ...command]; } - - /** - * Start the DynamoDB container - */ - public override async start(): Promise { - const command = this.createCommand(); - if (command) { - this.withCommand(command); - } - - const started = await super.start(); - return new StartedDynamoDBContainer(started); - } } export class StartedDynamoDBContainer extends AbstractStartedContainer { From 7a0dc86017cb4b4d0c6505988df3a2bb0064971e Mon Sep 17 00:00:00 2001 From: Stefan Schulte Date: Mon, 26 Oct 2026 17:56:54 +0100 Subject: [PATCH 03/14] DynamoDB: Remove withInMemory The default arguments of `dynamodb-local` already include `-inMemory` so we do not need a helper to enable it. Disabling should be possible when specifying a database path. --- .../dynamodb/src/dynamodb-container.test.ts | 6 ++--- .../dynamodb/src/dynamodb-container.ts | 25 ++++--------------- 2 files changed, 7 insertions(+), 24 deletions(-) diff --git a/packages/modules/dynamodb/src/dynamodb-container.test.ts b/packages/modules/dynamodb/src/dynamodb-container.test.ts index 0af0e79c2..004da208e 100644 --- a/packages/modules/dynamodb/src/dynamodb-container.test.ts +++ b/packages/modules/dynamodb/src/dynamodb-container.test.ts @@ -231,11 +231,9 @@ describe("DynamoDBContainer", { timeout: 180_000 }, () => { }); }); - it("stores data only in memory when requested", async () => { - // withInMemory { - await using container = await new DynamoDBContainer(IMAGE).withInMemory().start(); + it("stores data in memory when no dbPath set", async () => { + await using container = await new DynamoDBContainer(IMAGE).start(); const client = new DynamoDBClient(container.clientConfig()); - // } const { addUser } = await createDummyTable(client, "users"); await addUser(1, "Bob"); diff --git a/packages/modules/dynamodb/src/dynamodb-container.ts b/packages/modules/dynamodb/src/dynamodb-container.ts index fd3cb4507..1421b3176 100644 --- a/packages/modules/dynamodb/src/dynamodb-container.ts +++ b/packages/modules/dynamodb/src/dynamodb-container.ts @@ -24,7 +24,6 @@ type DynamoDBLocalClientConfig = { export class DynamoDBContainer extends GenericContainer { private sharedDB: boolean = false; - private inMemory: boolean = false; private disableTelemetry: boolean = false; private dbPath?: string; @@ -66,19 +65,6 @@ export class DynamoDBContainer extends GenericContainer { return this; } - /** - * Ensure the database is kept in memory - * - * Use this option to keep the complete database in memory. This will - * pass `-inMemory` to the container. - * Fore more information please read - * https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/DynamoDBLocal.UsageNotes.html#DynamoDBLocal.CommandLineOptions - */ - public withInMemory(): this { - this.inMemory = true; - return this; - } - /** * Do not sent any telemetry * @@ -107,26 +93,25 @@ export class DynamoDBContainer extends GenericContainer { private createCommand(): string[] | null { const command: string[] = []; + let skipInMemory = false; if (this.sharedDB) { command.push("-sharedDb"); } - if (this.inMemory) { - command.push("-inMemory"); - } if (this.disableTelemetry) { command.push("-disableTelemetry"); } if (this.dbPath) { command.push("-dbPath", this.dbPath); + skipInMemory = true; } if (command.length === 0) { return null; } - // If we pass arguments we also have to pass - // -jar DynamoDBLocal.jar - return ["-jar", "DynamoDBLocal.jar", ...command]; + // The defaut command is + // -jar DynamoDBLocal.jar -inMemory + return ["-jar", "DynamoDBLocal.jar", ...(skipInMemory ? [] : ["-inMemory"]), ...command]; } } From ac4984bb08798c1858725aef876827d9eb1a4815 Mon Sep 17 00:00:00 2001 From: Stefan Schulte Date: Mon, 26 Oct 2026 20:26:25 +0100 Subject: [PATCH 04/14] DynamoDB: Drop support for client config --- docs/modules/dynamodb.md | 8 -- .../dynamodb/src/dynamodb-container.test.ts | 73 ++++++++++++++----- .../dynamodb/src/dynamodb-container.ts | 50 ------------- 3 files changed, 55 insertions(+), 76 deletions(-) diff --git a/docs/modules/dynamodb.md b/docs/modules/dynamodb.md index c1ff070b7..d93da13e2 100644 --- a/docs/modules/dynamodb.md +++ b/docs/modules/dynamodb.md @@ -26,14 +26,6 @@ will not be used: [](../../packages/modules/dynamodb/src/dynamodb-container.test.ts) inside_block:connectEndpoint -### Connect via client config - -The above can also be written as - - -[](../../packages/modules/dynamodb/src/dynamodb-container.test.ts) inside_block:connectClientConfig - - ### Use a shared database diff --git a/packages/modules/dynamodb/src/dynamodb-container.test.ts b/packages/modules/dynamodb/src/dynamodb-container.test.ts index 004da208e..97253d721 100644 --- a/packages/modules/dynamodb/src/dynamodb-container.test.ts +++ b/packages/modules/dynamodb/src/dynamodb-container.test.ts @@ -86,24 +86,13 @@ async function createDummyTable( } describe("DynamoDBContainer", { timeout: 180_000 }, () => { - it("starts a container", async () => { - // connectClientConfig { - await using container = await new DynamoDBContainer(IMAGE).start(); - const client = new DynamoDBClient(container.clientConfig()); - - const command = new ListTablesCommand({}); - const result = await client.send(command); - - expect(result.TableNames).toStrictEqual([]); - // } - }); - it("connects with endpoint", async () => { // connectEndpoint { await using container = await new DynamoDBContainer(IMAGE).start(); const client = new DynamoDBClient({ region: "us-east-1", credentials: { + // use any credentials accessKeyId: "AKIAIOSFODNN7EXAMPLE", secretAccessKey: "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY", }, @@ -119,7 +108,15 @@ describe("DynamoDBContainer", { timeout: 180_000 }, () => { it("stores data", async () => { await using container = await new DynamoDBContainer(IMAGE).start(); - const client = new DynamoDBClient(container.clientConfig()); + const client = new DynamoDBClient({ + region: "us-east-1", + credentials: { + // use any credentials + accessKeyId: "AKIAIOSFODNN7EXAMPLE", + secretAccessKey: "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY", + }, + endpoint: container.getEndpoint(), + }); const { teardown, addUser, getUser } = await createDummyTable(client, "users"); @@ -207,7 +204,15 @@ describe("DynamoDBContainer", { timeout: 180_000 }, () => { it("persists data when dbPath is set", async () => { // persistWithDbPath { await using container = await new DynamoDBContainer(IMAGE).withDbPath(".").start(); - const client = new DynamoDBClient(container.clientConfig()); + const client = new DynamoDBClient({ + region: "us-east-1", + credentials: { + // use any credentials + accessKeyId: "AKIAIOSFODNN7EXAMPLE", + secretAccessKey: "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY", + }, + endpoint: container.getEndpoint(), + }); // } const { addUser } = await createDummyTable(client, "users"); @@ -216,7 +221,15 @@ describe("DynamoDBContainer", { timeout: 180_000 }, () => { await container.restart(); // restarting will change the mapped port so we need a new client - const clientAfterRestart = new DynamoDBClient(container.clientConfig()); + const clientAfterRestart = new DynamoDBClient({ + region: "us-east-1", + credentials: { + // use any credentials + accessKeyId: "AKIAIOSFODNN7EXAMPLE", + secretAccessKey: "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY", + }, + endpoint: container.getEndpoint(), + }); // The table should still be there including its content const command = new GetItemCommand({ @@ -233,7 +246,15 @@ describe("DynamoDBContainer", { timeout: 180_000 }, () => { it("stores data in memory when no dbPath set", async () => { await using container = await new DynamoDBContainer(IMAGE).start(); - const client = new DynamoDBClient(container.clientConfig()); + const client = new DynamoDBClient({ + region: "us-east-1", + credentials: { + // use any credentials + accessKeyId: "AKIAIOSFODNN7EXAMPLE", + secretAccessKey: "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY", + }, + endpoint: container.getEndpoint(), + }); const { addUser } = await createDummyTable(client, "users"); await addUser(1, "Bob"); @@ -241,7 +262,15 @@ describe("DynamoDBContainer", { timeout: 180_000 }, () => { await container.restart(); // restarting will change the mapped port so we need a new client - const clientAfterRestart = new DynamoDBClient(container.clientConfig()); + const clientAfterRestart = new DynamoDBClient({ + region: "us-east-1", + credentials: { + // use any credentials + accessKeyId: "AKIAIOSFODNN7EXAMPLE", + secretAccessKey: "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY", + }, + endpoint: container.getEndpoint(), + }); // We should not be able to see the table anymore const result = await clientAfterRestart.send(new ListTablesCommand()); @@ -255,7 +284,15 @@ describe("DynamoDBContainer", { timeout: 180_000 }, () => { // disabledTelemetry { await using container = await new DynamoDBContainer(IMAGE).withDisableTelemetry().start(); - const client = new DynamoDBClient(container.clientConfig()); + const client = new DynamoDBClient({ + region: "us-east-1", + credentials: { + // use any credentials + accessKeyId: "AKIAIOSFODNN7EXAMPLE", + secretAccessKey: "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY", + }, + endpoint: container.getEndpoint(), + }); // } const command = new ListTablesCommand({}); diff --git a/packages/modules/dynamodb/src/dynamodb-container.ts b/packages/modules/dynamodb/src/dynamodb-container.ts index 1421b3176..f7ff6bce5 100644 --- a/packages/modules/dynamodb/src/dynamodb-container.ts +++ b/packages/modules/dynamodb/src/dynamodb-container.ts @@ -3,25 +3,6 @@ import { AbstractStartedContainer, GenericContainer } from "testcontainers"; // https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/DynamoDBLocal.UsageNotes.html#DynamoDBLocal.Endpoint const DYNAMODB_PORT = 8000; -/* When running in AWS we do not have to pass any client configuration: - * - * * The default endpoint will be used - * * The region will be inferred from the lambda region - * * The credentials are taken from the lambda IAM role - * - * When connecting locally, we have to provide all three eventhough region - * and credentials are not really relevant. We want to help the user with - * a minimal configuration that works - */ -type DynamoDBLocalClientConfig = { - endpoint: string; - region: string; - credentials: { - readonly accessKeyId: string; - readonly secretAccessKey: string; - }; -}; - export class DynamoDBContainer extends GenericContainer { private sharedDB: boolean = false; private disableTelemetry: boolean = false; @@ -133,35 +114,4 @@ export class StartedDynamoDBContainer extends AbstractStartedContainer { url.port = this.getPort().toString(); return url.toString(); } - - /** - * Returns a partial DynamoDBConfig - * - * When intializing a DynamoDB client inside AWS itself (e.g. inside a - * lambda) we do not have to pass any configuration settings. When - * connecting locally, we have to set at least `endpoint`, - * `region` (can be a dummy) and `credentials` (can also be a dummy). - * - * `clientConfig` will return minimal configuration with `endpoint`, a dummy - * `region` of `us-east-1` and dummy credentials. - * - * This allows you to intialize a client through - * - * ```js - * const client = new DynamoDBClient(container.clientConfig()); - * ``` - */ - public clientConfig(): DynamoDBLocalClientConfig { - // The AWS credentials you see here are fake credentials and taken from the - // following example: - // https://docs.aws.amazon.com/IAM/latest/UserGuide/id_credentials_access-keys.html - return { - endpoint: this.getEndpoint(), - region: "us-east-1", - credentials: { - accessKeyId: "AKIAIOSFODNN7EXAMPLE", - secretAccessKey: "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY", - }, - }; - } } From c071c31ca1cceffdd1ecdec803aa8a458e56917d Mon Sep 17 00:00:00 2001 From: Stefan Schulte Date: Mon, 26 Oct 2026 20:43:28 +0100 Subject: [PATCH 05/14] DynamoDB: Improve testcase for withDisableTelemetry We cannot really observe a change in behaviour to validate telemetry is disabled but we can at least validate we are passing the correct command to our container. --- .../modules/dynamodb/src/dynamodb-container.test.ts | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/packages/modules/dynamodb/src/dynamodb-container.test.ts b/packages/modules/dynamodb/src/dynamodb-container.test.ts index 97253d721..c6abcdd2d 100644 --- a/packages/modules/dynamodb/src/dynamodb-container.test.ts +++ b/packages/modules/dynamodb/src/dynamodb-container.test.ts @@ -283,7 +283,13 @@ describe("DynamoDBContainer", { timeout: 180_000 }, () => { */ // disabledTelemetry { - await using container = await new DynamoDBContainer(IMAGE).withDisableTelemetry().start(); + const container = new DynamoDBContainer(IMAGE).withDisableTelemetry(); + await using startedContainer = await container.start(); + // } + + // @ts-expect-error - accessing private property for testing + expect(container.createOpts.Cmd).toEqual(["-jar", "DynamoDBLocal.jar", "-inMemory", "-disableTelemetry"]); + const client = new DynamoDBClient({ region: "us-east-1", credentials: { @@ -291,9 +297,8 @@ describe("DynamoDBContainer", { timeout: 180_000 }, () => { accessKeyId: "AKIAIOSFODNN7EXAMPLE", secretAccessKey: "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY", }, - endpoint: container.getEndpoint(), + endpoint: startedContainer.getEndpoint(), }); - // } const command = new ListTablesCommand({}); const result = await client.send(command); From 247ce279503e139b302ecca5b0baa6fc5398920e Mon Sep 17 00:00:00 2001 From: Stefan Schulte Date: Mon, 26 Oct 2026 20:45:52 +0100 Subject: [PATCH 06/14] DynamoDB: Do not import globals Vitest is currently configured with ```typescript export default defineConfig({ test: { globals: true, } } ``` So we do not have to import `describe`, `it`, `expect`, etc. Drop those imports. --- packages/modules/dynamodb/src/dynamodb-container.test.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/modules/dynamodb/src/dynamodb-container.test.ts b/packages/modules/dynamodb/src/dynamodb-container.test.ts index c6abcdd2d..aaaa75842 100644 --- a/packages/modules/dynamodb/src/dynamodb-container.test.ts +++ b/packages/modules/dynamodb/src/dynamodb-container.test.ts @@ -8,7 +8,6 @@ import { waitUntilTableExists, waitUntilTableNotExists, } from "@aws-sdk/client-dynamodb"; -import { describe, it } from "vitest"; import { getImage } from "../../../testcontainers/src/utils/test-helper"; import { DynamoDBContainer } from "./dynamodb-container"; From 2119fbf50690b00c1a659a4437a8d2ece11a3f69 Mon Sep 17 00:00:00 2001 From: Stefan Schulte Date: Mon, 26 Oct 2026 21:19:32 +0100 Subject: [PATCH 07/14] DynamoDB: Adapt wait strategy We now adopt the same wait strategy that is used in other languages that support DynamoDB --- packages/modules/dynamodb/src/dynamodb-container.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/modules/dynamodb/src/dynamodb-container.ts b/packages/modules/dynamodb/src/dynamodb-container.ts index f7ff6bce5..9332c399e 100644 --- a/packages/modules/dynamodb/src/dynamodb-container.ts +++ b/packages/modules/dynamodb/src/dynamodb-container.ts @@ -1,4 +1,4 @@ -import { AbstractStartedContainer, GenericContainer } from "testcontainers"; +import { AbstractStartedContainer, GenericContainer, Wait } from "testcontainers"; // https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/DynamoDBLocal.UsageNotes.html#DynamoDBLocal.Endpoint const DYNAMODB_PORT = 8000; @@ -10,7 +10,7 @@ export class DynamoDBContainer extends GenericContainer { constructor(image: string) { super(image); - this.withExposedPorts(DYNAMODB_PORT).withStartupTimeout(120_000); + this.withExposedPorts(DYNAMODB_PORT).withWaitStrategy(Wait.forHttp("/", DYNAMODB_PORT).forStatusCode(400)); } /** From 11fd887c03efac38775f8ee149edfdcd99acc292 Mon Sep 17 00:00:00 2001 From: Stefan Schulte Date: Mon, 26 Oct 2026 21:24:45 +0100 Subject: [PATCH 08/14] DynamoDB: Shorten documentation Most of the documentation seems to assume the application inside the behaviour is well understood so we now also drop additional explanation around the usage of the DynamoDB container. --- docs/modules/dynamodb.md | 4 ---- 1 file changed, 4 deletions(-) diff --git a/docs/modules/dynamodb.md b/docs/modules/dynamodb.md index d93da13e2..6d2faead8 100644 --- a/docs/modules/dynamodb.md +++ b/docs/modules/dynamodb.md @@ -18,10 +18,6 @@ Choose an image from the [container registry](https://hub.docker.com/r/amazon/dy ### Connect to local endpoint -In order to use a DynamoDB Client we have to specify -at least `region`, `credentials` and `endpoint` eventhough `region` and `credentials` -will not be used: - [](../../packages/modules/dynamodb/src/dynamodb-container.test.ts) inside_block:connectEndpoint From 726a7f0deb5d1f63b92cdfa07abc6b3e6c994f31 Mon Sep 17 00:00:00 2001 From: Stefan Schulte Date: Mon, 26 Oct 2026 22:01:29 +0100 Subject: [PATCH 09/14] DynamoDB: Simplify helper function In order to easily create a table with dummy data inside our tests we introduced a small helper function. We now cut functionality we don't really require like changing the table name or returning a function to destroy the table. Since we throw away the whole container there is not reason to delete a single table. --- .../dynamodb/src/dynamodb-container.test.ts | 51 +++++-------------- 1 file changed, 12 insertions(+), 39 deletions(-) diff --git a/packages/modules/dynamodb/src/dynamodb-container.test.ts b/packages/modules/dynamodb/src/dynamodb-container.test.ts index aaaa75842..d904e008c 100644 --- a/packages/modules/dynamodb/src/dynamodb-container.test.ts +++ b/packages/modules/dynamodb/src/dynamodb-container.test.ts @@ -1,38 +1,22 @@ import { CreateTableCommand, - DeleteTableCommand, DynamoDBClient, GetItemCommand, ListTablesCommand, PutItemCommand, waitUntilTableExists, - waitUntilTableNotExists, } from "@aws-sdk/client-dynamodb"; import { getImage } from "../../../testcontainers/src/utils/test-helper"; import { DynamoDBContainer } from "./dynamodb-container"; const IMAGE = getImage(__dirname); -/** - * Create an empty dummy user table - * - * This function can be used in test cases - * to easily add a new table. - * - * @param client - DynamoDBClient pointing to the container - * @param tableName - The desired table name - * @returns - Three functions: teardown, addUser, getUser - */ -async function createDummyTable( - client: DynamoDBClient, - tableName: string -): Promise<{ - teardown: () => Promise; +async function createTestTable(client: DynamoDBClient): Promise<{ addUser: (userId: number, name: string) => Promise; getUser: (userId: number) => Promise; }> { const command = new CreateTableCommand({ - TableName: tableName, + TableName: "users", BillingMode: "PAY_PER_REQUEST", AttributeDefinitions: [ { @@ -49,17 +33,12 @@ async function createDummyTable( }); await client.send(command); - await waitUntilTableExists({ client, maxWaitTime: 30 }, { TableName: tableName }); + await waitUntilTableExists({ client, maxWaitTime: 30 }, { TableName: "users" }); return { - teardown: async (): Promise => { - const deleteCommand = new DeleteTableCommand({ TableName: tableName }); - await client.send(deleteCommand); - await waitUntilTableNotExists({ client, maxWaitTime: 30 }, { TableName: tableName }); - }, addUser: async (userId, name) => { const putCommand = new PutItemCommand({ - TableName: tableName, + TableName: "users", Item: { userId: { N: userId.toString() }, name: { S: name }, @@ -69,17 +48,14 @@ async function createDummyTable( }, getUser: async (userId) => { const getCommand = new GetItemCommand({ - TableName: tableName, + TableName: "users", Key: { userId: { N: userId.toString() }, }, ConsistentRead: true, }); - const response = await client.send(getCommand); - if (response.Item?.name?.S) { - return response.Item.name.S; - } - return null; + const result = await client.send(getCommand); + return result.Item?.name?.S ?? null; }, }; } @@ -117,8 +93,7 @@ describe("DynamoDBContainer", { timeout: 180_000 }, () => { endpoint: container.getEndpoint(), }); - const { teardown, addUser, getUser } = await createDummyTable(client, "users"); - + const { addUser, getUser } = await createTestTable(client); await addUser(1, "Bob"); await addUser(2, "Alice"); @@ -126,8 +101,6 @@ describe("DynamoDBContainer", { timeout: 180_000 }, () => { const userB = await getUser(2); expect(userA).toBe("Bob"); expect(userB).toBe("Alice"); - - await teardown(); }); it("separates data by client by default", async () => { @@ -154,7 +127,7 @@ describe("DynamoDBContainer", { timeout: 180_000 }, () => { }, }); - await createDummyTable(clientA, "users"); + await createTestTable(clientA); const resultA = await clientA.send(new ListTablesCommand()); const resultB = await clientB.send(new ListTablesCommand()); @@ -189,7 +162,7 @@ describe("DynamoDBContainer", { timeout: 180_000 }, () => { }); // } - await createDummyTable(clientA, "users"); + await createTestTable(clientA); const resultA = await clientA.send(new ListTablesCommand()); const resultB = await clientB.send(new ListTablesCommand()); @@ -214,7 +187,7 @@ describe("DynamoDBContainer", { timeout: 180_000 }, () => { }); // } - const { addUser } = await createDummyTable(client, "users"); + const { addUser } = await createTestTable(client); await addUser(1, "Bob"); await container.restart(); @@ -255,7 +228,7 @@ describe("DynamoDBContainer", { timeout: 180_000 }, () => { endpoint: container.getEndpoint(), }); - const { addUser } = await createDummyTable(client, "users"); + const { addUser } = await createTestTable(client); await addUser(1, "Bob"); await container.restart(); From dafbbd79269702def2cc524347f40381b5b1ae1f Mon Sep 17 00:00:00 2001 From: Stefan Schulte Date: Mon, 26 Oct 2026 22:27:22 +0100 Subject: [PATCH 10/14] DynamoDB: Simplify construction of getEndpoint There a a lot of ways to construct a URL, so use what seems to be best practice inside this repository. --- packages/modules/dynamodb/src/dynamodb-container.ts | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/packages/modules/dynamodb/src/dynamodb-container.ts b/packages/modules/dynamodb/src/dynamodb-container.ts index 9332c399e..0c1503171 100644 --- a/packages/modules/dynamodb/src/dynamodb-container.ts +++ b/packages/modules/dynamodb/src/dynamodb-container.ts @@ -109,9 +109,7 @@ export class StartedDynamoDBContainer extends AbstractStartedContainer { * client configuration suitable for most usecases */ public getEndpoint(): string { - const hostname = this.getHost(); - const url = new URL(`http://${hostname}`); - url.port = this.getPort().toString(); - return url.toString(); + const url = `http://${this.getHost()}:${this.getPort().toString()}`; + return url; } } From cccf1fa4fd39373ac74074174ca457067062141f Mon Sep 17 00:00:00 2001 From: Stefan Schulte Date: Wed, 18 Nov 2026 12:14:38 +0100 Subject: [PATCH 11/14] DynamoDB: Disable dbPath and withTelemetry After PR comments we now drop dbPath since there is no current usecase for it and we disable telemetry by default instead of providing it as an option. --- docs/modules/dynamodb.md | 6 - .../dynamodb/src/dynamodb-container.test.ts | 149 ++---------------- .../dynamodb/src/dynamodb-container.ts | 90 +---------- 3 files changed, 24 insertions(+), 221 deletions(-) diff --git a/docs/modules/dynamodb.md b/docs/modules/dynamodb.md index 6d2faead8..8ae64ea2b 100644 --- a/docs/modules/dynamodb.md +++ b/docs/modules/dynamodb.md @@ -27,9 +27,3 @@ Choose an image from the [container registry](https://hub.docker.com/r/amazon/dy [](../../packages/modules/dynamodb/src/dynamodb-container.test.ts) inside_block:sharedDB - -### Disable telemetry - - -[](../../packages/modules/dynamodb/src/dynamodb-container.test.ts) inside_block:disabledTelemetry - diff --git a/packages/modules/dynamodb/src/dynamodb-container.test.ts b/packages/modules/dynamodb/src/dynamodb-container.test.ts index d904e008c..3bd0340eb 100644 --- a/packages/modules/dynamodb/src/dynamodb-container.test.ts +++ b/packages/modules/dynamodb/src/dynamodb-container.test.ts @@ -60,6 +60,19 @@ async function createTestTable(client: DynamoDBClient): Promise<{ }; } +function createClient(endpoint: string, accessKeyId: string = "AKIAIOSFODNN7EXAMPLE"): DynamoDBClient { + const client = new DynamoDBClient({ + region: "us-east-1", + credentials: { + // use any credentials + accessKeyId, + secretAccessKey: "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY", + }, + endpoint, + }); + return client; +} + describe("DynamoDBContainer", { timeout: 180_000 }, () => { it("connects with endpoint", async () => { // connectEndpoint { @@ -83,15 +96,7 @@ describe("DynamoDBContainer", { timeout: 180_000 }, () => { it("stores data", async () => { await using container = await new DynamoDBContainer(IMAGE).start(); - const client = new DynamoDBClient({ - region: "us-east-1", - credentials: { - // use any credentials - accessKeyId: "AKIAIOSFODNN7EXAMPLE", - secretAccessKey: "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY", - }, - endpoint: container.getEndpoint(), - }); + const client = createClient(container.getEndpoint()); const { addUser, getUser } = await createTestTable(client); await addUser(1, "Bob"); @@ -107,25 +112,8 @@ describe("DynamoDBContainer", { timeout: 180_000 }, () => { await using container = await new DynamoDBContainer(IMAGE).start(); // Uses dummy credentials from the documentation - const clientA = new DynamoDBClient({ - region: "eu-central-1", - endpoint: container.getEndpoint(), - credentials: { - accessKeyId: "AKIAIOSFODNN7EXAMPLE", - secretAccessKey: "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY", - }, - }); - - // Uses slightly different credentials to simulate another - // AWS account - const clientB = new DynamoDBClient({ - region: "eu-central-1", - endpoint: container.getEndpoint(), - credentials: { - accessKeyId: "AKIAIOSFODNN7EXAMPL2", - secretAccessKey: "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", - }, - }); + const clientA = createClient(container.getEndpoint(), "AKIAIOSFODNN7EXAMPL1"); + const clientB = createClient(container.getEndpoint(), "AKIAIOSFODNN7EXAMPL2"); await createTestTable(clientA); @@ -172,109 +160,4 @@ describe("DynamoDBContainer", { timeout: 180_000 }, () => { expect(resultA.TableNames).toStrictEqual(["users"]); expect(resultB.TableNames).toStrictEqual(["users"]); }); - - it("persists data when dbPath is set", async () => { - // persistWithDbPath { - await using container = await new DynamoDBContainer(IMAGE).withDbPath(".").start(); - const client = new DynamoDBClient({ - region: "us-east-1", - credentials: { - // use any credentials - accessKeyId: "AKIAIOSFODNN7EXAMPLE", - secretAccessKey: "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY", - }, - endpoint: container.getEndpoint(), - }); - // } - - const { addUser } = await createTestTable(client); - await addUser(1, "Bob"); - - await container.restart(); - - // restarting will change the mapped port so we need a new client - const clientAfterRestart = new DynamoDBClient({ - region: "us-east-1", - credentials: { - // use any credentials - accessKeyId: "AKIAIOSFODNN7EXAMPLE", - secretAccessKey: "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY", - }, - endpoint: container.getEndpoint(), - }); - - // The table should still be there including its content - const command = new GetItemCommand({ - TableName: "users", - Key: { userId: { N: "1" } }, - ConsistentRead: true, - }); - const response = await clientAfterRestart.send(command); - expect(response.Item).toStrictEqual({ - userId: { N: "1" }, - name: { S: "Bob" }, - }); - }); - - it("stores data in memory when no dbPath set", async () => { - await using container = await new DynamoDBContainer(IMAGE).start(); - const client = new DynamoDBClient({ - region: "us-east-1", - credentials: { - // use any credentials - accessKeyId: "AKIAIOSFODNN7EXAMPLE", - secretAccessKey: "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY", - }, - endpoint: container.getEndpoint(), - }); - - const { addUser } = await createTestTable(client); - await addUser(1, "Bob"); - - await container.restart(); - - // restarting will change the mapped port so we need a new client - const clientAfterRestart = new DynamoDBClient({ - region: "us-east-1", - credentials: { - // use any credentials - accessKeyId: "AKIAIOSFODNN7EXAMPLE", - secretAccessKey: "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY", - }, - endpoint: container.getEndpoint(), - }); - - // We should not be able to see the table anymore - const result = await clientAfterRestart.send(new ListTablesCommand()); - expect(result.TableNames).toStrictEqual([]); - }); - - it("allows to disable telemetry", async () => { - /* Unfortunately there is no way to validate this right now, but at - * least we know it does not pass something invalid to the container - */ - - // disabledTelemetry { - const container = new DynamoDBContainer(IMAGE).withDisableTelemetry(); - await using startedContainer = await container.start(); - // } - - // @ts-expect-error - accessing private property for testing - expect(container.createOpts.Cmd).toEqual(["-jar", "DynamoDBLocal.jar", "-inMemory", "-disableTelemetry"]); - - const client = new DynamoDBClient({ - region: "us-east-1", - credentials: { - // use any credentials - accessKeyId: "AKIAIOSFODNN7EXAMPLE", - secretAccessKey: "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY", - }, - endpoint: startedContainer.getEndpoint(), - }); - - const command = new ListTablesCommand({}); - const result = await client.send(command); - - expect(result.TableNames).toStrictEqual([]); - }); }); diff --git a/packages/modules/dynamodb/src/dynamodb-container.ts b/packages/modules/dynamodb/src/dynamodb-container.ts index 0c1503171..066281522 100644 --- a/packages/modules/dynamodb/src/dynamodb-container.ts +++ b/packages/modules/dynamodb/src/dynamodb-container.ts @@ -5,95 +5,28 @@ const DYNAMODB_PORT = 8000; export class DynamoDBContainer extends GenericContainer { private sharedDB: boolean = false; - private disableTelemetry: boolean = false; - private dbPath?: string; constructor(image: string) { super(image); - this.withExposedPorts(DYNAMODB_PORT).withWaitStrategy(Wait.forHttp("/", DYNAMODB_PORT).forStatusCode(400)); + this.withExposedPorts(DYNAMODB_PORT) + .withStartupTimeout(120_000) + .withWaitStrategy(Wait.forHttp("/", DYNAMODB_PORT).forStatusCode(400)); } - /** - * Share table between clients - * - * By default the DynamoDB container will present different tables - * depending on the current AWS access key. - * - * Use this option to use a shared database instead. This will - * pass ´-sharedDb` to the container. For more information please read - * https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/DynamoDBLocal.UsageNotes.html#DynamoDBLocal.CommandLineOptions - */ public withSharedDB(): this { this.sharedDB = true; return this; } - /** - * Specify a database path - * - * Specify a directory (for example `.`) to persist the - * database. This can be used to preserve tables between container - * restarts but in general is not required. - * - * This option will set `-dbPath` when invoking the container. - * Fore more information please read - * https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/DynamoDBLocal.UsageNotes.html#DynamoDBLocal.CommandLineOptions - * - * @param dbPath - The path to the database file inside the container - */ - public withDbPath(dbPath: string): this { - this.dbPath = dbPath; - return this; - } - - /** - * Do not sent any telemetry - * - * By default the AWS DynamoDB container will sent telemetry information. - * Use this option to pass `-disableTelemetry` to the container - * Fore more information please read - * https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/DynamoDBLocal.UsageNotes.html#DynamoDBLocal.CommandLineOptions - */ - public withDisableTelemetry(): this { - this.disableTelemetry = true; - return this; - } - - /** - * Start the DynamoDB container - */ public override async start(): Promise { - const command = this.createCommand(); - if (command) { - this.withCommand(command); - } + this.withCommand(["-jar", "DynamoDBLocal.jar", "-inMemory", ...(this.sharedDB ? ["-sharedDb"] : [])]); + this.withEnvironment({ + DDB_LOCAL_TELEMETRY: "0", + }); const started = await super.start(); return new StartedDynamoDBContainer(started); } - - private createCommand(): string[] | null { - const command: string[] = []; - let skipInMemory = false; - if (this.sharedDB) { - command.push("-sharedDb"); - } - if (this.disableTelemetry) { - command.push("-disableTelemetry"); - } - if (this.dbPath) { - command.push("-dbPath", this.dbPath); - skipInMemory = true; - } - - if (command.length === 0) { - return null; - } - - // The defaut command is - // -jar DynamoDBLocal.jar -inMemory - return ["-jar", "DynamoDBLocal.jar", ...(skipInMemory ? [] : ["-inMemory"]), ...command]; - } } export class StartedDynamoDBContainer extends AbstractStartedContainer { @@ -101,15 +34,8 @@ export class StartedDynamoDBContainer extends AbstractStartedContainer { return this.startedTestContainer.getMappedPort(DYNAMODB_PORT); } - /*+ - * Returns the endpoint to reach the DynamoDB service - * - * The returned endpoint can be used to pass to your DynamoDB client. You - * can also use `clientConfig` instead which provides you a complete - * client configuration suitable for most usecases - */ public getEndpoint(): string { - const url = `http://${this.getHost()}:${this.getPort().toString()}`; + const url = `http://${this.getHost()}:${this.getPort()}`; return url; } } From 2071e88e13dbac24638ad02588476d3d802cb8b6 Mon Sep 17 00:00:00 2001 From: Stefan Schulte Date: Fri, 20 Nov 2026 09:12:17 +0100 Subject: [PATCH 12/14] DynamoDB: Move withEnvironment to constructor Give users a change to overwrite the environment --- packages/modules/dynamodb/src/dynamodb-container.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/modules/dynamodb/src/dynamodb-container.ts b/packages/modules/dynamodb/src/dynamodb-container.ts index 066281522..a32a9ab0c 100644 --- a/packages/modules/dynamodb/src/dynamodb-container.ts +++ b/packages/modules/dynamodb/src/dynamodb-container.ts @@ -10,7 +10,10 @@ export class DynamoDBContainer extends GenericContainer { super(image); this.withExposedPorts(DYNAMODB_PORT) .withStartupTimeout(120_000) - .withWaitStrategy(Wait.forHttp("/", DYNAMODB_PORT).forStatusCode(400)); + .withWaitStrategy(Wait.forHttp("/", DYNAMODB_PORT).forStatusCode(400)) + .withEnvironment({ + DDB_LOCAL_TELEMETRY: "0", + }); } public withSharedDB(): this { @@ -20,9 +23,6 @@ export class DynamoDBContainer extends GenericContainer { public override async start(): Promise { this.withCommand(["-jar", "DynamoDBLocal.jar", "-inMemory", ...(this.sharedDB ? ["-sharedDb"] : [])]); - this.withEnvironment({ - DDB_LOCAL_TELEMETRY: "0", - }); const started = await super.start(); return new StartedDynamoDBContainer(started); From 2ffb5168059fa9832b064365204d0f41be1b451d Mon Sep 17 00:00:00 2001 From: Stefan Schulte Date: Fri, 20 Nov 2026 09:12:42 +0100 Subject: [PATCH 13/14] DynamoDB: Remove comments --- packages/modules/dynamodb/src/dynamodb-container.test.ts | 5 ----- 1 file changed, 5 deletions(-) diff --git a/packages/modules/dynamodb/src/dynamodb-container.test.ts b/packages/modules/dynamodb/src/dynamodb-container.test.ts index 3bd0340eb..1538731ee 100644 --- a/packages/modules/dynamodb/src/dynamodb-container.test.ts +++ b/packages/modules/dynamodb/src/dynamodb-container.test.ts @@ -64,7 +64,6 @@ function createClient(endpoint: string, accessKeyId: string = "AKIAIOSFODNN7EXAM const client = new DynamoDBClient({ region: "us-east-1", credentials: { - // use any credentials accessKeyId, secretAccessKey: "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY", }, @@ -111,7 +110,6 @@ describe("DynamoDBContainer", { timeout: 180_000 }, () => { it("separates data by client by default", async () => { await using container = await new DynamoDBContainer(IMAGE).start(); - // Uses dummy credentials from the documentation const clientA = createClient(container.getEndpoint(), "AKIAIOSFODNN7EXAMPL1"); const clientB = createClient(container.getEndpoint(), "AKIAIOSFODNN7EXAMPL2"); @@ -120,7 +118,6 @@ describe("DynamoDBContainer", { timeout: 180_000 }, () => { const resultA = await clientA.send(new ListTablesCommand()); const resultB = await clientB.send(new ListTablesCommand()); - // Since clientA created the table, only clientA should see it expect(resultA.TableNames).toStrictEqual(["users"]); expect(resultB.TableNames).toStrictEqual([]); }); @@ -155,8 +152,6 @@ describe("DynamoDBContainer", { timeout: 180_000 }, () => { const resultA = await clientA.send(new ListTablesCommand()); const resultB = await clientB.send(new ListTablesCommand()); - // Eventhough clientA created the table, both should see it due to - // the shared table design expect(resultA.TableNames).toStrictEqual(["users"]); expect(resultB.TableNames).toStrictEqual(["users"]); }); From 3a9bb8f9d2838f366ab38f371250bbd21e4f64d7 Mon Sep 17 00:00:00 2001 From: Cristian Greco Date: Fri, 20 Nov 2026 11:13:24 +0000 Subject: [PATCH 14/14] DynamoDB: Return expressions directly and fix typo --- packages/modules/dynamodb/src/dynamodb-container.test.ts | 5 ++--- packages/modules/dynamodb/src/dynamodb-container.ts | 3 +-- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/packages/modules/dynamodb/src/dynamodb-container.test.ts b/packages/modules/dynamodb/src/dynamodb-container.test.ts index 1538731ee..55008cc38 100644 --- a/packages/modules/dynamodb/src/dynamodb-container.test.ts +++ b/packages/modules/dynamodb/src/dynamodb-container.test.ts @@ -61,7 +61,7 @@ async function createTestTable(client: DynamoDBClient): Promise<{ } function createClient(endpoint: string, accessKeyId: string = "AKIAIOSFODNN7EXAMPLE"): DynamoDBClient { - const client = new DynamoDBClient({ + return new DynamoDBClient({ region: "us-east-1", credentials: { accessKeyId, @@ -69,7 +69,6 @@ function createClient(endpoint: string, accessKeyId: string = "AKIAIOSFODNN7EXAM }, endpoint, }); - return client; } describe("DynamoDBContainer", { timeout: 180_000 }, () => { @@ -126,7 +125,7 @@ describe("DynamoDBContainer", { timeout: 180_000 }, () => { // sharedDB { await using container = await new DynamoDBContainer(IMAGE).withSharedDB().start(); - // Both clients will see the same data eventhough the + // Both clients will see the same data even though the // ACCESS_KEY_ID differ const clientA = new DynamoDBClient({ region: "us-east-1", diff --git a/packages/modules/dynamodb/src/dynamodb-container.ts b/packages/modules/dynamodb/src/dynamodb-container.ts index a32a9ab0c..4a57458e9 100644 --- a/packages/modules/dynamodb/src/dynamodb-container.ts +++ b/packages/modules/dynamodb/src/dynamodb-container.ts @@ -35,7 +35,6 @@ export class StartedDynamoDBContainer extends AbstractStartedContainer { } public getEndpoint(): string { - const url = `http://${this.getHost()}:${this.getPort()}`; - return url; + return `http://${this.getHost()}:${this.getPort()}`; } }