Skip to content
GitHub Discord

DynamoDB streams

Create a DynamoDB table, enable streams, and subscribe to it with a function.

sst.config.ts
const table = new sst.aws.Dynamo("MyTable", {
fields: {
id: "string",
},
primaryIndex: { hashKey: "id" },
stream: "new-and-old-images",
});
table.subscribe("MySubscriber", "subscriber.handler", {
filters: [
{
dynamodb: {
NewImage: {
message: {
S: ["Hello"],
},
},
},
},
],
});
const creator = new sst.aws.Function("MyCreator", {
handler: "creator.handler",
link: [table],
url: true,
});
const reader = new sst.aws.Function("MyReader", {
handler: "reader.handler",
link: [table],
url: true,
});
subscriber.ts
export const handler = async (event) => {
console.log(JSON.stringify(event, null, 2));
return "ok";
};
creator.ts
import { Resource } from "sst";
import { DynamoDBClient, PutItemCommand } from "@aws-sdk/client-dynamodb";
const client = new DynamoDBClient();
export const handler = async (event) => {
await client.send(
new PutItemCommand({
TableName: Resource.MyTable.name,
Item: {
id: { S: Date.now().toString() },
message: { S: "Hello" },
},
})
);
return {
statusCode: 200,
body: JSON.stringify({ status: "sent" }, null, 2),
};
};
reader.ts
import { Resource } from "sst";
import { DynamoDBClient, ScanCommand } from "@aws-sdk/client-dynamodb";
const client = new DynamoDBClient();
export const handler = async () => {
const result = await client.send(
new ScanCommand({
TableName: Resource.MyTable.name,
})
);
return {
statusCode: 200,
body: JSON.stringify(result.Items, null, 2),
};
};

View the full example.