TTL, Backups & PITR - AWS Serverless
1. TTL (Time to Live)
TTL in DynamoDB allows you to define a specific time period after which items are automatically deleted. This is useful for managing data lifecycle without manual intervention.
How TTL Works
- Define a TTL attribute in your table (e.g., `expirationTime`).
- Set the value to a timestamp in epoch format (Unix time).
- DynamoDB automatically deletes items once the current time exceeds the TTL value.
Example of Setting TTL
{
"TableName": "YourTableName",
"AttributeDefinitions": [
{
"AttributeName": "id",
"AttributeType": "S"
},
{
"AttributeName": "expirationTime",
"AttributeType": "N"
}
],
"TimeToLiveSpecification": {
"AttributeName": "expirationTime",
"TimeToLiveStatus": "ENABLED"
}
}
2. Backups
DynamoDB offers on-demand and continuous backup options to protect your data. These backups can be restored to a new table.
Types of Backups
- On-Demand Backup: Manual backups created at any time.
- Continuous Backup: Enables point-in-time recovery (PITR).
Creating an On-Demand Backup
aws dynamodb create-backup \
--table-name YourTableName \
--backup-name YourBackupName
3. Point-In-Time Recovery (PITR)
PITR allows you to restore your table to any point in time within the last 35 days, providing an additional layer of data protection.
Enabling PITR
aws dynamodb update-table \
--table-name YourTableName \
--point-in-time-recovery-specification '{"PointInTimeRecoveryStatus": "ENABLED"}'
Restoring from PITR
aws dynamodb restore-table-to-point-in-time \
--source-table-name YourTableName \
--target-table-name YourRestoredTableName \
--restore-date-time 2021-10-01T12:00:00Z
4. Best Practices
- Regularly monitor TTL settings to ensure data is being deleted as expected.
- Use on-demand backups before performing major changes to your table.
- Enable PITR to safeguard against accidental data loss.
- Test your backup and restore processes periodically.
5. FAQ
What happens if I do not set TTL?
If TTL is not set, items will remain in the table until manually deleted or the table is deleted.
Can I set TTL on existing items?
Yes, you can add a TTL attribute to existing items by updating them with the desired expiration timestamp.
Is PITR enabled by default?
No, PITR must be enabled explicitly for each table.