How to deploy DynamoDB locally?

Set up a No-SQL database playground for testing purposes

3 min read

What is a No-SQL database?

A "No-SQL" or "non-relational" database (the term is used since mid to late 2000s) is mainly about being less strict with how the data is stored - semi-structured or even unstuctured.

Just remember that "SQL" is the abbreviation for "Structured Query Language", and so "No-SQL" means "No Structured Query Language". 😎

The ability to store and change data structures flexibly allows businesses to move and scale quickly, adapting to market needs.

Accessing data is also faster compared to a "relational" database, due to accessing data without having to join multiple tables.

However, relational databases also have their advantages, but more to this in another blog post! 🤪

What is DynamoDB?

DynamoDB is a database service offered by Amazon. It is

a fully managed, serverless, key-value No-SQL database designed to run high-performance applications at any scale. DynamoDB offers built-in security, continuous backups, automated multi-Region replication, in-memory caching, and data import and export tools

Source

Alrighty - let's get going

  1. Create a new project and install:
// create project

$ mkdir <dynamodb-local>
$ cd <dynamodb-local>
$ npm init -y

// install package

$ npm i @aws-sdk/client-dynamodb
  1. Create tsconfig.json file:
$ touch tsconfig.json
  1. Copy the following into the tsconfig.json file:
{
  "compilerOptions": {
    "esModuleInterop": true,
    "lib": ["es2015", "dom"]
  }
}
  1. Create Docker file:
$ touch docker-compose.yml
  1. Copy the following into the Docker file:
// docker-compose.yml

version: '3.8'
services:
 dynamodb-local:
   command: "-jar DynamoDBLocal.jar -sharedDb -dbPath ./data"
   image: "amazon/dynamodb-local:latest"
   container_name: dynamodb-local
   ports:
     - "8000:8000"
   volumes:
     - "./docker/dynamodb:/home/dynamodblocal/data"
   working_dir: /home/dynamodblocal
  1. Create database creation file:
$ touch createTable.ts
  1. Copy the following into the table creation file:
// createTable.ts

import {
  DynamoDBClient,
  CreateTableCommand,
  CreateTableCommandInput,
} from '@aws-sdk/client-dynamodb';

const dbclient = new DynamoDBClient({
  region: 'eu-central-1',
  credentials: {
    accessKeyId: 'MYACCESSKEY',
    secretAccessKey: 'MYSECRETKEY',
  },
  endpoint: 'http://localhost:8000',
});

const command: CreateTableCommandInput = {
  TableName: 'MyTable',
  KeySchema: [{ AttributeName: 'id', KeyType: 'HASH' }],
  AttributeDefinitions: [{ AttributeName: 'id', AttributeType: 'S' }],
  BillingMode: 'PAY_PER_REQUEST',
};

const run = async function () {
  try {
    const results = await dbclient.send(new CreateTableCommand(command));
    console.log(results);
  } catch (err) {
    console.error(JSON.stringify(err));
  }
};

run();
  1. Run Docker:
$ docker compose up
  1. Execute database table creation file:
$ npx ts-node createTable.ts
  1. Check database table is created:
$ AWS_DEFAULT_REGION=eu-central-1 \
AWS_ACCESS_KEY_ID=MYACCESSKEY \
AWS_SECRET_ACCESS_KEY=MYSECRETKEY \
aws dynamodb list-tables --endpoint-url http://localhost:8000

// response
{
    "TableNames": [
        "MyTable"
    ]
}

Source

Last Words

The rest you can find out yourself! 😎 Make sure to rest well before you continue! Adiós 👋