Manage EventBridge Schedules using Step Functions
Tags: aws, serverless, showdev, eventdriven
Published: 2023-02-24 11:52:14

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](https://docs.aws.amazon.com/scheduler/latest/UserGuide/what-is-scheduler.html) as well. [One time schedule](https://docs.aws.amazon.com/scheduler/latest/UserGuide/schedule-types.html#one-time) 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xwegdhtuj4r6zm9ze7jo.png)

## 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
}
```

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](https://medium.com/r/?url=https%3A%2F%2Faws.amazon.com%2Fblogs%2Fcompute%2Fintroducing-amazon-eventbridge-scheduler)
2. [Amazon EventBridge Scheduler Docs](https://docs.aws.amazon.com/scheduler/latest/UserGuide/what-is-scheduler.html)
3. [Amazon EventBridge One Time Schedules](https://docs.aws.amazon.com/scheduler/latest/UserGuide/schedule-types.html#one-time)

_** Cover Photo by [Lucian Alexe on Unsplash](https://unsplash.com/photos/f2xfTOv0p9Y?utm_source=unsplash&utm_medium=referral&utm_content=creditShareLink)_