How to setup DynamoDB locally

You don't need AWS credentials to try DynamoDB

Table of Contents
  1. Using Docker
    1. Using DynamoDB Local
  2. Creating a table

While writing code for DynamoDB, I started by creating a class to fake how DynamoDB could work, there were a lot of flaws in the logic, but it worked - it was mostly just putting dictionaries into a global dict.

This worked okay when all I had to do was put/get items, but when it came time to use other things, such as query with different filters, variable names and begins_with statements, it became hard to use the fake class.

Luckily, AWS created DynamoDB Local, which allows you to setup a local DynamoDB that works. You can use all the commands available in your SDK and test your code as if using your AWS account.

Using Docker

DynamoDB Local requires you to have the Java Runtime Environment. If you use Java and have JRE installed, you can follow the installation details in the DynamoDB Local documentation.

I didn't want to install the Java Runtime Environment on my computer, so Docker was the way to go. This example will use the most straightforward recipe to get DynamoDB Local installed.

Let's start by creating a docker-compose.yml file and adding all the required things:

1version: "3.8"
3 dynamodb:
4 image: "amazon/dynamodb-local:latest"
5 container_name: dynamodb
6 ports:
7 - "8000:8000"
8 volumes:
9 - "./docker/dynamodb:/home/dynamodblocal/data"
10 working_dir: /home/dynamodblocal
11 command: "-jar DynamoDBLocal.jar -sharedDb -dbPath ./data"
12 environment:
16 - AWS_DEFAULT_REGION=us-east-1

Note that we are passing some environment variables, they need to be set, but you don't need your real AWS credentials.

Now, all we need to do is run the docker-compose up to pull the image and run DynamoDB Local. DynamoDB will be available at http://localhost:8000.

Using DynamoDB Local

Now that we have the database running locally, we need to do one more thing - use it instead of the AWS service. When creating our client or resource, we need to pass endpoint_url="http://localhost:8000" In this article, I'll use the Python library aioboto3.

1import aioboto
3async def put_item(item: dict):
4 """Put item in the local dynamodb."""
5 session = aioboto.Session()
6 async with session.resource("dynamodb", "us-east-1", endpoint_url="http://localhost:8000") as dynamodb:
7 table = await dynamodb.Table("Books")
9 await table.put(Item=item)

If you run this command, you will see that it will fail because the table doesn't exist in your DynamoDB Local. You will have to create a new table before using your local database.

Creating a table

Let's copy the code from the example in the How to create and use indexes in DynamoDB article. We can create a new table with:

1async def create_table_and_index():
2 """Create DynamoDB table with index."""
3 session = aioboto3.Session()
4 async with session.resource("dynamodb") as dynamodb:
5 table = await dynamodb.create_table(
6 TableName="Books",
7 KeySchema=[
8 {"AttributeName": "BookTitle", "KeyType": "HASH"}, # Partition key
9 {"AttributeName": "Publisher", "KeyType": "RANGE"}, # Sort key
10 ],
11 AttributeDefinitions=[
12 #
13 # Table attributes.
14 #
15 {"AttributeName": "BookTitle", "AttributeType": "S"},
16 {"AttributeName": "Publisher", "AttributeType": "S"},
17 #
18 # Index attributes.
19 #
20 {"AttributeName": "Author", "AttributeType": "S"},
21 ],
22 GlobalSecondaryIndexes=[
23 {
24 "IndexName": "author-index",
25 "KeySchema": [
26 {"AttributeName": "Author", "KeyType": "HASH"},
27 {"AttributeName": "Publisher", "KeyType": "RANGE"},
28 ],
29 "Projection": {
30 "ProjectionType": "ALL",
31 },
32 }
33 ],
34 # There are required on table creation
35 ProvisionedThroughput={
36 "ReadCapacityUnits": 10,
37 "WriteCapacityUnits": 10,
38 },
39 )
41 await table.wait_until_exists()

Once you run the above command, if you have the AWS CLI installed locally, you can run the following command in your terminal to confirm that the table was indeed created:

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

You should see our Books table. Now you can run the put_item to add a new item to the database.


0 Like 0 Comment

You might also like these

This article will show you how to create an use indexes in DynamoDB using the python library aioboto3.

Read More

How to create and use indexes in DynamoDB

How to create and use indexes in DynamoDB

Using the command django makemigrations failed, so I had to figure out how to use psql on a docker container and add those columns manually.

Read More

Add column to postgres table

Add column to postgres table

Exploration on how to run Pyscript in a React (NextJS) app, this article explores issues and solutions to run PyScript in a React app.

Read More

How to run PyScript in React

How to run PyScript in React

While working on adding tests to Pyscript I came across a use case where I had to check if an example image is always generated the same.

Read More

How to compare two images using NumPy

How to compare two images using NumPy