Run DynamoDB Locally using Docker
Amazon has the downloadable version of DynamoDB as a Docker image. https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/DynamoDBLocal.DownloadingAndRunning.html
In this post, I am going to follow the documentation to run the docker image and access the database with browser (javascript) and awscli.
run
First, prepare docker-compose.yml as follows:
version: '3.8'
services:
dynamodb-local:
command: "-jar DynamoDBLocal.jar -sharedDb -optimizeDbBeforeStartup -dbPath ./data"
image: "amazon/dynamodb-local:latest"
container_name: dynamodb-local
ports:
- "8000:8000"
volumes:
- "./docker/dynamodb:/home/dynamodblocal/data"
working_dir: /home/dynamodblocal
and run:
% docker-compose upjavascript
You can access http://localhost:8000/shell/ to open DynamoDB JavaScript Shell.
If you do not have a programming preference for DynamoDB yet, JavaScript may be a good choice for this reason.
awscli
You can use --endpoint-url option to use awscli to access the database.
https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Tools.CLI.html
% aws dynamodb list-tables --endpoint-url http://localhost:8000
{
"TableNames": []
}
% aws dynamodb create-table --table-name student \
--attribute-definitions AttributeName=id,AttributeType=S \
--key-schema AttributeName=id,KeyType=HASH \
--provisioned-throughput ReadCapacityUnits=5,WriteCapacityUnits=5 \
--endpoint-url http://localhost:8000
% aws dynamodb list-tables --endpoint-url http://localhost:8000
{
"TableNames": [
"student"
]
}data persistence
The data is stored in ./docker/dynamodb/shared-local-instance.db
% tree -a
.
├── docker
│ └── dynamodb
│ └── shared-local-instance.db
└── docker-compose.yml
Thus, it is not deleted when the container is restarted, unless the file is removed.

Comments
Post a Comment