CloudFormation - Enable TTL for DynamoDB Create Table
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Amazon CloudFormation is a pivotal service in the AWS ecosystem, enabling developers to define, provision, and manage their cloud infrastructure through code. One of the many resources that can be defined and managed using CloudFormation is Amazon DynamoDB, a fast and flexible NoSQL database service for any scale. Understanding how to enable Time to Live (TTL) during the creation of a DynamoDB table using CloudFormation provides significant benefits, including automated data lifecycle management, reduced storage costs, and efficient handling of stale data. In this article, we will explore this feature in detail, focusing on how to implement it within a CloudFormation template.
What is Time to Live (TTL) in DynamoDB?
Time to Live (TTL) is a feature in Amazon DynamoDB that allows you to automatically delete expired items from your tables. This feature is particularly useful for reducing storage costs, managing data retention policies, and ensuring that your database maintains optimal performance by not holding on to stale data.
When you specify TTL, you set an attribute that records a timestamp (in Unix epoch format) indicating when the item should be considered expired and eligible for deletion. DynamoDB automatically deletes these items without consuming write throughput or incurring storage charges for the expired items themselves.
Enabling TTL in CloudFormation
To enable TTL for a DynamoDB table in a CloudFormation template, you will need to specify the TimeToLiveSpecification property within the AWS::DynamoDB::Table resource. Below is a step-by-step guide and example on how to implement this.
CloudFormation Template Example
Here's an exemplary CloudFormation YAML template snippet for creating a DynamoDB table with TTL enabled:
Key Components
- TableName: This is the unique name assigned to the DynamoDB table.
- AttributeDefinitions: Defines the attributes used for the primary key.
- KeySchema: Specifies the primary key schema for the table.
- BillingMode: In this example, we use
PAY_PER_REQUEST, which fits well for unpredictable workloads. Alternatively,PROVISIONEDmode can be defined if predictable throughput rates are preferred. - TimeToLiveSpecification: This crucial component contains two parts:
- AttributeName: The name of the attribute holding the TTL timestamp.
- Enabled: A boolean to indicate TTL is active for the table.
Important Considerations
- Attribute Definition: The TTL attribute must exist in the items before enabling TTL. If the attribute is absent or the value is set to null, the item is considered non-expired.
- TTL Processing: Item deletions are usually performed within 48 hours after expiration. Still, DynamoDB does not guarantee that items will be deleted immediately after they expire.
- Unavailable Feature: TTL cannot be disabled once enabled. To change the TTL attribute, you would need to create a new table and migrate data.
- Estimate Storage and Throughput: Understand the potential impact of TTL on your use case, especially regarding storage cost reduction and data access patterns.
Advantages of Using TTL
- Cost Management: Automatically delete unnecessary data, reducing overall storage costs.
- Data Lifecycle Management: Enforce data retention policies without manual intervention.
- Optimized Performance: Maintain optimal database performance by discarding stale and infrequently accessed data.
Use Cases
- Session Management: Automatically expire session tokens or state information after a predetermined period.
- Audit Logs: Retain logs for a specified duration before they are deleted, ensuring compliance with data governance policies.
- Cache Implementations: Manage cache invalidation effectively by setting custom expiration times on cached items.
Summary Table
| Feature | Description |
| Time to Live (TTL) | Automatically delete expired items from DynamoDB |
| TTL Attribute Name | Must specify an attribute to store expiration timestamp |
| TTL Enablement | Once enabled, cannot be disabled |
| Deletion Time | Items usually deleted within 48 hours post expiration |
| Billing Mode | PAY_PER_REQUEST or PROVISIONED available |
| Use Cases | Session management, audit logs, cache invalidation |
By configuring TTL in DynamoDB through CloudFormation, developers can leverage automated lifecycle management, streamline operations, and achieve scalable architecture effortlessly. Overall, this capability underscores the robust, flexible nature of AWS CloudFormation in orchestrating cloud resources efficiently and effectively.

