0
0
DynamoDBquery~30 mins

TTL with Streams for archival in DynamoDB - Mini Project: Build & Apply

Choose your learning style9 modes available
TTL with Streams for archival
📖 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
Need a 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
Need a 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
Need a 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
Need a hint?

Use a for loop over streamEvent.Records and check record.eventName === 'REMOVE'.