How to automatically delete rows from Dynamo DB through AWS Cloud formation template?

Let’s say you have built an app which uses AWS Dynamo DB as database.

And you have a use case where you want to delete the rows in the table after a certain time , let’s say 1 hour.

This can be done automatically!

You just need to configure “Time To Live” settings while creating the table.

You can either create the table manually using AWS Console or automate it using Cloud Formation Templates.

In this post we will see how to do it using Cloud Formation Templates.

STEP 1: Create Cloud Formation Template:

In Cloud Formation Template , while you create the specification for the table , you need to add the below properties under Resources ->DynamoDBTable -> Properties hierarchy:

TimeToLiveSpecification:
AttributeName: “ttl”
Enabled: true

Here Attribute Name is the field in the table which is going to store the time period for which the row should exist .
In the above case it is “ttl”.
This is the “time to live” for the specific entry.
Once this time lapses the particular row will be automatically deleted.
This field name need not be specified while you create the table.
Dynamo DB is a NoSQL database and you can dynamically add new fields while inserting rows into the table.

Here is a Cloud Formation template with “TimeToLiveSpecification” added.


Resources:
  DynamoDBTable:
    Type: "AWS::DynamoDB::Table"
    Properties:
      TableName: "MyDynamoDBTable"
      AttributeDefinitions:
        - AttributeName: "id"
          AttributeType: "S"
      KeySchema:
        - AttributeName: "id"
          KeyType: "HASH"
      ProvisionedThroughput:
        ReadCapacityUnits: 5
        WriteCapacityUnits: 5
      TimeToLiveSpecification:
        AttributeName: "ttl"
        Enabled: true

Once you deploy this cloud formation template , TTL will be automatically enabled for the table.

The next step is to insert a row with a value for “ttl” column.

STEP 2: Insert a row into the Dynamodb table with value for TTL:

Here is a sample code to insert a row with a value for TTL:

const AWS = require('aws-sdk');
const dynamodb = new AWS.DynamoDB();

exports.handler = async (event) => {
  const params = {
    TableName: 'MyDynamoDBTable',
    Item: {
      'id': { 'S': '123' },
      'name': { 'S': 'John Doe' },
      'ttl': { 'N': Math.floor(Date.now() / 1000) + 3600 } // TTL value for 1 hour
    }
  };
  
  try {
    const response = await dynamodb.putItem(params).promise();
    console.log(`Row inserted successfully: ${response}`);
  } catch (err) {
    console.error(`Error inserting row: ${err}`);
  }
  
  return {
    statusCode: 200,
    body: JSON.stringify({ message: 'Row inserted successfully' }),
  };
};

In this example, we’re using the AWS SDK for Node.js to create a new instance of the DynamoDB class and specify the name of the DynamoDB table we want to insert a row into (MyDynamoDBTable).

We then define the params object that includes the TableName and Item properties.

The Item property is an object that represents the row that we want to insert into the table. In this example, we’re inserting a row with an id of 123, a name of John Doe, and a ttl value that is set to the current timestamp plus 3600 seconds, which is equivalent to 1 hour.

Next, we use the putItem method of the DynamoDB class to insert the row into the table with the specified parameters.

We await the response, which includes the details of the row that was inserted.

If an error occurs, we log an error message to the console.

Finally, we return a response from the Lambda function to indicate that the row was inserted successfully.

Notice that the TTL should be a number and it should be specified in seconds .

The TTL time also follows unix epoch format.

For the above inserted entry you can check after 1 hour and the row would have been deleted from Dynamo DB table.

That’s it!


Posted

in

by

Comments

Leave a Reply

Discover more from The Full Stack Developer

Subscribe now to keep reading and get access to the full archive.

Continue reading