📖 Scenario: You manage a DynamoDB table that stores user session data. Each session should expire automatically after a set time. You want to archive expired sessions to another storage before they are deleted.
🎯 Goal: Build a DynamoDB table with TTL enabled and a stream to capture expired items for archival.
📋 What You'll Learn
Create a DynamoDB table named UserSessions with a primary key SessionId (string).
Add an attribute ExpiresAt (number) to store the TTL timestamp.
Enable TTL on the ExpiresAt attribute.
Enable DynamoDB Streams on the table to capture expired item removals.
Write a stream processing function outline that reads expired items from the stream for archival.
💡 Why This Matters
🌍 Real World
Automatically expiring user sessions and archiving them helps keep the database clean and preserves data for audits or analysis.
💼 Career
Understanding TTL and streams in DynamoDB is essential for backend developers and cloud engineers managing scalable, serverless applications.
Progress0 / 4 steps
1
Create DynamoDB table with TTL attribute
Create a DynamoDB table named UserSessions with a primary key SessionId of type string. Add an attribute ExpiresAt of type number to store the TTL timestamp.
DynamoDB
Hint
Use TableName, KeySchema, and AttributeDefinitions to define the table and attributes.
2
Enable TTL on the ExpiresAt attribute
Add a TTL configuration to enable automatic expiration on the ExpiresAt attribute for the UserSessions table.
DynamoDB
Hint
Use TimeToLiveSpecification with Enabled: true and AttributeName: 'ExpiresAt'.
3
Enable DynamoDB Streams for expired item capture
Enable DynamoDB Streams on the UserSessions table with StreamViewType set to OLD_IMAGE to capture expired item removals.
DynamoDB
Hint
Use StreamSpecification with StreamEnabled: true and StreamViewType: 'OLD_IMAGE'.
4
Write stream processing function outline for archival
Write a function named processExpiredSessions that takes a streamEvent parameter. Inside, iterate over streamEvent.Records and for each record with eventName equal to REMOVE, extract the OldImage and add it to an archivedSessions list for archival.
DynamoDB
Hint
Use a for loop over streamEvent.Records and check record.eventName === 'REMOVE'.
Practice
(1/5)
1. What is the main purpose of enabling TTL (Time to Live) on a DynamoDB table?
easy
A. Increase read capacity units for faster queries
B. Encrypt data at rest for security
C. Create backups of the table automatically
D. Automatically delete expired items to keep the table clean
Solution
Step 1: Understand TTL function
TTL marks items with an expiration timestamp so DynamoDB can delete them automatically after expiry.
Step 2: Identify TTL's main benefit
This automatic deletion helps keep the table clean without manual intervention.
Final Answer:
Automatically delete expired items to keep the table clean -> Option D
Quick Check:
TTL = automatic deletion [OK]
Hint: TTL deletes expired data automatically [OK]
Common Mistakes:
Confusing TTL with encryption
Thinking TTL creates backups
Assuming TTL increases capacity
2. Which of the following is the correct way to enable TTL on a DynamoDB table using AWS CLI?
easy
A. aws dynamodb enable-ttl --table MyTable --attribute expiryDate
B. aws dynamodb set-ttl --table-name MyTable --ttl-attribute expiryDate
C. aws dynamodb update-time-to-live --table-name MyTable --time-to-live-specification Enabled=true,AttributeName=expiryDate
D. aws dynamodb activate-ttl --table MyTable --attributeName expiryDate
Solution
Step 1: Recall AWS CLI TTL command
The correct command is 'update-time-to-live' with parameters for table name and TTL specification.
Hint: Use 'update-time-to-live' with Enabled=true [OK]
Common Mistakes:
Using non-existent commands like enable-ttl
Wrong parameter names
Missing 'time-to-live-specification' flag
3. Given a DynamoDB Stream enabled on a table with TTL, what type of event will appear in the stream when an item expires and is deleted?
medium
A. INSERT
B. REMOVE
C. MODIFY
D. UPDATE
Solution
Step 1: Understand TTL deletion effect on streams
When TTL deletes an item, DynamoDB Streams capture this as a REMOVE event because the item is removed.
Step 2: Identify correct event type
INSERT and MODIFY happen on new or updated items, not deletions. REMOVE is the correct event for deletions.
Final Answer:
REMOVE -> Option B
Quick Check:
TTL deletion = REMOVE event [OK]
Hint: TTL deletions trigger REMOVE events in streams [OK]
Common Mistakes:
Choosing INSERT or MODIFY for deletions
Confusing UPDATE with MODIFY
Assuming TTL triggers no stream events
4. You wrote a Lambda function triggered by DynamoDB Streams to archive deleted items. However, the Lambda never receives any events for expired items. What is the most likely cause?
medium
A. The stream is not enabled or not set to capture REMOVE events
B. TTL is not enabled on the table
C. Lambda function has no permissions to read the table
D. The table has no primary key defined
Solution
Step 1: Check stream configuration
If the Lambda never receives events, the stream might be disabled or not configured to capture REMOVE events, which TTL deletions generate.
Step 2: Verify other causes
TTL must be enabled, but if streams are off or missing REMOVE events, Lambda won't get notifications. Permissions or missing primary key would cause other errors.
Final Answer:
The stream is not enabled or not set to capture REMOVE events -> Option A
Quick Check:
Missing REMOVE events in stream = The stream is not enabled or not set to capture REMOVE events [OK]
Hint: Ensure stream captures REMOVE events for TTL deletions [OK]
Common Mistakes:
Ignoring stream settings
Assuming permissions cause no events
Overlooking TTL enablement
5. You want to archive expired DynamoDB items to S3 using a Lambda triggered by Streams. Which approach ensures no data loss and handles possible Lambda failures?
hard
A. Process REMOVE events in Lambda and write directly to S3, retrying on failure
B. Use TTL to delete items, ignore streams, and periodically scan table for expired items
C. Disable TTL and manually delete items after archiving
D. Process INSERT events in Lambda and archive items before expiration
Solution
Step 1: Understand archival with TTL and streams
TTL deletes expired items, streams capture REMOVE events. Lambda triggered by these events can archive data safely.
Step 2: Ensure reliability and no data loss
Processing REMOVE events and writing to S3 with retries ensures data is archived even if Lambda fails temporarily.
Step 3: Evaluate other options
Use TTL to delete items, ignore streams, and periodically scan table for expired items ignores streams and risks missing data. Disable TTL and manually delete items after archiving disables TTL, losing automatic cleanup. Process INSERT events in Lambda and archive items before expiration archives before expiration, which is not TTL-based archival.
Final Answer:
Process REMOVE events in Lambda and write directly to S3, retrying on failure -> Option A
Quick Check:
Reliable archival = Process REMOVE events in Lambda and write directly to S3, retrying on failure [OK]
Hint: Archive REMOVE events with retries for safety [OK]
Common Mistakes:
Ignoring stream REMOVE events
Disabling TTL unnecessarily
Archiving on INSERT instead of REMOVE
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.