Run and test DynamoDB applications locally using Docker and Testcontainers
Use the DynamoDB Local Testcontainers module for your Go applications!
1
2
3
4
5
6
7
8
start container
docker run --rm -p 8000:8000 amazon/dynamodb-local
connect and create a table
aws dynamodb create-table --endpoint-url http://localhost:8000 --table-name Books --attribute-definitions AttributeName=ISBN,AttributeType=S --key-schema AttributeName=ISBN,KeyType=HASH --billing-mode PAY_PER_REQUEST
list tables
aws dynamodb list-tables --endpoint-url http://localhost:8000
--endpoint-url
soon
1
2
go mod init demo
go get github.com/abhirockzz/dynamodb-local-testcontainers-go
- Start the DynamoDB Local Docker container -
dynamodblocal.RunContainer(ctx)
- Gets the client handle for the DynamoDB (local) instance -
dynamodbLocalContainer.GetDynamoDBClient(context.Background())
- Uses the client handle to execute operations. In this case - create a table, add an item, query that item.
- Terminate it at the end of the program (typically register it using
defer
) -dynamodbLocalContainer.Terminate(ctx)
WithTelemetryDisabled
- When specified, DynamoDB local will not send any telemetry.WithSharedDB
- If you use this option, DynamoDB creates a shared database file in which data is stored. This is useful if you want to persist data for e.g. between successive test executions.
WithSharedDB
, here is a common workflow:- Start container and get client handle
- Create table, add data and query it
- Re-start container
- Query same data (again) - Should be there
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
func withSharedDB() {
ctx := context.Background()
//start container
dynamodbLocalContainer, _ := dynamodblocal.RunContainer(ctx)
defer dynamodbLocalContainer.Terminate(ctx)
//get client
client, _ := dynamodbLocalContainer.GetDynamoDBClient(context.Background())
//create table, add data
createTable(client)
value := "test_value"
addDataToTable(client, value)
//query same data
queryResult, _ := queryItem(client, value)
log.Println("queried data from dynamodb table. result -", queryResult)
//re-start container
dynamodbLocalContainer.Stop(context.Background(), aws.Duration(5*time.Second))
dynamodbLocalContainer.Start(context.Background())
//query same data
client, _ = dynamodbLocalContainer.GetDynamoDBClient(context.Background())
queryResult, _ = queryItem(client, value)
log.Println("queried data from dynamodb table. result -", queryResult)
}
1
container, err := dynamodblocal.RunContainer(ctx, WithSharedDB(), WithTelemetryDisabled())
http://<your_host>:<service_port>
. Most commonly, this is what you would use: http://locahost:8000
1
2
3
4
5
6
7
8
9
10
type DynamoDBLocalResolver struct {
hostAndPort string
}
func (r *DynamoDBLocalResolver) ResolveEndpoint(ctx context.Context, params dynamodb.EndpointParameters) (endpoint smithyendpoints.Endpoint, err error) {
return smithyendpoints.Endpoint{
URI: url.URL{Host: r.hostAndPort, Scheme: "http"},
}, nil
}
Any opinions in this post are those of the individual author and may not reflect the opinions of AWS.