1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

DynamoDB Localを用いたTTL 動作確認方法 メモ

Posted at

DynamoDBのTTL機能をローカルで動作確認したため、メモしておく。

TTLとは

  • Time To Liveの略
  • DynamoDBの各データを自動で削除する機能
    • レコード毎にデータの有効期限を指定し、その有効期限を過ぎたデータが自動削除される

準備

DynamoDB Local + DynamoDB Admin準備

  • docker-compose.yml

    version: '3'
    
    services:
      dynamodb-local:
        container_name: dynamodb-local
        image: amazon/dynamodb-local:latest
        user: root
        command: -jar DynamoDBLocal.jar -sharedDb -dbPath /data
        volumes:
          - dynamodb-data:/data
        ports:
          - 8000:8000
        networks:
          - dynamodb-network
    
      dynamodb-admin:
        container_name: dynamodb-admin
        image: aaronshaf/dynamodb-admin:latest
        environment:
          - DYNAMO_ENDPOINT=dynamodb-local:8000
        ports:
          - 8001:8001
        depends_on:
          - dynamodb-local
        networks:
          - dynamodb-network
    
    volumes:
      dynamodb-data:
    
    networks:
      dynamodb-network:
        driver: bridge
    
  • 起動

    docker-compose up
    

テストテーブル作成

# テーブルtest_table作成
# AWS CLI実行用profileは各自の環境に合わせて設定すること。
aws dynamodb create-table --cli-input-json file://test_table_schema.json  --endpoint-url http://localhost:8000 --profile dynamodb
echo Creating Table "test_table" Succeeded
  • テーブルスキーマtest_table_schema.json

    {
      "AttributeDefinitions": [
        {
          "AttributeName": "id",
          "AttributeType": "S"
        }
      ],
      "TableName": "test_table",
      "KeySchema": [
        {
          "AttributeName": "id",
          "KeyType": "HASH"
        }
      ],
      "ProvisionedThroughput": {
        "ReadCapacityUnits": 5,
        "WriteCapacityUnits": 5
      }
    }
    

TTL設定

  • TTL属性ttlを設定
# TTL有効化
aws dynamodb update-time-to-live --table-name test_table --time-to-live-specification "Enabled=true, AttributeName=ttl" --endpoint-url http://localhost:8000 --profile dynamodb

動作確認

テストデータ登録

  • TTLを現在時刻+5分に設定し、2レコード登録
# テストデータ登録
# TTL設定(現在時刻+5分)
TTL=$(date +%s -d "5 minute")
echo TTL=${TTL}
# テストデータJSON
tempfile=$(mktemp)
cat  > ${tempfile} << EOS
{
    "test_table": [
        {
            "PutRequest": {
                "Item": {
                    "id": {
                        "S": "ABCDE12345"
                    },
                    "ttl": {
                        "N": "${TTL}"
                    }
                }
            }
        },
        {
            "PutRequest": {
                "Item": {
                    "id": {
                        "S": "FGHIJ67890"
                    },
                    "ttl": {
                        "N": "${TTL}"
                    }
                }
            }
        }
    ]
}
EOS
aws dynamodb batch-write-item --request-items file://${tempfile} --endpoint-url http://localhost:8000 --profile dynamodb
echo Putting Initial Data Succeeded
rm -f ${tempfile}
  • 登録結果

    dynamo_test_data.png

  • TTL経過後のテーブルデータ※削除されている

    dynamo_test_data_after.png

参考情報

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?