Home > Back-end >  Can I set the TTL value while enabling TTL on a DynamoDB table via the AWS CLI?
Can I set the TTL value while enabling TTL on a DynamoDB table via the AWS CLI?

Time:11-07

We have a requirement to delete dynamodb items that are 3 days old so I tried default AWS CLI update query but the query doesn't take input value for TTL.

As per documentation, I'm following the below query to activate dynamoDB TTL(Time to Live) however when it is activiated it defaults to one hour however I want a query that will take 3 days as the TTL value, can anyone help me with the correct query if it's available, we are creating a script deployment so we won't be doing via UI console.

aws dynamodb update-time-to-live \
    --table-name MusicCollection \
    --time-to-live-specification Enabled=true,AttributeName=ttl

As shown in below image, items are not deleted passed the current time, I guess it deletes after an hour.

enter image description here

enter image description here

CodePudding user response:

DynamoDB Time to Live (TTL) only deals with you specifying a specific timestamp attribute as the TTL value & AWS deleting ('expiring') the record after the date and time of the timestamp has elapsed.

The aws dynamodb update-time-to-live only enables TTL on a specific attribute.

It does not set the TTL value for you.

The Simulated date and time option simply only simulates that specific point in time and then shows you a few items that would be expired. It's for your to check your TTL value. Again, it does not set it for you.

You will need to update your item(s) manually using put-item to specify a TTL value; there is no feature within DynamoDB to populate a TTL value.

Try using this as a starting point, if your script is in Bash:

EXP=`date -d ' 3 days'  %s`
aws dynamodb put-item \
--table-name "MusicCollection" \
--item '{"id": {"N": "1"}, "ttl": {"N": "'$EXP'"}}'

CodePudding user response:

TTL does not default to 1hour. TTL deletes items which exceed the current time.

For that reason if you want items to expire after 3 days then you need to set a TTL value on those items for 3 days in the future.

Bear in mind it can take up to 48 hours for TTL to evict the item, meaning the item will be removed between 3 and 5 days. To overcome this, you can add a FilterExpression to your read requests where you drop any items which are older than 3 days.

https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/TTL.html

  • Related