AWS DynamoDB TTL – Setting up the right way

AWS DynamoDB offers a feature called Time to Live, when enabled on table in Amazon DynamoDB, a background job checks the TTL attribute of items to determine whether they are expired

AWS DynamoDB TTL – This is easy to setup except that there is a catch with data type declaration before saving (putItem) into table . as per amazon documentation this ttl field is a Numeric value however this is going to fail you send straight numeric value .

Sample terraform code for creating AWS DynamoDB TTL

resource "aws_dynamodb_table" "email-dynamodb-table" {
  name           = "SESNotifications"
  billing_mode   = "PROVISIONED"
  read_capacity  = 5
  write_capacity = 5
  hash_key       = "SESMessageId"
  range_key      = "SnsPublishTime"
  point_in_time_recovery {
    enabled = local.enable_point_in_time_recovery

  ttl {
    attribute_name = "ttl"
    enabled        = true

  attribute {
    name = "SESMessageId"
    type = "S"

  attribute {
    name = "SnsPublishTime"
    type = "S"

  tags = {
    "Name"            = "App-Name"

By default Amazon creates ttl attribute with Number as data type.

Lambda code used to put item in the DynamoDB – complete lambda code is available here on amazon documentation but without the ttl part.

//calculate ttl time 
var date = new Date()
//set ttl to 6 months from current date. records will expire after 6 months. 
var ttl_date = new Date(date.setMonth(date.getMonth()+6));
var ttl_seconds = Math.floor(ttl_date.getTime() / 1000)

//refer to the above link to understand code completely , add ttl to the var itemParams = {} as an additional attribute. 
ttl: {N: ttl_seconds}

This Lambda code is going to fail with below error, even though we are passing Numerical value as expected.

“InvalidParameterType: Expected params.Item[‘ttl’].N to be a string”

What is wrong , why is it failing ? it took a a while for me to figure out that DynamoDB expects that you convert ttl into toString and then pass it as shown below.
We are still sending the data type as Number but DynamoDb expects you to send the data across the network as String for compatibility reasons and internally DynamoDB treats this attribute as Number , this is the case whenever you are dealing with Numbers.

var ttl_seconds = Math.floor(ttl_date.getTime() / 1000).toString()

For testing purposes you can can use use 10 minutes as ttl value from your nodejs lambda as follows

var ttl_date = new Date(date.setMinutes(date.getMinutes()+10));

AWS DynamoDB ttl value screenshot after saving the data in table.


You can also refer to this stackoverflow thread for more information

Also refer to this article on how to setup AWS Transfer for SFTP


Leave a Reply

Your email address will not be published. Required fields are marked *