Creating Tables with DynamoDB Toolbox

I'm playing around with DynamoDB Toolbox, and one thing that wasn't clear in my mind was how do I actually create a table using the definitions I made using Toolbox.

Quick github issues search lead me to this issue:

How do we create the actual table in aws dynamodb? · Issue #101 · jeremydaly/dynamodb-toolbox
Preference is to not have two definitions for the same table: So how can we either create a table through the api or/and generate a cloudformation template? If this is out of the scope of this libr...

where Simlu was asking the same question, and the answer was "Do It Yourself".

I dread code duplication especially around things like table definitions - it's trivial to have all your tests pass, but in the end app fail in production because you have a mismatch between two definitions of the same thing.

I've made a little example repo and a helper. For now feel free to just copy and paste it.

Helper like this might get into the dynamodb-toolbox itself, so I didn't create a package for it yet. But if jeremydaly decides this code should live in a package, I will do that.

import type { DynamoDB } from "aws-sdk";

type DynamoDBTypes = "string" | "number" | "binary";

type ToolboxData = {
  partitionKey: string;
  sortKey?: string;
  attributes: {
    [key: string]: DynamoDBTypes;
  name: string;

function getAttributes(tableDefinition: DynamoDB.CreateTableInput) {
  const typesMap: { [key: string]: DynamoDBTypes } = {
    S: "string",
    N: "number",
    B: "binary",

  return tableDefinition.AttributeDefinitions.reduce((previous, current) => {
    return {
      [current.AttributeName]: typesMap[current.AttributeType],
  }, {});

function getPartitionKey(tableDefinition: DynamoDB.CreateTableInput) {
  return tableDefinition.KeySchema.find((k) => {
    return k.KeyType.toUpperCase() === "HASH";

function getSortKey(tableDefinition: DynamoDB.CreateTableInput) {
  const rangeAttribute = tableDefinition.KeySchema.find((k) => {
    return k.KeyType.toUpperCase() === "RANGE";
  return rangeAttribute ? rangeAttribute.AttributeName : undefined;

export const dynamoSdkToToolbox = (
  tableDefinition: DynamoDB.CreateTableInput
): ToolboxData => ({
  partitionKey: getPartitionKey(tableDefinition),
  attributes: getAttributes(tableDefinition),
  sortKey: getSortKey(tableDefinition),
  name: tableDefinition.TableName,

(there are tests for it in the example repo)

Contribute to xolvio/dynamodb-toolbox-examples development by creating an account on GitHub.

This is how to use it. Assuming you have a table definition using AWS SDK format:

import type { DynamoDB } from "aws-sdk";

export const tableDefinition: DynamoDB.CreateTableInput = {
  TableName: "my-table",
  AttributeDefinitions: [
      AttributeType: "S",
      AttributeName: "pk",
      AttributeType: "S",
      AttributeName: "sk",
  KeySchema: [
      AttributeName: "pk",
      KeyType: "HASH",
      AttributeName: "sk",
      KeyType: "RANGE",
  BillingMode: "PAY_PER_REQUEST"

pass it to the dynamoSdkToToolbox helper:

const DocumentClient = new DynamoDB.DocumentClient()

const MyTable = new Table({

This will set things like attributes, partitionKey, sortKey and name for you - making sure they are and stay in sync.

Using the same table definition you can create a table (although you would probably use cloud formation or CDK for that, that's a topic for a separate article, but you might want to use a helper like this to transform the SDK definition to CDK )

await dynamoDB.createTable(tableDefinition).promise()

Feel free to play around with the code:

Contribute to xolvio/dynamodb-toolbox-examples development by creating an account on GitHub.

Let us help you on your journey to Quality Faster

We at specialize in helping our clients get more for less. We can get you to the holy grail of continuous deployment where every commit can go to production — and yes, even for large enterprises.

Here are the things we offer:

Services (we help you)

  • Custom Software Development
    Whether you're looking to create a proof-of-concept or build an entire new capability, we can help you get there at speed and with high quality.
  • Apollo GraphQL Consulting
    We can help in all areas related to Apollo GraphQL and surrounding technologies including Apollo Federation, One-graph unification, Domain Driven Design, µService deployment, automated testing, security, performance, and more.
  • Continuous Delivery Coaching
    We have made it our business to help organizations modernize their technologies stacks and processes to gain speed, stability, and quality improvements, especially in enterprises where legacy systems seem impossible to deal with.

Products (you help yourself)

  • XSpecs
    XSpecs is a full-team collaborative toolset that simplifies the writing of specifications and integrates them into your team's existing workflow. This helps you improves velocity, decreases bugs, and increases quality.
  • Quality Faster
    A complete full-team, full-cycle and full-stack guide to help you and your team increase the speed at which you deliver software while simultaneously increasing quality.

Have something else in mind? Get in touch.

  • Testing with DynamoDB Toolbox

  • Instantly update your Lambda functions (development)

  • Centralize your AWS Lambda Errors