📖 Scenario: You are building a simple inventory system that tracks product stock levels. When a product's stock changes, you want to capture this event and store it separately for auditing and analytics.
🎯 Goal: Create a DynamoDB table to store products, enable DynamoDB Streams to capture changes, and write a simple event handler function that processes these change events.
📋 What You'll Learn
Create a DynamoDB table named Products with ProductID as the primary key.
Enable DynamoDB Streams on the Products table to capture item modifications.
Create a variable called stream_arn to hold the stream ARN of the Products table.
Write a function called process_event that takes a DynamoDB stream event record and extracts the ProductID and Stock values.
Add a final step to simulate processing a sample event record.
💡 Why This Matters
🌍 Real World
Event-driven architectures are common in modern cloud applications to react to data changes in real time, such as updating dashboards or triggering workflows.
💼 Career
Understanding how to use DynamoDB Streams and process events is valuable for backend developers and cloud engineers working with AWS services.
Progress0 / 4 steps
1
Create the DynamoDB Products table
Create a variable called products_table that represents a DynamoDB table named Products with ProductID as the primary key.
DynamoDB
Hint
Define products_table as a dictionary with keys TableName, KeySchema, and AttributeDefinitions.
2
Enable DynamoDB Streams on the Products table
Add a key StreamSpecification to products_table with StreamEnabled set to True and StreamViewType set to NEW_AND_OLD_IMAGES. Then create a variable called stream_arn and set it to the string 'arn:aws:dynamodb:region:account-id:table/Products/stream/2024-06-01T00:00:00.000'.
DynamoDB
Hint
Update StreamSpecification inside products_table and create stream_arn as a string variable.
3
Write the event processing function
Write a function called process_event that takes one parameter record. Inside the function, extract ProductID and Stock from record['dynamodb']['NewImage'] assuming they are strings and numbers respectively, and return them as a tuple (product_id, stock).
DynamoDB
Hint
Use dictionary keys to access NewImage data and convert stock to integer.
4
Simulate processing a sample event record
Create a variable called sample_record that mimics a DynamoDB stream event record with ProductID set to 'P123' and Stock set to 50. Then call process_event(sample_record) and assign the result to a variable called result.
DynamoDB
Hint
Define sample_record as a nested dictionary matching DynamoDB stream format and call process_event.
Practice
(1/5)
1. What is the main purpose of using DynamoDB Streams in an event-driven architecture?
easy
A. To capture and record changes in the DynamoDB table as events
B. To store backup copies of the DynamoDB table
C. To increase the read capacity of the DynamoDB table
D. To encrypt data stored in DynamoDB
Solution
Step 1: Understand DynamoDB Streams role
DynamoDB Streams capture data modification events like inserts, updates, and deletes.
Step 2: Identify the purpose in event-driven architecture
These captured events trigger other processes automatically, enabling real-time reactions.
Final Answer:
To capture and record changes in the DynamoDB table as events -> Option A
Quick Check:
DynamoDB Streams = capture changes [OK]
Hint: Streams capture table changes as events for triggers [OK]
Common Mistakes:
Confusing Streams with backups
Thinking Streams increase capacity
Assuming Streams encrypt data
2. Which of the following is the correct way to enable DynamoDB Streams on a table using AWS CLI?
easy
A. aws dynamodb set-stream --table MyTable --enabled true
B. aws dynamodb enable-stream --table MyTable --type NEW_IMAGE
C. aws dynamodb create-stream --table-name MyTable --view-type KEYS_ONLY
D. aws dynamodb update-table --table-name MyTable --stream-specification StreamEnabled=true,StreamViewType=NEW_AND_OLD_IMAGES
Solution
Step 1: Recall AWS CLI command for enabling streams
The correct command uses 'update-table' with '--stream-specification' to enable streams.
Step 2: Verify syntax and parameters
aws dynamodb update-table --table-name MyTable --stream-specification StreamEnabled=true,StreamViewType=NEW_AND_OLD_IMAGES correctly sets StreamEnabled=true and specifies StreamViewType as NEW_AND_OLD_IMAGES.
Final Answer:
aws dynamodb update-table --table-name MyTable --stream-specification StreamEnabled=true,StreamViewType=NEW_AND_OLD_IMAGES -> Option D