AWS DynamoDB with Clojure

Image for post
Image for post
Running Clojure unit tests in IntelliJ IDEA / Cursive.


After my Five Languages project I was searching something new to learn. I decided to refresh my AWS skills a bit and and compare AWS container services EKS and Fargate and compare their deployment models for Docker containers. I decided to use my Clojure Simple Server exercise as a demo application. There was one problem, though. The Simple Server was just a demo application and it simulated database by reading csv files to internal data structures — therefore making the server stateful. First I needed to change the implementation to make the server fully stateless — storing all state outside the server in a real external database. I decided to use AWS DynamoDB nosql database. In this blog post I write about my experiences to manipulate DynamoDB using Clojure and how to use local DynamoDB Docker container instance as a development database. Next I’ll be using this DynamoDB version of Simple Server to deploy the server to EKS and Fargate and write new blog posts regarding those experiences, so stay tuned.

You can find the project in my Github account.

Local DynamoDB Docker Container Based Development Database

For a small exercise with minimal demo data set the DynamoDB price is almost neglectable, but there there is always the round-trip latency to AWS data center. Therefore I decided to use a local DynamoDB Docker container based development database — and have a nice excuse to learn to use it. The local DynamoDB was a positive surprise — all DynamoDB APIs I used (cli, Python/boto3, Clojure/amazonica) worked nicely out of the box. I found only one item that worked differently in local DynamoDB compared to real AWS DynamoDB service (more about that later).

You can start the local DynamoDB Docker container using command:

That’s it! You have a running DynamoDB in your workstation and you can use AWS command line interface (cli), or any DynamoDB API SDK to access this DynamoDB instance.

Let’s Create Tables

I created a sub-directory [dynamodb] for DynamoDB related utilities I used in this project. You can find in that directory script which creates the four tables needed in the project: session table, users table, product groups table and products table. It is pretty straightforward to use aws cli to create the tables, and the script works the same way with local DynamoDB Docker container instance and with real AWS DynamoDB service. Example of one aws cli call to create the product table:

The endpoint is used only for local DynamoDB Docker container instance and it points to the port you deployed the container earlier:

Let’s Import Data

The next task is to import the test data. I used this as an excuse to refresh my Python/boto3 skills. I could have used Clojure for this as well but since this is my personal project I can do it any way I like. I thought that it might be interesting to do the data import using Python.

First we need to create the virtual environment for python3, activate the virtual environment and then install boto3 to the virtual environment (well, you don’t have to use virtual environment but I personally almost always create a virtual environment to keep my workstation Python installation cleaner):

Then create the local-dynamodb profile and dummy AWS credentials in your ~/.aws/credentials file:

NOTE: When I first experimented with the local DynamoDB Docker container instance I just couldn’t make it work. Then I tried a real access key and secret key generated by AWS — and they magically worked. So, I guess the local DynamoDB Docker container instance expects the access key and secret key to be with valid length even though the content is irrelevant.

Then you are ready to import the data using the script:

The is pretty simple. First we get the dynamodb client:

And then we just dump everything from the csv file to DynamoDB:

That’s it! With two commands we can create the development tables and import the test data to the development tables.

Clojure Implementation

Programming Clojure was once again a real joy. Clojure REPL and especially the Cursive REPL is the best productivity tool I have ever used in any programming language. Once you have configured all your favorite hot keys e.g. for jumping between code editor and REPL editor using REPL to explore new APIs, try various ways to create functions etc makes programming an interesting interaction between the programmer and the code — and code evolves in an organic manner during this interaction.

I used amazonica library to manipulate DynamoDB. There is going to be another interesting choice with the next Clojure 1.10 version by Cognitect: aws-api. I definitely try to use it once Clojure 1.10 is rolled out. But at least this time I was still using amazonica.

I refactored the original stateful Clojure implementation under new profile “single-node” denoting that this profile runs the server in a stateful single node. Then I created two new profiles: “local-dynamodb” and “dynamodb-dev” for local DynamoDB Docker container instance and for real AWS DynamoDB development environment. Check the details in profiles.clj file.

Clojure provides nice polymorhism by multimethods and protocols. I used these mechanisms to abstract the database manipulation — i.e. the unit tests and the server layer has no idea whether the database queries are hitting the internal dummy database, local DynamoDB Docker container instance or the real AWS DynamoDB service. When running the unit tests you just select the right profile and the server uses the database configured for that profile (see the shell scripts I provided for all three environments).

Using amazonica is really simple. You just provide the credentials for that DynamoDB instance, table name and query parameters, example:

Once I had implemented all DynamoDB manipulations and had run the unit tests that everything is working properly using the local DynamoDB Docker container instance I tried the unit tests with real AWS DynamoDB profile. There was one discrepancy that I found out. I had created a secondary index for one table and local DynamoDB Docker container instance happily accepted :select “ALL_ATTRIBUTES” but real AWS DynamoDB service complained about this mistake. When changing the select clause to :select “ALL_PROJECTED_ATTRIBUTES” everything worked fine also with the real AWS DynamoDB service.


Using local DynamoDB Docker container instance makes DynamoDB development really fast. Clojure is really superb for data manipulation, and using Clojure with amazonica library it is pretty effortless to work with DynamoDB.

Written by

I’m a Software architect and developer. Currently implementing systems on AWS / GCP / Azure / Docker / Kubernetes using Java, Python, Go and Clojure.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store