You are viewing a free preview of this lesson.
Subscribe to unlock all 10 lessons in this course and every other course on LearningBro.
DynamoDB Streams captures a time-ordered sequence of item-level changes in a table. Combined with AWS Lambda, Streams enables powerful event-driven architectures, real-time processing, and change data capture (CDC) patterns.
When you enable Streams on a table, DynamoDB records every item-level change:
Application → DynamoDB Table → DynamoDB Stream → Consumer
│
├── Lambda Function
├── Kinesis Data Streams
└── Custom Application
When enabling Streams, you choose what data to include in the stream record:
| View Type | Description | Use Case |
|---|---|---|
| KEYS_ONLY | Only the primary key attributes | When you just need to know which item changed |
| NEW_IMAGE | The entire item after the change | Processing the current state |
| OLD_IMAGE | The entire item before the change | Auditing what was changed |
| NEW_AND_OLD_IMAGES | Both before and after | Comparing changes, computing deltas |
aws dynamodb update-table \
--table-name Orders \
--stream-specification \
StreamEnabled=true,StreamViewType=NEW_AND_OLD_IMAGES
Each stream record contains:
| Field | Description |
|---|---|
| eventID | Unique identifier for the stream record |
| eventName | INSERT, MODIFY, or REMOVE |
| dynamodb.Keys | Primary key of the changed item |
| dynamodb.NewImage | Item after the change (if applicable) |
| dynamodb.OldImage | Item before the change (if applicable) |
| dynamodb.SequenceNumber | Position in the stream |
| dynamodb.StreamViewType | The view type configured |
| eventSourceARN | ARN of the stream |
The most common pattern is triggering an AWS Lambda function from a DynamoDB Stream:
aws lambda create-event-source-mapping \
--function-name ProcessOrderChanges \
--event-source-arn arn:aws:dynamodb:eu-west-1:123456789012:table/Orders/stream/2024-01-01T00:00:00.000 \
--starting-position LATEST \
--batch-size 100 \
--maximum-batching-window-in-seconds 5
exports.handler = async (event) => {
for (const record of event.Records) {
console.log('Event:', record.eventName);
console.log('Keys:', JSON.stringify(record.dynamodb.Keys));
if (record.eventName === 'INSERT') {
const newItem = record.dynamodb.NewImage;
// Process new order...
} else if (record.eventName === 'MODIFY') {
const oldItem = record.dynamodb.OldImage;
const newItem = record.dynamodb.NewImage;
// Process order update...
} else if (record.eventName === 'REMOVE') {
const oldItem = record.dynamodb.OldImage;
// Process order deletion...
}
}
};
Subscribe to continue reading
Get full access to this lesson and all 10 lessons in this course.