Enable DynamoDB expiration time (TTL) using CFN

Enable DynamoDB expiration time (TTL) using CloudFormation

One of the features of DynamoDB is TTL.

Amazon DynamoDB Time to Live (TTL) allows you to define a per-item timestamp to determine when an item is no longer needed. Shortly after the date and time of the specified timestamp, DynamoDB deletes the item from your table without consuming any write throughput.

Expiring items by using DynamoDB Time to Live (TTL)

The specifics of TTL are covered in the following official AWS page.


The purpose of this page is to reproduce the above page using CloudFormation.
Specifically, we will create a DynamoDB table with TTL enabled, and confirm that when an item expires, it is automatically deleted.


Diagram of enabling DynamoDB Expiration Time (TTL) using CloudFormation.

Create a DynamoDB table.
In addition to the usual settings for partition keys, sort keys, etc., configure the table with settings related to TTL activation.

Create a Lambda function.
The function’s action is to create the sample item data and store it in the DynamoDB table.
The runtime environment for the function is Python 3.8.

CloudFormation template files

The above configuration is built with CloudFormation.
The CloudFormation templates are located at the following URL


Explanation of key points of the template files


    Type: AWS::DynamoDB::Table
        - AttributeName: UserName
          AttributeType: S
        - AttributeName: SessionId
          AttributeType: S
      BillingMode: PAY_PER_REQUEST
        - AttributeName: UserName
          KeyType: HASH
        - AttributeName: SessionId
          KeyType: RANGE
      TableClass: STANDARD
      TableName: !Ref Prefix
        AttributeName: TTL
        Enabled: true
Code language: YAML (yaml)

For basic information on DynamoDB, please refer to the following page

Introduction to DynamoDB – Building DB for Review Data 【Building Simple DB for Review Data with DynamoDB】 This is one of the AWS DVA topics related to development with AWS services.As an introduction to DynamoD...

Set “UserName” as the partition key and “SessionId” as the sort key.

Settings regarding TTL are made in the TimeToLiveSpecification property.
Specify the attribute related to the TTL expiration date with the AttributeName property.
In this case, specify the attribute named “TTL”.

Lambda Function

    Type: AWS::Lambda::Function
          TABLE_NAME: !Ref Table
        ZipFile: |
          import boto3
          import datetime
          import json
          import os
          import random
          import string
          import time

          TABLE_NAME = os.environ['TABLE_NAME']

          dynamodb_client = boto3.client('dynamodb')

          def lambda_handler(event, context):
            now = datetime.datetime.now()

            for i in range(10):
              username = 'user' + str(i)
              sessionid = ''.join(random.choices(string.ascii_letters + string.digits, k=10))

              ttl = now + datetime.timedelta(minutes=i+5)
              ttl_str = ttl.strftime('%Y-%m-%d %H:%M:%S.%f')
              ttl_unix = str(time.mktime(ttl.timetuple()))

              item = {
                'UserName': {'S': username},
                'SessionId': {'S': sessionid},
                'TTL_str': {'S': ttl_str},
                'TTL': {'N': ttl_unix}

              dynamodb_response = dynamodb_client.put_item(
      FunctionName: !Sub "${Prefix}-function"
      Handler: !Ref Handler
      Runtime: !Ref Runtime
      Role: !GetAtt FunctionRole.Arn
      Timeout: !Ref Timeout
Code language: YAML (yaml)

The code to be executed by the Lambda function in inline format.
For more information, please see the following page

3 parterns to create Lambda with CloudFormation (S3/Inline/Container) 【Creating Lambda with CloudFormation】 When creating a Lambda with CloudFormation, there are three main patterns as follows. Uploading the code to an S3 buc...

After creating a client object for DynamoDB, use the put_item method to store items in a table.
In this case, we will create and save 10 sample data items.
The expiration time to be set for each item is based on the time when the function is executed, and is increased by one minute.

The key point is “TTL,” which is the item to be saved.
This value is used as the expiration date for this item.

The value to be used as the TTL is explained in the AWS Official as follows

The TTL attribute’s value must be a top-level Number data type.

The TTL attribute’s value must be a timestamp in Unix epoch time format in seconds.

Using DynamoDB Time to Live (TTL)

Calculate UNIX time from date/time data according to the above and specify it in “TTL”.

Confirm the IAM role for the Lambda function.

    Type: AWS::IAM::Role
        Version: 2012-10-17
          - Effect: Allow
            Action: sts:AssumeRole
                - lambda.amazonaws.com
        - arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole
        - PolicyName: !Sub "${Prefix}-DynamodbPutItemPolicy"
            Version: 2012-10-17
              - Effect: Allow
                  - dynamodb:PutItem
                  - !Sub "arn:aws:dynamodb:${AWS::Region}:${AWS::AccountId}:table/${Table}"
Code language: YAML (yaml)

Authorization to store items against a DynamoDB table.


Use CloudFormation to build this environment and check the actual behavior.

Create CloudFormation stacks and check resources in stacks

Create a CloudFormation stacks.
For information on how to create stacks and check each stack, please refer to the following page

CloudFormation’s nested stack 【How to build an environment with a nested CloudFormation stack】 Examine nested stacks in CloudFormation. CloudFormation allows you to nest stacks. Nested ...

After checking the resources in each stack, information on the main resources created this time is as follows

  • DynamoDB table: fa-107
  • Lambda function: fa-107-function

Check each resource from the AWS Management Console.

Check DynamoDB.

Detail of DynamoDB 1.

The TTL is indeed enabled.

Next, check the Lambda function.

Detail of Lambda 1.

This function is also successfully created.
When this function is invoked, the sample data is saved in the DynamoDB table.

Checking Action

Now that everything is ready, invoke the Lambda function.

Detail of Lambda 2.

The Lambda function has been successfully executed.

The function execution log is also checked.

Detail of Lambda 3.

We can see that the data was indeed saved 10 times in the DynamoDB table.

Check the table.

Detail of DynamoDB 2.

It is true that 10 sample data are stored in the table.
Looking at the TTL (TTL) item, we see that the expiration time is set in UNIX time format.

Wait for a while until the expiration time of some items has passed.

Detail of DynamoDB 3.

The number of items has indeed decreased.
It means that the expired items have been deleted automatically.

Wait for a while longer.

Detail of DynamoDB 4.

All items have been deleted.
As you can see, by enabling TTL in DynamoDB and setting the expiration date and time for each item, you can automatically delete items.


We have confirmed that when you create a DynamoDB table with TTL enabled and an item expires, the item will be automatically deleted.