Lambda triggers on stream events let you run code automatically when your database changes. This helps you react quickly to updates without checking manually.
Lambda trigger on stream events in DynamoDB
1. Enable DynamoDB Streams on your table. 2. Create a Lambda function. 3. Configure the Lambda function as a trigger for the DynamoDB stream. Example JSON event structure received by Lambda: { "Records": [ { "eventID": "string", "eventName": "INSERT | MODIFY | REMOVE", "dynamodb": { "Keys": { ... }, "NewImage": { ... }, "OldImage": { ... }, "SequenceNumber": "string", "SizeBytes": number, "StreamViewType": "NEW_IMAGE | OLD_IMAGE | NEW_AND_OLD_IMAGES | KEYS_ONLY" }, "eventSource": "aws:dynamodb", "eventVersion": "1.1", "eventSourceARN": "string" } ] }
You must enable DynamoDB Streams on your table before Lambda can receive events.
The Lambda function receives an event object containing records of changes.
exports.handler = async (event) => { for (const record of event.Records) { console.log('Event Name:', record.eventName); console.log('New Image:', JSON.stringify(record.dynamodb.NewImage)); } };
exports.handler = async (event) => { event.Records.forEach(record => { if (record.eventName === 'INSERT') { console.log('New item added:', JSON.stringify(record.dynamodb.NewImage)); } }); };
This Lambda function collects all new items added in the event and logs them together.
exports.handler = async (event) => { const addedItems = []; for (const record of event.Records) { if (record.eventName === 'INSERT') { addedItems.push(record.dynamodb.NewImage); } } console.log('Added items:', JSON.stringify(addedItems)); };
Lambda triggers run automatically and do not require manual polling.
StreamViewType setting controls what data is sent to Lambda (new image, old image, or both).
Remember to give your Lambda function permission to read from the DynamoDB stream.
Lambda triggers on DynamoDB streams let you run code when data changes.
Enable streams, create a Lambda, and connect them as a trigger.
Use the event data to react to inserts, updates, or deletes automatically.
Practice
Solution
Step 1: Understand DynamoDB streams
DynamoDB streams capture data changes like inserts, updates, and deletes.Step 2: Role of Lambda triggers
Lambda triggers run code automatically when these stream events occur.Final Answer:
To automatically run code when data in the DynamoDB table changes -> Option DQuick Check:
Lambda triggers = auto-run code on data change [OK]
- Thinking Lambda triggers require manual execution
- Confusing stream enabling with backup
- Assuming Lambda disables streams
Solution
Step 1: Enable stream on DynamoDB table
Streams must be enabled first to capture data changes.Step 2: Add Lambda as a trigger on the enabled stream
Lambda triggers are linked to the stream to react to events.Final Answer:
Enable stream on the table, then add Lambda as a trigger on the stream -> Option AQuick Check:
Enable stream first, then add Lambda trigger [OK]
- Trying to add Lambda trigger before enabling stream
- Not linking Lambda to the stream
- Enabling stream and Lambda separately without connection
def lambda_handler(event, context):
for record in event['Records']:
if record['eventName'] == 'INSERT':
print(record['dynamodb']['NewImage']['id']['S'])
What will this code output when a new item with id '123' is inserted?
Solution
Step 1: Check eventName condition
The code checks if eventName is 'INSERT', which matches new item insertion.Step 2: Access NewImage id value
It accesses 'NewImage' dictionary and prints the 'id' string value '123'.Final Answer:
It prints '123' to the logs -> Option AQuick Check:
INSERT event prints new id '123' [OK]
- Confusing NewImage with OldImage
- Assuming eventName is MODIFY instead of INSERT
- Not accessing nested dictionary keys correctly
def lambda_handler(event, context):
for record in event['Records']:
print(record['dynamodb']['NewImage']['id']['S'])
What is the likely cause of the error?
Solution
Step 1: Understand stream event types
DELETE events do not have 'NewImage', only 'OldImage'.Step 2: Accessing NewImage without check causes error
Trying to access 'NewImage' on DELETE events causes a KeyError.Final Answer:
The event may contain DELETE events without NewImage data -> Option BQuick Check:
DELETE events lack NewImage causing runtime error [OK]
- Assuming all events have NewImage
- Ignoring DELETE event structure
- Believing Lambda can't access streams
Solution
Step 1: Understand stream filtering limits
DynamoDB streams cannot filter by attribute values, only event types.Step 2: Use Lambda code to check eventName and compare status
Lambda can check if eventName is 'MODIFY' and compare OldImage and NewImage 'status' to detect change to 'completed'.Final Answer:
In Lambda, check if eventName is 'MODIFY' and compare OldImage and NewImage 'status' values -> Option CQuick Check:
Filter in Lambda by event and attribute change [OK]
- Trying to filter by attribute in stream settings
- Using KEYS_ONLY which lacks attribute data
- Creating multiple Lambdas unnecessarily
Ready to prove it?
Pass the test and this pattern moves unknown
