I'd like to enable TTL for my newly-created-table via CloudFormation. I've tried the following to no avail:
{
"Resources" : {
"mytable" : {
"Type" : "AWS::DynamoDB::Table",
"Properties" : {
"TableName" : "my_table",
"ProvisionedThroughput" : {"ReadCapacityUnits" : 1, "WriteCapacityUnits" : 5},
"KeySchema" :
[
{"AttributeName" : "user_email", "KeyType" : "HASH"},
{"AttributeName" : "datetime", "KeyType" : "RANGE"}
],
"AttributeDefinitions": [
{"AttributeName" : "user_email", "AttributeType" : "S"},
{"AttributeName" : "datetime", "AttributeType" : "S"}
],
"TimeToLiveDescription": {
"AttributeName": "expire_at",
"TimeToLiveStatus": "ENABLED"
}
}
}
}
I used the TimeToLiveDescription, which I obtained from this doc.
Attempting to create the stack gave me the following error:
Encountered unsupported property TimeToLiveDescription
The DynamoDB TTL-support for CloudFormation exists now. See:
https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-dynamodb-timetolivespecification.html
Example:
{
"TableName": "MyTable",
"AttributeDefinitions": [
{
"AttributeName": "Uid",
"AttributeType": "S"
}
],
"KeySchema": [
{
"AttributeName": "Uid",
"KeyType": "HASH"
}
],
"ProvisionedThroughput": {
"ReadCapacityUnits": "1",
"WriteCapacityUnits": "1"
},
"TimeToLiveSpecification": {
"AttributeName": "TimeToLive",
"Enabled": "TRUE"
}
}
For the sake of completeness, following is an example CloudFromation YAML that creates a Table with TTL enabled
AWSTemplateFormatVersion: '2010-09-09'
Description: The database for my Service
Resources:
BatchDataTable:
Type: AWS::DynamoDB::Table
Properties:
TableName: "MyDynamoTable"
BillingMode: PAY_PER_REQUEST
AttributeDefinitions:
- AttributeName: Id
AttributeType: S
- AttributeName: SortKey
AttributeType: S
KeySchema:
- AttributeName: Id
KeyType: "HASH"
- AttributeName: SortKey
KeyType: "RANGE"
TimeToLiveSpecification:
AttributeName: TimeToLive
Enabled: true
Now if you add an item to this tabe with an Attribute named "TimeToLive" and have it's value set to the Unix Epoch at which the item should expire, DynamoDB will clear the item from that table when the TTL is reached.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With