DEV Community

Cover image for Manage EventBridge Schedules using Step Functions (Part 2)- with Wait State
Pubudu Jayawardana for AWS Community Builders

Posted on • Originally published at Medium

Manage EventBridge Schedules using Step Functions (Part 2)- with Wait State

Last week I composed an article with a sample project on how to manage one time schedules created by Amazon EventBridge Scheduler using StepFunctions.

There I use StepFunctions callback pattern to wait for the schedule to run and then delete the schedule.

In this post I will explain another way to create the schedule and delete it after the given time without the callback pattern, but using a wait state in the execution.

Step Functions State Machine

State Machine

How it works

  1. On the Step Functions execution, it first creates a one time schedule using EventBridge Scheduler SDK integration.

  2. Step Function execution input must be in the below format:

    {
        "scheduleDate": "YYYY-MM-DD",
        "scheduleTime": "hh:mm:ss",
        "flexibleTimeWindow": 5
    }
    
  3. In the next step, the wait time is calculated using a Lambda Function. Here, based on the schedules date and time and the flexibleTimeWindow of the schedule, the wait time is calculated and output as ISO date time format.

  4. Next step is the 'Wait' state where it waits until the wait time returns for the last step.

  5. After the wait time, the execution continues and in the last step, the schedule will be deleted.

Limitations

  1. Schedule can only be within 1 year ahead because the maximum duration a standard flow can run is 1 year.

  2. Unlike the solution that uses callback, Step Function execution will not get to know if the task that was triggered by the schedule is successful or not.

  3. Technically for this scenario, you can use Express workflows. However practically it is not suitable, since the maximum execution time is only 5 minutes.

Improvements

  1. Currently Step Functions intrinsic functions doesn't support any date/time operations. When that's available in future, calculating the wait time can be done without the Lambda Function.

  2. Once EventBridge team release the feature to auto-delete schedules after they are triggered, this whole scenario will be invalidated.

Want to test yourself?

I have created a sample project for you to test this scenario in your AWS account. You need AWS CLI, SAM CLI and GIT installed in your machine.

Below are the deployment details.

  1. Clone the repository: https://github.com/pubudusj/manage-eb-schedules-with-stepfunctions-wait

  2. Go into the directory manage-eb-schedules-with-stepfunctions-wait

  3. Install dependencies with sam build

  4. Deploy the stack with sam deploy -g

  5. Once the stack is deployed successfully, you can start a Step Functions execution with below payload format:

    {
        "scheduleDate": "YYYY-MM-DD",
        "scheduleTime": "hh:mm:ss",
        "flexibleTimeWindow": 5
    }
    

    You can see the schedule is created and the execution waits until the schedule time + the flexible time window and deletes the schedule.

  6. To delete the stack, use: sam delete

Summary

In this use case, you can create one time schedules in EventBridge Scheduler using Step Functions SDK integrations. And wait in the same execution until it runs. Then delete it once it is completed.

Links

  1. Introducing Amazon EventBridge Scheduler
  2. Amazon EventBridge Scheduler Docs
  3. Amazon EventBridge One Time Schedules

Cover Photo by Jon Tyson on Unsplash

Top comments (0)