Starting with AWS DynamoDB using Python
Tags: python, softwareengineering, aws, dynamodb
Published: 2020-03-10 13:40:47

---
title: Starting with AWS DynamoDB using Python
published: true
date: 2020-03-10 10:08:07 UTC
tags: python,software-engineering,aws,dynamodb
canonical_url: https://revs.runtime-revolution.com/starting-with-aws-dynamodb-using-python-b4f294f912a
---

![](https://cdn-images-1.medium.com/max/680/1*qKw_ICEQ8Aw202wAEroxhw.png)

> Amazon **DynamoDB** is a _key-value and document database that delivers single-digit millisecond performance at any scale._ [https://aws.amazon.com/dynamodb/](https://aws.amazon.com/dynamodb/)

This guide is meant to provide a base, or a quick start, for interfacing with AWS DynamoDB using Python, its contents are based on the repo [https://github.com/5thempire/aws-dynamodb](https://github.com/5thempire/aws-dynamodb).

The chosen examples are meant to illustrate the breadth of options available, even though they’re not the typical use case.

We are going to create two tables: one for storing URLs and their corresponding domain, and another for storing logs, which have a timestamp, a log status and a message. The URLs data model will be using a key-value configuration, while the logs data model will use a document configuration.

First things first, let’s define the data models.

### Key-Value — URL data model

The key-value configuration is a very simple implementation, where the only requirement is to specify the key or index. In DynamoDB this is set under the attribute definition and the key schema.

The attribute definition stores the name of the keys and their corresponding type.

The key schema defines the type of primary keys we’re using, in DynamoDB they can be one of two types, hash or range. We could have one of each, but for the current example we’ll only be using hash, leaving out range for you to play with.

{% gist https://gist.github.com/kenvontucky/f0849e000da3c9e3f887edeb613ff0cc.js %}

### Document — Log data model

The document configuration is a bit more complex. To query the log documents we’ll use two indexes, one will be the timestamp and the other the status of the log, the latter of which is set as a global secondary index.

The global secondary index also includes a projection attribute. This configuration is very important, since it defines what will be retrieved when we query using the secondary index. In our definition we’re retrieving the complete document.

{% gist https://gist.github.com/kenvontucky/a5c58f97bba1ddd99ce39110a15f9b47 %}

### Python DynamoDB base class

This approach has one base class to interact with DynamoDB, which is not meant to be used on its own, but to provide a solid base for the table specific definitions. These specificities are set by overriding the abstract methods for get, put, update and remove.

{% gist https://gist.github.com/kenvontucky/f81502fab713bb5e744af12753acd906 %}

### Python URL class

This class overrides the base class by defining what’s specific to the domain data model interaction.

{% gist https://gist.github.com/kenvontucky/1100684a10ed344b91f1ccf00d41a1f8 %}

### Python Log class

This class overrides the base class by defining what’s specific to the log data model interaction, but also adds filtering methods. There are two filters defined:

- **filter\_by\_timestamp\_status** : which uses both timestamp and status keys to filter results
- **filter\_by\_status** : which filters by a specific status (i.e.: INFO)

{% gist https://gist.github.com/kenvontucky/f19584b9d4276ca5f4afa5ae6ef57d2f %}

### Wrapping up

Don’t forget there are a few options out there that provide the typical level of abstraction a database model normally requires. However, sometimes a more hands on approach is preferred.

For those who want to experience a running example visit [https://github.com/5thempire/aws-dynamodb](https://github.com/5thempire/aws-dynamodb). There you’ll find the samples mentioned here along with the corresponding set of tests, that can be used as a basis for further experiments.

### References

[https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Introduction.html](https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Introduction.html)

[https://amazon-dynamodb-labs.com/](https://amazon-dynamodb-labs.com/)

_At_ [_Runtime Revolution_](http://www.runtime-revolution.com/) _we take our craft seriously and always go the extra mile to deliver a reliable, maintainable, and testable product. Do you have a project to move forward or a product you’d like to launch? We would love to help you!_

* * *