0
0
DynamodbHow-ToBeginner ยท 3 min read

How to Enable TTL in DynamoDB: Step-by-Step Guide

To enable TTL in DynamoDB, specify a table and a timestamp attribute that stores the expiry time in Unix epoch format. Use the AWS Management Console, AWS CLI, or SDK to activate TimeToLiveSpecification with the attribute name, so expired items are automatically deleted.
๐Ÿ“

Syntax

Enabling TTL requires specifying the table name and the attribute that holds the expiration timestamp. The attribute must be a number representing Unix epoch time in seconds.

The key part is the TimeToLiveSpecification object with two properties:

  • Enabled: Set to true to activate TTL.
  • AttributeName: The name of the attribute storing the expiry timestamp.
bash
aws dynamodb update-time-to-live \
    --table-name YourTableName \
    --time-to-live-specification Enabled=true,AttributeName=ttlAttribute
๐Ÿ’ป

Example

This example uses AWS CLI to enable TTL on a table named Orders with the TTL attribute called expiryTime. The attribute should contain the expiration time as a Unix timestamp in seconds.

bash
aws dynamodb update-time-to-live \
    --table-name Orders \
    --time-to-live-specification Enabled=true,AttributeName=expiryTime
Output
{ "TimeToLiveSpecification": { "Enabled": true, "AttributeName": "expiryTime" } }
โš ๏ธ

Common Pitfalls

  • Wrong attribute type: TTL attribute must be a number (Unix epoch time in seconds), not a string or date format.
  • TTL attribute missing: Items without the TTL attribute are never deleted.
  • TTL disabled: Forgetting to set Enabled=true means TTL won't work.
  • Delay in deletion: TTL deletion is not immediate; it can take up to 48 hours after expiration.
bash
aws dynamodb update-time-to-live \
    --table-name Orders \
    --time-to-live-specification Enabled=false,AttributeName=expiryTime

# Correct way:
aws dynamodb update-time-to-live \
    --table-name Orders \
    --time-to-live-specification Enabled=true,AttributeName=expiryTime
๐Ÿ“Š

Quick Reference

ParameterDescription
TableNameName of the DynamoDB table
EnabledSet to true to activate TTL
AttributeNameName of the attribute storing expiration time (Unix epoch seconds)
TTL Attribute TypeMust be Number (Unix epoch time in seconds)
Deletion DelayExpired items may be deleted up to 48 hours later
โœ…

Key Takeaways

Enable TTL by specifying the expiration attribute and setting Enabled=true.
TTL attribute must be a number representing Unix epoch time in seconds.
Items without the TTL attribute are not deleted automatically.
TTL deletion is asynchronous and can take up to 48 hours after expiry.
Use AWS CLI, SDK, or Console to configure TTL on your DynamoDB table.