Manage EventBridge Schedules using Step Functions (Part 2)- with Wait State
Table of Contents
Last week I composed an article with a sample project on how to manage one time schedules created by Amazon EventBridge Scheduler using StepFunctions.
https://pubudu.dev/posts/manage-eventbridge-schedules-using-step-functions
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 #
How it works #
-
On the Step Functions execution, it first creates a one time schedule using EventBridge Scheduler SDK integration.
-
Step Function execution input must be in the below format:
{ "scheduleDate": "YYYY-MM-DD", "scheduleTime": "hh:mm:ss", "flexibleTimeWindow": 5 }
-
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. -
Next step is the ‘Wait’ state where it waits until the wait time returns for the last step.
-
After the wait time, the execution continues and in the last step, the schedule will be deleted.
Limitations #
-
Schedule can only be within 1 year ahead because the maximum duration a standard flow can run is 1 year.
-
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.
-
Technically for this scenario, you can use Express workflows. However practically it is not suitable, since the maximum execution time is only 5 minutes.
Possible improvements #
-
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.
-
Once EventBridge team release the feature to auto-delete schedules after they are triggered, this whole scenario will be invalidated.
Try this 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.
-
Clone the repository: https://github.com/pubudusj/manage-eb-schedules-with-stepfunctions-wait
-
Go into the directory
manage-eb-schedules-with-stepfunctions-wait
-
Install dependencies with
sam build
-
Deploy the stack with
sam deploy -g
-
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.
-
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.