Skip to main content
The Dynamo component lets you add an Amazon DynamoDB table to your app.

Constructor

sst.config.ts
const table = new sst.aws.Dynamo("MyTable", {
  fields: {
    userId: "string",
    noteId: "string"
  },
  primaryIndex: { hashKey: "userId", rangeKey: "noteId" }
});

Parameters

fields

fields
Record<string, 'string' | 'number' | 'binary'>
required
Define the fields used for indexes. You only need to define fields used in indexes, not all table fields.
{
  fields: {
    userId: "string",
    noteId: "string",
    createdAt: "number"
  }
}
Field types cannot be changed after table creation.

primaryIndex

primaryIndex
object
required
Define the table’s primary index.
{
  primaryIndex: { hashKey: "userId", rangeKey: "noteId" }
}

globalIndexes

globalIndexes
Record<string, object>
Configure global secondary indexes. You can have up to 20 global indexes per table.
{
  globalIndexes: {
    CreatedAtIndex: { hashKey: "userId", rangeKey: "createdAt" }
  }
}
Create composite keys with multiple attributes:
{
  globalIndexes: {
    RegionCategoryIndex: {
      hashKey: ["region", "category"],
      rangeKey: "createdAt"
    }
  }
}

localIndexes

localIndexes
Record<string, object>
Configure local secondary indexes. You can have up to 5 local indexes per table.
{
  localIndexes: {
    CreatedAtIndex: { rangeKey: "createdAt" }
  }
}

stream

stream
"keys-only" | "new-image" | "old-image" | "new-and-old-images"
Enable DynamoDB Streams to capture item changes.
{
  stream: "new-and-old-images"
}
Use "new-and-old-images" as a good default since it contains all the data.
Streams are not enabled by default due to associated costs.

ttl

ttl
string
The field name to store Time to Live (TTL) timestamps. Items are deleted when TTL is reached.
{
  ttl: "expiresAt"
}

Properties

name

arn

nodes

Methods

subscribe

SDK

Query the table from your function code:
src/api.ts
import { Resource } from "sst";
import { DynamoDBClient, QueryCommand } from "@aws-sdk/client-dynamodb";

const client = new DynamoDBClient();

await client.send(new QueryCommand({
  TableName: Resource.MyTable.name,
  KeyConditionExpression: "userId = :userId",
  ExpressionAttributeValues: {
    ":userId": { S: "my-user-id" }
  }
}));

Examples

Add a global index

sst.config.ts
new sst.aws.Dynamo("MyTable", {
  fields: {
    userId: "string",
    noteId: "string",
    createdAt: "number"
  },
  primaryIndex: { hashKey: "userId", rangeKey: "noteId" },
  globalIndexes: {
    CreatedAtIndex: { hashKey: "userId", rangeKey: "createdAt" }
  }
});

Subscribe to streams

sst.config.ts
const table = new sst.aws.Dynamo("MyTable", {
  fields: {
    userId: "string",
    noteId: "string"
  },
  primaryIndex: { hashKey: "userId", rangeKey: "noteId" },
  stream: "new-and-old-images"
});

table.subscribe("MySubscriber", "src/subscriber.handler");
sst.config.ts
const table = new sst.aws.Dynamo("MyTable", {
  fields: {
    userId: "string",
    noteId: "string"
  },
  primaryIndex: { hashKey: "userId", rangeKey: "noteId" }
});

new sst.aws.Function("MyFunction", {
  handler: "src/api.handler",
  link: [table]
});

Build docs developers (and LLMs) love