Automating Time-Based actions using EventBridge's scheduler or DynamoDB: TTL and DDB Stream
profile picture Akshay Sonwalkar
5 min read Nov 29, 2023

Automating Time-Based actions using EventBridge's scheduler or DynamoDB: TTL and DDB Stream

Automating Time-Based actions using EventBridge’s scheduler or DynamoDB: TTL and DDB Stream

📖 Use Case: Task Management System

In today’s fast-paced world, managing tasks efficiently is vital for both individuals and organizations. To address this, we aim to create a comprehensive Task Management System. This system will smoothly combine task scheduling and execution while harnessing the storage capacities of DynamoDB or the capabilities of EventBridge.

Our solution will empower users to effortlessly schedule and carry out actions for designated tasks at predetermined times. Through the utilization of DynamoDB table and its features like TTL, important task details like task names, descriptions, and scheduled timings will be stored securely. This user-friendly system aims to streamline task management and enhance productivity amidst busy schedules and multiple responsibilities.

📝 Solution 1: Using TTL and DDB Stream

  • TTL (Time To Live): TTL is a feature provided by DynamoDB that automatically deletes a record (item) from a table when a specified time is reached.
  • DDB Stream (DynamoDB Streams): DDB Stream is a feature of Amazon DynamoDB that captures and records a time-ordered sequence of modifications made to a DynamoDB table, enabling real-time reaction to changes in the table’s data.
  • By configuring TTL for a specific attribute, we can monitor the time (TTL attribute) and trigger lambda using the DDB Stream.
  • A Lambda function runs provided code on the deleted record sent by the DDB stream.
  • Additionally, we have the flexibility to update the record within the Lambda function and add it back to DynamoDB.

📝 Solution 2: EventBridge’s Scheduler

  • EventBridge’s Scheduler: EventBridge is a fully managed event bus service provided by AWS. It includes a scheduler feature that can trigger Lambda functions at specific times or intervals, such as every day at 12:00 AM or every 10 minutes.

😕 Both solutions have their disadvantages:

  • Solution 1 – TTL can take up to 48 hours to delete a record.
  • Solution 2 – It will run only at the specified time, e.g., if you set to run a lambda every 10 min then it will run every 10 min even though there is nothing to perform any action.

⏱️ Note: Although TTL can take up to 48 hours to delete a record, it typically only takes around 20 minutes. If your application is not time-sensitive, this solution is highly recommended.

✨ Key Points:

  • The value (time) of the TTL attribute should be provided in seconds as a Unix timestamp.
  • Deletion of records by TTL may take a maximum of 48 hours, but it usually occurs within 20 minutes.
  • TTL can delete items whose attribute values are not older than 5 years from the current date. Records older than that will not be deleted.

⚙️ Implementation Steps of Solution 1:

To implement this solution, follow these steps:

  1. Create a serverless.yml file to define the DynamoDB table with TTL enabled.
# serverless.yml
service: __serviceName__
provider: aws
	# Your Serverless function definitions go here.
    Type: AWS::DynamoDB::Table
      TableName: __TableName__ # Add your table name
      BillingMode: PAY_PER_REQUEST # Choose the appropriate billing mode
        - AttributeName: requestId
          AttributeType: "S"
        - AttributeName: requestId
          KeyType: HASH
        AttributeName: __TTLAttributeName__ # Specify the TTL attribute name for monitoring time
        Enabled: true # Set this to true
  1. Once you have configured the table, deploy the code.
  2. When a new record is added to the table with the TTLAttributeName and a corresponding value in seconds, the TTL feature continuously monitors the time. After the specified time elapses, the record is scheduled for deletion. However, it’s important to note that the actual deletion process may take up to 48 hours to complete.
    1. Based on my experience, it usually takes around 20 to 25 minutes for me.

🔄 Integration with Lambda for Operations:

To perform operations or trigger additional actions upon record deletion, you can confidently utilize DynamoDB Streams.

  1. Update your serverless.yml file with the following configuration:
# serverless.yml
service: __serviceName__
provider: aws
    handler: __Path to the function__
    description: TTL handler
      - stream:
          type: dynamodb
          arn: !GetAtt __TABLE_NAME.StreamArn
            - eventName: [REMOVE] # You can also use INSERT and MODIFY as well to trigger the Lambda, but TTL specifically removes records.
  1. With this setup, the Lambda function will be triggered whenever a record is deleted from DynamoDB by TTL. This means that you can perform a set of operations on the deleted record within the Lambda function.

🎉 Awesome! By using this solution, you can automate operations on your DynamoDB records based on specific time. Enjoy the seamless functionality and the power of TTL and DDB Stream!

⚙️ Implementation Steps of Solution 2:

To use EventBridge’s Scheduler for triggering operations:

  1. Configure the EventBridge rule to schedule the desired trigger time and frequency.
  2. Specify the Lambda function as the target for the EventBridge rule.
# serverless.yml
service: __serviceName__
provider: aws
    handler: __Path to the function__
    description: TTL handler
      - schedule: cron(0 12 * * ? *) # as per UTC time for specific time, event triggers every day at 12:00 PM (noon) in the server's local time. OR
	  - schedule: rate(10 minutes) # for every n minutes
  1. Define the necessary operations within the Lambda function that will be executed when the trigger occurs.

🎉 Fantastic! By leveraging EventBridge’s Scheduler, you can effortlessly trigger Lambda functions at specific times. Embrace the seamless functionality and unlock the power of automated scheduling using EventBridge’s Scheduler. Enjoy the convenience and efficiency it brings to your application!

I hope you found this explanation clear and helpful. If you have any further questions, feel free to ask! 😊

Application Modernization Icon

Innovate faster, and go farther with serverless-native application development. Explore limitless possibilities with AntStack's serverless solutions. Empowering your business to achieve your most audacious goals.

Build with us


Your Digital Journey deserves a great story.

Build one with us.

Recommended Blogs

Cookies Icon

These cookies are used to collect information about how you interact with this website and allow us to remember you. We use this information in order to improve and customize your browsing experience and for analytics and metrics about our visitors on this website.

If you decline, your information won’t be tracked when you visit this website. A single cookie will be used in your browser to remember your preference not to be tracked.

Build With Us