📖 Scenario: You are managing a cloud database where data should automatically expire after a certain time to save space and keep information fresh.Imagine a library where books are borrowed and must be returned after a fixed period. Similarly, data entries will have a 'time to live' (TTL) that tells the system when to delete them.
🎯 Goal: Create a DynamoDB table with a TTL attribute that automatically deletes expired items.
📋 What You'll Learn
Create a DynamoDB table named LibraryBooks with a primary key BookID of type string.
Add a TTL attribute named ExpirationTime to the table configuration.
Set the TTL attribute to enable automatic expiration of items.
Use AWS CloudFormation syntax for the configuration.
💡 Why This Matters
🌍 Real World
TTL is used in cloud databases to automatically remove outdated data, saving storage and improving performance.
💼 Career
Understanding TTL configuration is important for cloud engineers and architects managing scalable, cost-efficient databases.
Progress0 / 4 steps
1
Create DynamoDB table resource
Create a DynamoDB table resource named LibraryBooks with a primary key attribute BookID of type String in AWS CloudFormation YAML syntax.
AWS
Hint
Define the DynamoDB table with Type: AWS::DynamoDB::Table. Use AttributeDefinitions and KeySchema to set the primary key.
2
Add TTL attribute name configuration
Add a TimeToLiveSpecification property to the LibraryBooks resource with AttributeName set to ExpirationTime.
AWS
Hint
Use TimeToLiveSpecification inside Properties to specify the TTL attribute name.
3
Enable TTL for the attribute
Add Enabled: true inside the TimeToLiveSpecification property to activate TTL on the ExpirationTime attribute.
AWS
Hint
Set Enabled to true to turn on TTL for the attribute.
4
Complete the CloudFormation template
Add the ExpirationTime attribute to AttributeDefinitions with type Number to store the TTL timestamp.
AWS
Hint
TTL attribute must be defined as a Number type in AttributeDefinitions.
Practice
(1/5)
1. What is the main purpose of Time to Live (TTL) in AWS DynamoDB?
easy
A. Backup data to another region
B. Encrypt data at rest for security
C. Automatically delete expired items to save storage space
D. Increase read capacity for faster queries
Solution
Step 1: Understand TTL function
TTL is designed to remove items automatically after a set expiration time.
Step 2: Identify the main benefit
This automatic deletion helps save storage space and reduces manual cleanup.
Final Answer:
Automatically delete expired items to save storage space -> Option C
Quick Check:
TTL purpose = automatic deletion [OK]
Hint: TTL deletes expired data automatically to save space [OK]
Common Mistakes:
Confusing TTL with encryption
Thinking TTL backs up data
Assuming TTL improves query speed
2. Which attribute type must the TTL attribute have in DynamoDB to enable expiration?
easy
A. Number representing Unix epoch time in seconds
B. Binary data with expiration timestamp
C. Boolean indicating expired or not
D. String containing date in ISO format
Solution
Step 1: Check TTL attribute requirements
DynamoDB TTL requires a Number attribute representing expiration time as Unix epoch seconds.
Step 2: Eliminate other types
String, Boolean, or Binary types are not valid for TTL expiration timestamps.
Final Answer:
Number representing Unix epoch time in seconds -> Option A
Quick Check:
TTL attribute = Number (epoch seconds) [OK]
Hint: TTL attribute must be a number with epoch seconds [OK]
Common Mistakes:
Using string dates instead of numbers
Setting TTL attribute as Boolean
Trying to use binary data for TTL
3. Given this DynamoDB item with TTL attribute expireAt: 1700000000 (Unix epoch seconds), what happens when current time is 1700000001?
medium
A. Item remains until manually deleted
B. Item's TTL attribute is reset to current time
C. Item is archived to S3 automatically
D. Item is immediately deleted by DynamoDB TTL process
Solution
Step 1: Compare current time with TTL value
The current time (1700000001) is greater than the TTL attribute (1700000000), so the item is expired.
Step 2: Understand TTL behavior on expiration
DynamoDB TTL automatically deletes expired items shortly after expiration time passes.
Final Answer:
Item is immediately deleted by DynamoDB TTL process -> Option D
Quick Check:
Current time > TTL = item deleted [OK]
Hint: Item deletes soon after TTL time passes [OK]
Common Mistakes:
Thinking TTL deletes instantly at exact second
Assuming manual deletion is needed
Believing TTL archives data instead of deleting
4. You enabled TTL on attribute expiry but items are not deleting after expiration. What is the most likely cause?
medium
A. DynamoDB table is in a different region
B. TTL attribute expiry is missing or has wrong data type
C. TTL only works on primary key attributes
D. TTL requires manual activation every 24 hours
Solution
Step 1: Check TTL attribute correctness
If the TTL attribute is missing or not a Number with epoch seconds, TTL won't delete items.
Step 2: Verify other options
TTL works regardless of region, does not require manual activation, and does not need to be primary key.
Final Answer:
TTL attribute expiry is missing or has wrong data type -> Option B
Quick Check:
Wrong TTL attribute = no deletion [OK]
Hint: TTL attribute must exist and be correct type [OK]
Common Mistakes:
Thinking TTL depends on table region
Believing TTL needs manual daily activation
Assuming TTL attribute must be primary key
5. You want to store session data that expires exactly 30 minutes after creation using DynamoDB TTL. Which approach correctly sets the TTL attribute?
hard
A. Set TTL attribute to current Unix epoch time in seconds plus 1800 seconds
B. Set TTL attribute to current time as ISO string plus 30 minutes
C. Set TTL attribute to Boolean true when session expires
D. Set TTL attribute to current Unix epoch time in milliseconds plus 1800000
Solution
Step 1: Calculate TTL value correctly
TTL attribute must be a Number in seconds, so add 1800 seconds (30 minutes) to current epoch seconds.
Step 2: Eliminate incorrect formats
ISO strings, Booleans, or milliseconds are invalid for TTL attribute in DynamoDB.
Final Answer:
Set TTL attribute to current Unix epoch time in seconds plus 1800 seconds -> Option A
Quick Check:
TTL = epoch seconds + 1800 [OK]
Hint: TTL value = current epoch seconds + 1800 for 30 minutes [OK]
Common Mistakes:
Using ISO date strings instead of epoch seconds
Using milliseconds instead of seconds
Setting TTL as Boolean instead of number
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.