Enable time to live (TTL) in DynamoDB
Note
To assist in debugging and verification of proper operation of the TTL feature, the values provided for the item TTL are logged in plaintext in DynamoDB diagnostic logs.
You can enable TTL in the Amazon DynamoDB Console, the AWS Command Line Interface (AWS CLI), or using the Amazon DynamoDB API Reference with any of the supposed AWS SDKs. It takes approximately one hour to enable TTL across all partitions.
Sign in to the AWS Management Console and open the DynamoDB console at https://console.aws.amazon.com/dynamodb/
. -
Choose Tables, and then choose the table that you want to modify.
-
In the Additional settings tab, in the Time to Live (TTL) section, choose Turn on to enable TTL.
-
When enabling TTL on a table, DynamoDB requires you to identify a specific attribute name that the service will look for when determining if an item is eligible for expiration. The TTL attribute name, shown below, is case sensitive and must match the attribute defined in your read and write operations. A mismatch will cause expired items to go undeleted. Renaming the TTL attribute requires you to disable TTL and then re-enable it with the new attribute going forward. TTL will continue to process deletions for approximately 30 minutes once it is disabled. TTL must be reconfigured on restored tables.
-
(Optional) You can perform a test by simulating the date and time of the expiration and matching a few items. This provides you with a sample list of items and confirms that there are items containing the TTL attribute name provided along with the expiration time.
After TTL is enabled, the TTL attribute is marked TTL when you view items on the DynamoDB console. You can view the date and time that an item expires by hovering your pointer over the attribute.
-
Enable TTL on the
TTLExample
table.aws dynamodb update-time-to-live --table-name TTLExample --time-to-live-specification "Enabled=true, AttributeName=ttl"
-
Describe TTL on the
TTLExample
table.aws dynamodb describe-time-to-live --table-name TTLExample { "TimeToLiveDescription": { "AttributeName": "ttl", "TimeToLiveStatus": "ENABLED" } }
-
Add an item to the
TTLExample
table with the Time to Live attribute set using the BASH shell and the AWS CLI.EXP=`date -d '+5 days' +%s` aws dynamodb put-item --table-name "TTLExample" --item '{"id": {"N": "1"}, "ttl": {"N": "'$EXP'"}}'
This example starts with the current date and adds 5 days to it to create an
expiration time. Then, it converts the expiration time to epoch time format to
finally add an item to the "TTLExample
" table.
Note
One way to set expiration values for Time to Live is to calculate the number of seconds to add to the expiration time. For example, 5 days is 432,000 seconds. However, it is often preferable to start with a date and work from there.
It is fairly simple to get the current time in epoch time format, as in the following examples.
-
Linux Terminal:
date +%s
-
Python:
import time; int(time.time())
-
Java:
System.currentTimeMillis() / 1000L
-
JavaScript:
Math.floor(Date.now() / 1000)
AWSTemplateFormatVersion: "2010-09-09" Resources: TTLExampleTable: Type: AWS::DynamoDB::Table Description: "A DynamoDB table with TTL Specification enabled" Properties: AttributeDefinitions: - AttributeName: "Album" AttributeType: "S" - AttributeName: "Artist" AttributeType: "S" KeySchema: - AttributeName: "Album" KeyType: "HASH" - AttributeName: "Artist" KeyType: "RANGE" ProvisionedThroughput: ReadCapacityUnits: "5" WriteCapacityUnits: "5" TimeToLiveSpecification: AttributeName: "TTLExampleAttribute" Enabled: true
Additional details on using TTL within your AWS CloudFormation templates can be found here.