Dung (Donny) Nguyen

Senior Software Engineer

Creating a DynamoDB Table with Sort Key

You can create a DynamoDB table with a partition key + sort key using JavaScript and the AWS SDK for JavaScript v3. Since you’re already deep in backend and AWS work, this will feel very natural.

Below is a clean, production‑ready example.


Creating a DynamoDB Table with Sort Key (AWS SDK v3)

1. Install the SDK

npm install @aws-sdk/client-dynamodb

2. Create the table

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

const client = new DynamoDBClient({ region: "us-east-1" });

async function createTable() {
  const command = new CreateTableCommand({
    TableName: "Users",
    AttributeDefinitions: [
      { AttributeName: "userId", AttributeType: "S" },   // partition key
      { AttributeName: "createdAt", AttributeType: "S" } // sort key
    ],
    KeySchema: [
      { AttributeName: "userId", KeyType: "HASH" },      // partition key
      { AttributeName: "createdAt", KeyType: "RANGE" }   // sort key
    ],
    BillingMode: "PAY_PER_REQUEST"
  });

  try {
    const result = await client.send(command);
    console.log("Table created:", result);
  } catch (err) {
    console.error("Error creating table:", err);
  }
}

createTable();

What’s happening here

AttributeDefinitions

Defines the data types:

KeySchema

BillingMode

PAY_PER_REQUEST is great for dev or unpredictable workloads.


Quick mental model for sort keys

Use a sort key when you want:

Example item:

{
  "userId": "123",
  "createdAt": "2025-01-01T10:00:00Z"
}