Serverless Change Data Capture with DynamoDB Streams and Lambda
Learn how to build a serverless change‑data‑capture pipeline with DynamoDB Streams and Lambda, covering stream view choices, event‑source mapping, ordering guarantees, duplicate handling, and monitoring.
08 Jul 2025, 04:53 UTC

The problem: reacting to table changes without polling
Applications often need to trigger downstream work — search indexing, audit logs, cache invalidation — the moment a DynamoDB item is created, updated, or deleted. Polling the table is wasteful and adds latency. DynamoDB Streams paired with an AWS Lambda trigger gives a fully managed, serverless change‑data‑capture (CDC) pipeline, but it comes with ordering guarantees, retention limits, and duplicate‑delivery semantics that you must design for.
Why Streams + Lambda fits the CDC pattern
DynamoDB Streams records every item‑level mutation and retains those records for 24 hours. An event source mapping can invoke a Lambda function for each batch of records, eliminating the need for a long‑running consumer process. The integration is native: no extra infrastructure, automatic scaling, and pay‑per‑invocation pricing.
Configuring the stream view and the Lambda mapping
Choose a stream view type
Four view types control payload size and cost:
KEYS_ONLY– only the partition and sort key.NEW_IMAGE– the full item after the write.OLD_IMAGE– the full item before the write.NEW_AND_OLD_IMAGES– both versions, useful for diff logic.
For most CDC workloads NEW_AND_OLD_IMAGES is the sweet spot, but it roughly doubles the record size compared with KEYS_ONLY.
Create the Lambda function and attach the event source
Run the following AWS CLI commands from a workstation that has the AWS CLI v2 configured with permissions for dynamodb:EnableStream, lambda:CreateFunction, lambda:CreateEventSourceMapping, and iam:PassRole.
# 1. Enable the stream on the table
TABLE_NAME=Orders
aws dynamodb update-table \
--table-name $TABLE_NAME \
--stream-specification StreamEnabled=true,StreamViewType=NEW_AND_OLD_IMAGES
# 2. Capture the stream ARN (returned in the update-table output)
STREAM_ARN=$(aws dynamodb describe-table --table-name $TABLE_NAME \
--query 'Table.LatestStreamArn' --output text)
# 3. Deploy a minimal Lambda (Node.js 20.x shown)
FUNCTION_NAME=OrdersStreamProcessor
ROLE_ARN=arn:aws:iam::123456789012:role/lambda-dynamodb-stream-role
zip function.zip index.js
aws lambda create-function \
--function-name $FUNCTION_NAME \
--runtime nodejs20.x \
--handler index.handler \
--zip-file fileb://function.zip \
--role $ROLE_ARN \
--timeout 30 \
--memory-size 256
# 4. Create the event source mapping with batching
aws lambda create-event-source-mapping \
--function-name $FUNCTION_NAME \
--event-source-arn $STREAM_ARN \
--starting-position LATEST \
--batch-size 100 \
--maximum-batching-window-in-seconds 5
Expected checks: after a write to Orders, open CloudWatch Logs for /aws/lambda/OrdersStreamProcessor. You should see an event object containing a Records array; each record has dynamodb.NewImage and dynamodb.OldImage when the view type is NEW_AND_OLD_IMAGES.
Handling ordering, duplicates, and transaction boundaries
Per‑partition ordering only
Streams guarantee order within a single partition key. If your downstream logic requires global ordering (e.g., a single audit log), you must either funnel all writes through a single partition key or implement a re‑ordering buffer downstream.
Idempotent processing
Lambda retries on failure can deliver the same batch multiple times. Design your handler to be idempotent — for example, write downstream records with a deterministic key derived from the stream record’s eventID or the primary key plus a version attribute.
Transaction splitting
A TransactWriteItems call produces separate stream records for each item, even though the transaction commits atomically. If you need to reconstruct the transaction boundary, correlate records by the shared eventSourceARN and approximate timestamp, but be aware that no explicit transaction identifier is emitted.
Trade‑offs, limits, and how to verify health
| Aspect | Implication | Mitigation |
|---|---|---|
| 24‑hour retention | Consumers lagging >24 h lose records permanently. | Monitor IteratorAgeMilliseconds CloudWatch metric; alert when it exceeds 12 h. |
| Payload size | NEW_AND_OLD_IMAGES can push records near the 1 MB limit. | Compress large attributes client‑side or switch to NEW_IMAGE if old image isn’t needed. |
| Duplicate deliveries | Retry logic may invoke the function with the same batch. | Make handlers idempotent; store processed eventID in a fast store (e.g., DynamoDB TTL table). |
| Cost | Read‑request units for stream reads + Lambda GB‑seconds. | Tune batch-size and maximum-batching-window-in-seconds to balance latency vs. invocation count. |
Practical verification steps
- Write a test item to the table (AWS Console or CLI).
- Confirm the Lambda log shows a
Recordsentry with the expected view fields. - In CloudWatch, graph
IteratorAgeMillisecondsfor the event source mapping; it should drop to near zero after the batch processes. - Introduce a deliberate error in the handler, redeploy, and verify that the same batch appears again (duplicate delivery) and that your idempotency guard prevents double‑processing.
Actionable next steps
Enable the stream on a low‑traffic table, attach a logging Lambda, and run the verification steps above. Once you see stable IteratorAge and idempotent behavior, promote the pattern to production tables. Add a CloudWatch alarm on IteratorAgeMilliseconds > 43200000 (12 h) and a dashboard that shows invocation count, error rate, and batch size. This gives you a serverless CDC pipeline that scales automatically while keeping operational overhead minimal.