How does DynamoDB integrate with AWS Lambda for event-driven processing?
See how DynamoDB Streams and Lambda event-source mappings enable serverless, event-driven processing with batching, ordering and built-in retry handling.
Expected Interview Answer
DynamoDB integrates with AWS Lambda through an event-source mapping on a DynamoDB Stream: Lambda polls the stream's shards, batches change records, and invokes your function with them, letting you process inserts, updates, and deletes without managing servers or polling code.
You enable a stream on the table, then create an event-source mapping that tells Lambda to read that stream. Lambda's service polls the shards, assembles records into batches (tunable by batch size and batching window), and invokes the function synchronously per shard, preserving per-shard order. On errors it retries the batch until success or record expiry, with options like bisect-on-error, maximum retry attempts, and an on-failure destination for poison records. This powers event-driven pipelines such as replication, notifications, aggregation, and search indexing with automatic scaling by shard.
- Fully serverless, no polling infrastructure to run
- Automatic scaling as stream shards grow
- Preserves per-shard ordering of changes
- Tunable batching for throughput and cost
- Built-in retries and failure handling for bad records
AI Mentor Explanation
The stream is the live scoring feed and Lambda is the statistician on standby: they don't sit refreshing the scoreboard, they're handed each new over's deliveries in order and immediately update the averages. The board (AWS) delivers the balls in batches, and if a calculation errors the statistician retries that over until it's right before moving on.
Step-by-Step Explanation
Step 1
Enable the stream
Turn on DynamoDB Streams for the table with a view type that includes the images your function needs.
Step 2
Create an event-source mapping
Point Lambda at the stream ARN and set batch size, batching window, and starting position.
Step 3
Lambda polls the shards
The Lambda service reads shards, batches records, and invokes your function per shard preserving order.
Step 4
Process the batch
Your handler iterates event.Records, branching on eventName (INSERT, MODIFY, REMOVE) to react.
Step 5
Handle failures
Configure retries, bisect-on-error, max retry age, and an on-failure destination for poison records.
What Interviewer Expects
- Knows the integration is a stream event-source mapping, not table polling
- Understands Lambda polls shards and invokes per shard in order
- Can name tuning knobs: batch size and batching window
- Explains retry and poison-record handling options
- Gives IAM/permission awareness for reading the stream
Common Mistakes
- Thinking Lambda continuously polls the table itself, not the stream
- Ignoring that a failing batch is retried and can block the shard
- Not handling all event types (INSERT, MODIFY, REMOVE)
- Forgetting the function's IAM role needs stream read permissions
- Assuming unlimited parallelism instead of one invocation per shard
Best Answer (HR Friendly)
“DynamoDB can automatically trigger a Lambda function whenever data changes. You turn on the table's change stream and connect it to Lambda, so every insert, update, or delete runs your code — no servers to manage and no need to keep checking the database yourself.”
Code Example
aws lambda create-event-source-mapping \
--function-name processOrders \
--event-source-arn arn:aws:dynamodb:us-east-1:111:table/Orders/stream/2026-07-21T00:00:00.000 \
--starting-position LATEST \
--batch-size 100 \
--maximum-batching-window-in-seconds 5export const handler = async (event) => {
for (const record of event.Records) {
switch (record.eventName) {
case 'INSERT':
await onCreate(record.dynamodb.NewImage); break
case 'MODIFY':
await onUpdate(record.dynamodb.OldImage, record.dynamodb.NewImage); break
case 'REMOVE':
await onDelete(record.dynamodb.OldImage); break
}
}
}Follow-up Questions
- How does batch size and batching window affect throughput and cost?
- What happens when a Lambda invocation fails on a stream batch?
- How does Lambda achieve parallelism across shards?
- What IAM permissions does the Lambda role need to read a stream?
- When would you use Kinesis Data Streams for DynamoDB with Lambda instead?
MCQ Practice
1. How does Lambda receive DynamoDB changes?
An event-source mapping has Lambda poll the table's stream shards and invoke the function with batches.
2. How is ordering handled when Lambda processes a stream?
Lambda processes each shard in order with one concurrent invocation per shard, preserving per-partition ordering.
3. What happens to a batch that keeps failing?
Lambda retries the batch and offers bisect-on-error, max retry age, and an on-failure destination for poison records.
Flash Cards
How does DynamoDB trigger Lambda? — Through an event-source mapping on a DynamoDB Stream; Lambda polls shards and invokes with batches.
How is stream ordering preserved? — Lambda runs one concurrent invocation per shard, processing records in order within that shard.
Name two batching tuning knobs. — Batch size and maximum batching window (in seconds).
How are poison records handled? — Via retries, bisect-on-error, max retry age/attempts, and an on-failure destination like an SQS queue or SNS topic.