Manage EventBridge Schedules using Step Functions

pubudusj

Pubudu Jayawardana

Posted on February 24, 2023

Manage EventBridge Schedules using Step Functions

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

State Machine

How it works

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

  2. This Schedule has a target lambda function.

  3. 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.

  4. At the set time, the Scheduler will trigger the lambda function.

  5. Within the Lambda function, based on Success or error in the business logic, task success or task failed Step Functions API can be called.

  6. 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.

  7. In the next step of Step Functions execution, the schedule will be deleted.

Few limitations

  1. Since callback pattern is only available for Step Functions standard flows, this solution works only within a standard flow.

  2. With this, you can only schedule only up to one year ahead. Because the maximum duration a standard flow can run is one year.

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

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

  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",
  "error": 0
}
Enter fullscreen mode Exit fullscreen mode

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.

Links

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

** Cover Photo by Lucian Alexe on Unsplash

💖 💪 🙅 🚩
pubudusj
Pubudu Jayawardana

Posted on February 24, 2023

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related