DynamoDB
Contents
AWS DynamoDB local
to run AWS DynamoDb on your own machine, you can use official Docker container of AWS.
https://hub.docker.com/r/amazon/dynamodb-local/
Docker pull command :
docker pull amazon/dynamodb-local
DynamoDB local is a downloadable version of DynamoDB that enables developers to develop and test applications using a version of DynamoDB running in your own development environment.
Benefits of using DynamoDB local
The new DynamoDB local Docker image enables you to get started with DynamoDB local quickly by using a docker image with all the DynamoDB local dependencies and necessary configuration built in. The new Docker image also enables you to include DynamoDB local in your containerized builds and as part of your continuous integration testing.
Using DynamoDB local does not require an internet connection and DynamoDB local works with your existing DynamoDB API calls. There are no provisioned throughput, data storage, or data transfer costs with DynamoDB local.
Getting Started with DynamoDB on Docker
Simply run docker run -p 8000:8000 amazon/dynamodb-local to execute DynamoDB locally.
You can find a sample application on GitHub demonstrating how to use DynamoDB local for testing.
To learn how to configure DynamoDB local, see the DynamoDB local usage notes in the AWS Docs.
Examples
$ lsof -i :8000 | grep LISTEN
$ docker run -p 8000:8000 amazon/dynamodb-local
$ aws dynamodb list-tables --endpoint-url http://localhost:8000
$ aws dynamodb create-table --table-name orders_table --attribute-definitions AttributeName=orderId,AttributeType=S --key-schema AttributeName=orderId,KeyType=HASH --billing-mode PAY_PER_REQUEST --endpoint-url http://localhost:8000
$ docker ps --filter ancestor=amazon/dynamodb-local
$ docker stop $(docker ps -a -q --filter ancestor=amazon/dynamodb-local)
to read, list from dynamodb local
$ aws dynamodb describe-table --table-name xx_table_xx --endpoint-url http://localhost:8000
$ aws dynamodb scan --table-name xx_table_xx --endpoint-url http://localhost:8000
to list tables :
$ aws dynamodb list-tables --endpoint-url http://localhost:8000
DynamoDB Notes
Table definition
The following describe-table example describes the MusicCollection table.
$ aws dynamodb describe-table \
--table-name MusicCollection
aws ref : https://docs.aws.amazon.com/cli/latest/reference/dynamodb/describe-table.html
Data import and Export
Below command will export all items as jsons documents
$ aws dynamodb scan --table-name TABLE_NAME > export.json
To export the data run the following:
$ aws dynamodb scan --table-name my-prod-table \
| jq '{"my-local-table": [.Items[] | {PutRequest: {Item: .}}]}' > data.json
Basically what happens is that we scan our prod table, transform the output of the scan to shape into the format of the batchWriteItem and dump the result into a file.
To import the data in your local table run:
aws dynamodb batch-write-item \
--request-items file://data.json \
--endpoint-url http://localhost:8000
Note: There are some restriction with the batch-write-item request - The BatchWriteItem operation can contain up to 25 individual PutItem and DeleteItem requests and can write up to 16 MB of data. (The maximum size of an individual item is 400 KB.).