DynamoDB on Localhost

Ewere Diagboya
FAUN — Developer Community 🐾
4 min readNov 16, 2018

--

DynamoDB + Docker (Image Credit: Ewere Diagboya)

AWS released DynamoDB in 2012. DynamoDB is a managed document store database with a 99.999% uptime. This makes it one of the highest available services in AWS cloud services. But the challenge has been that, developers do not have a proper test environment where they can properly test the DynamoDB applications. Up on till this moment, that AWS has released DynamoDB on localhost.

Blockers

Because everything has to be done on the console, CLI or API, it makes it difficult to perform CI/CD operations and DevOps processes, because there is not test bed. Developers also need some type of local replica to do different types of test along the way of developing the application.

Security is difficult because every single user needs to be given access to the ‘staging/production’ DynamoDB on the AWS console to perform their tests, which can really lead to disastrous configurations and data in the environment.

Collaboration is actually a mess, because everyone needs to work with a single table which can lead to a lot of clashes which is something Vagrant and Packer helps to solve in giving the developer a development environment.

These are just a few of the frustrations in using DynamoDB even with all the power and resilience AWS promises to give.

A New Dawn

AWS finally launched DynamoDB in docker. This makes it easy to setup DynamoDB locally and makes thing flexible and easier for developers to have DynamoDB locally and run all types of tests that is needed.

It also solves the challenge of collaboration, meaning developers can run their own DynamoDB and work with it and push before it goes into production

Devops practice is quite easier; a local staging environment can be setup now with same Docker to test the applications built by the developers and deployed to a general staging server to server as database server, when all test pass, it is promoted to go into production environment AWS.

In the next section I will explain how to setup a local DynamoDB application and communicate with it using a simple python code

Practical

We are going to setup the DynamoDB locally on Docker. There are some pre-requisites that are needed for this to work:

  • AWS CLI
  • Python (2.7/3.6)
  • Docker

First step is to install the AWS CLI which can be done with any of these commands:

Debian

$ apt install awscli

Python/Other Distros/Windows

$ pip install awscli

Second step is to configure the AWS CLI. No real live AWS credentials are needed for this (AccesskeyID, SecretKey), but the region is important. AWS specific region should be used for DynamoDB to work.

AWS CLI configuration

For our example, we used:

- ‘abcd’ as the Access KeyID,
- ‘efgh’ as the Secret Access Key
- ‘eu-west-1’ for the Default Region (more region at https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/Concepts.RegionsAndAvailabilityZones.html)
- ‘json’ for the Default output format

Third step is to install Docker. For this any version of Docker can work, and installation of Docker can be distro/OS inclined

Debian

$ apt install docker.io

RedHat

$ yum install docker

Fourth Step is to setup the DynamoDB service, which involves pulling and running the docker image, which is available in Dockerhub: https://hub.docker.com/r/amazon/dynamodb-local/

$ docker run -p 8000:8000 amazon/dynamodb-local
Running DynamoDB Locally in Docker

Sorry this might hold your terminal, but it will be better to do this

$ docker run -d -p 8000:8000 amazon/dynamodb-local

And this should run without holding your terminal

At this point, the DynamoDB service is running at port 8000. This is just a default port, but can be any port you wish to use to run the DynamoDB service.

Fifth Step is writing a simple python script (can be any language that supports AWS SDK) that connects to the DynamoDB service.

Simple Python Code to List Tables

When you run the code above it will give this output:

This completes the local setup of the DynamoDB

All other operations in the DynamoDB documentation for CRUD can be done.

BSOT

There are two major business values having DynamoDB brings to the table:

  1. Improvement of time with which teams can build and deliver applications to the business without much complications
  2. Reduction of cost of using an AWS account to test DynamoDB in the AWS console.

Thanks

Join our community Slack and read our weekly Faun topics ⬇

If this post was helpful, please click the clap 👏 button below a few times to show your support for the author! ⬇

--

--