Running DynamoDB Locally

I've been using traditional relational databases for far too many years, but as I've been exploring the Amazon Web Services (AWS), I came across their NoSQL offering DynamoDB.

I had a flight coming up and wanted to explore DynamoDB, but AWS expects you to run their services in the cloud, which needs an internet connection. Luckily AWS provide a means of trying it out locally.

Heading to their download page, you can download either a tar or zip package - it needs a JRE of version 6 or later. Extract the file and use the command:

java -Djava.library.path=./DynamoDBLocal_lib -jar DynamoDBLocal.jar -sharedDb

in the folder containing the .jar file. This command will start a local version of the database, contactable via port 8000 @ localhost.

Managing DynamoDB the Linux way

However, that's a custom command to remember, and Linux gives us a standard way of starting and stopping services, so I spent a bit of time putting together a systemd service to run the Java code.

The service assumes you've installed the Java package in /opt/deploy, so if you install somewhere else, change any references to it below.

For a new service, we need to create a systemd configuration file - in this case, the file should be /lib/systemd/system/dynamodb-server.service which should be owned by root (look at the other files in /lib/systemd/system for exact permissions). The dynamodb-server.service file should contain the following code:

 1[Unit]
 2 Description=Local installation of DynamoDB
 3 After=network.target
 4 Documentation=http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/DynamoDBLocal.html
 5
 6[Service]
 7 Environment=deploydir=/opt/dynamodb
 8 Environment=dbPath=${deploydir}/data
 9 Type=simple
10 User=dynamodb
11 Group=dynamodb
12 ExecStart=/usr/bin/java -Djava.library.path=${deploydir} -jar ${deploydir}/DynamoDBLocal.jar -sharedDb -dbPath ${deploydir}/data
13
14
15 [Install]locally
16 WantedBy=multi-user.target
Once that file is in place, you'll need to reload the SystemD files with the command sudo systemctl daemon-reload. We can then stop and start the service using the commands below:

  • sudo systemctl start dynamodb-server
  • sudo systemctl start dynamodb-server

With the service running and the AWS CLI installed, we can test it out by running a command such as

aws dynamodb -endpoint-url=http://localhost:8000 list-tables

Want to save time?

I've put together a script that will download the Java package, install it along with the SystemD configuration.

If you want to look at it in detail, head over to https://github.com/simonhanmer/local_dynamodb, but for the short version, run the following commands

  • wget https://github.com/simonhanmer/local_dynamodb/raw/master/dynamodb_setup.sh
  • sudo bash ./dynamodb_setup.sh

The usual warning about running code with sudo apply, but the code basically

  • pulls down the AWS DynamoDB package
  • extracts it to /opt/dynamodb, creating /opt/dynamodb/data to hold the datafiles
  • creates a systemd service, dynamodb-server to manage running DynamoDB
comments powered by Disqus