Skip to main content
Manage EventBridge Schedules using Step Functions

Manage EventBridge Schedules using Step Functions

·3 mins· loading ·
AWS Serverless EventBridge Scheduler Step Functions SAM
Pubudu Jayawardana
Author
Pubudu Jayawardana
Cloud Engineer, AWS Community Builder

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
#

Image: State machine
Image: 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.

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.

  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
}

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.

Useful Links #

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

Related

AWS Step Functions — Are Active Executions Affected When Your State Machine Gets Updated?
·4 mins· loading
AWS Lambda Serverless Step Functions CDK
Looking into different scenario of SF executions vs state machine updates
Simple Leave Management System with AWS Serverless
·5 mins· loading
AWS Lambda Serverless Step Functions DynamoDB SES
This is how I built a simple leave management system using AWS Serverless services
Split messages from single SQS queue into Multiple SQS queues using EventBridge
·4 mins· loading
AWS Serverless EventBridge SQS CDK
This blog post shows how we can split messages in a SQS queue into mulitple queues usint Amazon EventBride