What Is TTL In AWS DynamoDB And How We Can Enable It On Our Table Items
How to enable time to live on our data in our DynamoDB table
DynamoDB is a powerful and infinitely scalable noSQL database from AWS. It is fully managed, meaning you have no database maintenance to do, no need to patch or fix anything as that is all managed by AWS.
Furthermore, AWS provides us with the capabilities to scale our database infinitely by letting us separate our data into partitions, again all managed by DynamoDB — this makes it a very powerful database and simple to scale.
Amongst its many remarkable features, DynamoDB offers a very useful feature called TTL or Time To Live for its data.
TTL lets us define an “expiration date” on an attribute we want to be deleted automatically from our table.
Some common use cases why you might want to use TTL are:
Event Scheduling
Cache invalidation
Session Management
Temporary data
In the case of temporary data or event scheduling, we often want to delete attributes after a given date period, such as “startDate” or “verificationCode”.
A popular technique for this is to use TTL.
We’ll add a TTL flag to a start date attribute and have it deleted when the current date surpasses that value.
To do this we can use TTLs in DynamoDB
Implementing TTLs
If you head to the DynamoDB console you can create a table or click on an existing table.
The first thing we’ll do is add an item to our table. Here you can create a simple item of type string, with an attribute called code:
Click Create item to create it. You’ll then be redirected back to the table page.
On your table page, go to the tabs section and slide to the right to reveal the Additional settings tab, click on it:
Scroll down until you find the Time to Live section:
Click the Turn on button. You’ll be redirected to a page to define your TTL.
Choose the attribute you want to have expire. You can then set an expiration time for the attribute — set a date a few minutes in the future to test the feature (or you can Run preview).
Click the orange button Turn on TTL to activate the TTL on the attribute code.
Return to the items in your table and you should now see a “TTL” label on your attribute code on every item in the table:
Wait for the expiration date to arrive and you will see the attribute was deleted successfully.
*Disclaimer: DynamoDB states that attributes with TTLs can take up to 48 hours to be deleted by
This may not be useful for verification codes or similar features, so use it for items that can afford the delay.
👋 My name is Uriel Bitton and I’m committed to helping you master Serverless, Cloud Computing, and AWS.
🚀 If you want to learn how to build serverless, scalable, and resilient applications, you can subscribe to my blog:
https://medium.com/@atomicsdigital/subscribe
Thanks for reading and see you in the next one!