5

I have the following Pyhton3/Boto3 script that connects to a AWS DynamoDB table and attempts to set a 19-day TTL on all its records by looping through them:

#!/usr/bin/env python3
import boto3
import sys
from datetime import datetime, timedelta
from boto3.dynamodb.conditions import Key, Attr

client = boto3.resource('dynamodb')

ttl = 19    # The TTL in number of DAYS

myTable = client.Table('MyTable')

pe = "logId, id, created"
delCount = 0

try:
    response = integrationLogTable.scan(
        ProjectionExpression=pe,
        )
    while 'LastEvaluatedKey' in response:
        response = integrationLogTable.scan(
            ProjectionExpression=pe,
            ExclusiveStartKey=response['LastEvaluatedKey']
            )

        for i in response['Items']:
            # ???

except ClientError as e:
    print(e.response['Error']['Message'])

I'm struggling with how to actually add the 19-day TTL to all my records...any ideas?

hotmeatballsoup
  • 385
  • 6
  • 58
  • 136

1 Answers1

4

Here is the process for adding TTL to a DynamoDBTable,

  1. Add TTL to the dynamodb table
  2. Add the attribute to each record and save it.

Attribute format:

Add the TTL in seconds.

For 19 days, it is 192460*60 => 19 days / 24 hours/ 60 minutes/ 60 seconds.

1641600 seconds

import time
import sys

if sys.version_info < (3, 0):
    object.ttlattribute = 1641600 + long(time.time())
else:
    # In py3, long is renamed to int
    object.ttlattribute = 1641600 + int(time.time())

Hope it helps. Check out this link on how to perform all operations on DynamoDB using boto.

Alex Waygood
  • 6,304
  • 3
  • 24
  • 46
Kannaiyan
  • 12,554
  • 3
  • 44
  • 83
  • Thanks @Kannaiyan (+1) - but is `ttlattribute` the actual name for the TTL attribute, or were you just giving that as an example? If that's the *actual* name, it sounds like (if you follow me current code sample) I need to just set `i.ttlattribute = 1641600 + long(time.time())` from inside that for-loop, right? Thanks again! – hotmeatballsoup Jul 25 '18 at 14:10
  • And then, I'm wondering, how do I actually go about *saving* that back up to DynamoDB? – hotmeatballsoup Jul 25 '18 at 14:16
  • Thanks for the edit @Kannaiyan, can I just ask one last followup for confirmation? I'm confused as to whether: **(a)** I'm supposed to set up the *name* of the TTL myself on the table (from inside the DynamoDB console UI) and then simply use that name in my code before saving it back to AWS, or **(b)** whether `ttlattribute` is the actual name of the TTL attribute that DynamoDB supports for all of its tables. With the former I might be inclined to call my attribute someting like `dataExpiry` instead of `ttlattribute`, is this possible? Or do I need to use `ttlattribute`? Thanks again! – hotmeatballsoup Jul 25 '18 at 14:57
  • 2
    You need to create the attribute in your document. There is no default attributename for it. It is configured when you enable ttl for that table. Click on Enable TTL for that table in the AWS Console, it will ask you for the attribute name. – Kannaiyan Jul 25 '18 at 15:37
  • FYI, it seems that the ttl attribute should be a string. – lolu Feb 01 '21 at 11:29
  • https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/time-to-live-ttl-before-you-start.html#time-to-live-ttl-before-you-start-formatting Says it should be a timestamp or integer. – Kannaiyan Feb 08 '21 at 01:18