Manage EventBridge Schedules using Step Functions
Table of Contents
AWS Step functions recently announced SDK support for another 35 services.
Announcement: https://aws.amazon.com/about-aws/whats-new/2023/02/aws-step-functions-integration-35-services-emr-serverless
This includes the support for EventBridge Scheduler as well. One time schedule is one of the great features supported by EventBridge Scheduler. As the name implies, it allows you to schedule a task just to run once at a given time.
As at now ( February 2023) EventBridge Scheduler doesn’t have native functionality to know if the target task is successful or not. Also schedule will not be deleted itself automatically after the execution is completed at the given time.
In this blog I explain a simple use case where we can create one time schedules and later remove them after the execution using Step Functions using new SDK integrations.
Step Functions State Machine #
How it works #
-
On the Step Functions execution, it first creates a one time schedule using EventBridge Scheduler SDK integration.
-
This Schedule has a target lambda function.
-
The schedule creation step in the state machine is configured with the call back pattern. So it waits until Step Functions receives a Task Success or Failure signal.
-
At the set time, the Scheduler will trigger the lambda function.
-
Within the Lambda function, based on Success or error in the business logic, task success or task failed Step Functions API can be called.
-
Once Step Functions receives the callback it sends a message to a predefined SNS topic with the result. By subscribing to this SNS topic, you may continue on the business logic as to what to do based on the result.
-
In the next step of Step Functions execution, the schedule will be deleted.
Few limitations #
-
Since callback pattern is only available for Step Functions standard flows, this solution works only within a standard flow.
-
With this, you can only schedule only up to one year ahead. Because the maximum duration a standard flow can run is one year.
Try it 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
-
Go into the directory
manage-eb-schedules-with-stepfunctions
-
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",
"error": 0
}
To test the scheduled task failure scenario, set the error
value to 1
.
To delete the stack, use: sam delete
Summary #
In this use case, you can create one time schedules in EventBridge Scheduler and wait until it runs, then delete them once it is completed. All can be handled within a Step Functions execution as a low code solution.