Automate DynamoDB Local Table Creation with Docker Compose (2026)

Discover how to automate the creation of DynamoDB Local tables using Docker Compose for efficient local development and testing.

Automate DynamoDB Local Table Creation with Docker Compose (2026)

In this tutorial, we will explore how to automatically create a DynamoDB Local table when running docker-compose up. This is particularly useful for local development and testing, ensuring your environment is correctly set up every time you spin up your services.

Key Takeaways

  • Learn to set up DynamoDB Local with Docker Compose for local testing.
  • Automatically create DynamoDB tables upon startup.
  • Understand the integration with a Java REST API and PostgreSQL database.
  • Get tips on troubleshooting common setup issues.

Introduction

In modern development environments, using containers for local development is a common practice. Docker Compose allows you to define and run multi-container Docker applications, simplifying the setup of complex environments. When working on a project like a chatbot feature for a water billing office, it's beneficial to automate as much as possible, including the setup of a local DynamoDB instance.

This guide will show you how to modify your docker-compose.yml to automatically create a DynamoDB table every time you bring up your containers. This ensures consistency and saves time, especially when testing interactions that your chatbot records in DynamoDB.

Prerequisites

  • Basic understanding of Docker and Docker Compose.
  • Familiarity with AWS DynamoDB concepts.
  • A working Docker installation on your machine.
  • Knowledge of YAML and JSON for configuring Docker Compose files.

Step 1: Install Docker and Docker Compose

If you haven't already, ensure that Docker and Docker Compose are installed on your system. You can download Docker from the official Docker website. Docker Compose is usually included with Docker Desktop, but you can also install it separately if needed.

Step 2: Set Up DynamoDB Local in Docker Compose

First, define DynamoDB Local in your docker-compose.yml file. This allows you to run a local instance of DynamoDB for testing purposes.

version: '3.9'
services:
  dynamodb:
    image: amazon/dynamodb-local
    command: -jar DynamoDBLocal.jar -inMemory -sharedDb
    ports:
      - "8000:8000"

This configuration runs DynamoDB Local in memory, which is ideal for development as it requires no persistent storage.

Step 3: Automate Table Creation with a Script

To automatically create a table on startup, we can use a simple script. This script will use the AWS CLI to create the table once the DynamoDB Local instance is running.

#!/bin/bash

aws dynamodb create-table \
    --table-name ChatbotInteractions \
    --attribute-definitions AttributeName=Prompt,AttributeType=S \
    --key-schema AttributeName=Prompt,KeyType=HASH \
    --provisioned-throughput ReadCapacityUnits=5,WriteCapacityUnits=5 \
    --endpoint-url http://localhost:8000

Save this script as create-table.sh and ensure it is executable.

Step 4: Integrate the Script with Docker Compose

To run the script automatically, we can use a combination of Docker Compose and a wait-for-it script to ensure DynamoDB Local is ready before executing our table creation script.

version: '3.9'
services:
  dynamodb:
    image: amazon/dynamodb-local
    command: -jar DynamoDBLocal.jar -inMemory -sharedDb
    ports:
      - "8000:8000"
    volumes:
      - ./create-table.sh:/docker-entrypoint-initdb.d/create-table.sh
    entrypoint: ["/bin/sh", "-c", "./docker-entrypoint-initdb.d/create-table.sh"]

This configuration will mount and execute the script whenever the service starts, automating the table setup process.

Step 5: Verify Your Setup

Bring up your services with docker-compose up and check if the table is created successfully by accessing your DynamoDB Local instance.

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

This command should list your newly created table, confirming that the automation works as expected.

Common Errors/Troubleshooting

Here are some common issues you might encounter:

  • Script Permissions: Ensure create-table.sh is executable (chmod +x create-table.sh).
  • Network Issues: Verify your Docker network settings if you cannot connect to DynamoDB Local.
  • AWS CLI Version: Ensure you have the latest AWS CLI installed, which supports the required commands.

Conclusion

By following these steps, you can automate the creation of DynamoDB tables in your local development environment using Docker Compose. This setup is particularly advantageous for testing and developing applications like chatbots, allowing you to focus on feature development rather than setup hassles.

Frequently Asked Questions

Why use DynamoDB Local for development?

DynamoDB Local provides a quick and cost-effective way to develop and test applications without incurring AWS costs.

Can I persist data in DynamoDB Local?

Yes, by removing the -inMemory option, you can persist data to a local directory.

How do I access DynamoDB Local from my application?

Use the endpoint URL, typically http://localhost:8000, to connect to DynamoDB Local within your application.