経緯
QiitaやclassmethodでHitする記事だと serverless-dynamodb-ttl
pluginを使うように紹介されているが、cloud formationに同一の機能が実装されたことによって現在このレポジトリはアーカイブされている。
AWS CloudFormation supports now TTL, so this plugin for Serverless is useless. More information about how to use it in the AWS documentation.
repository
https://github.com/Jimdo/serverless-dynamodb-ttl
日本語の記事
https://dev.classmethod.jp/cloud/aws/enabling-dynamodb-ttl-with-serverless-framework-or-aws-sam/
https://qiita.com/sayama0402/items/28fe9b51b17c2bd66324
Cloud formationで書こう
tableの定義部分に以下のように記載すればOK
TimeToLiveSpecification:
AttributeName: attr_name
Enabled: true
全体の定義は以下の感じで書けばOK.
userTable:
Type: AWS::DynamoDB::Table
Properties:
TableName: table_user
AttributeDefinitions:
- AttributeName: table_category
AttributeType: S
- AttributeName: id
AttributeType: S
- AttributeName: expired_at_unix_time
AttributeType: N
KeySchema:
- AttributeName: table_category
KeyType: HASH
- AttributeName: id
KeyType: RANGE
ProvisionedThroughput:
ReadCapacityUnits: 1
WriteCapacityUnits: 1
TimeToLiveSpecification:
AttributeName: expired_at_unix_time
Enabled: true
結論
pluginじゃなくて公式のcloud formation使おう。