Scheduled EventBridge rule targeting API Destination
Asked Answered
V

3

8

Is it possible to create an EventBrisge rule which can be scheduled to run at a certain time of day and call an API as a custom target?

It seems as though a schedule can be setup for targets if they are AWS or Partner services, but not if they are custom configured endpoints.

All I want to do is setup a daily routine to call an endpoint so it can perform a daily cleanup task. If there is a better way, can someone suggest it, please?

EDIT

Under EventBridge I have created a Connection which points at the OAuth endpoint and then an API Destination which points at the API endpoint I want to invoke on a daily basis.

I have then created an event bus and a rule but when I try to set the Schedule option on the rule it shows a warning which states:

Schedule rule is not supported when custom or partner event bus is selected

I believe this means that I cannot invoke my own API endpoint with EventBridge without going through a Lambda. Am I wrong?

Vernitavernoleninsk answered 17/11, 2022 at 17:43 Comment(4)
Can you explain why EventBridge API Destinations don't work for you? docs.aws.amazon.com/eventbridge/latest/userguide/… Are you saying that when you create a rule that runs on a schedule docs.aws.amazon.com/eventbridge/latest/userguide/… that you are not allowed to select an API Destination target for that rule?Heidt
@mark-b I have updated the post with details of what I've created. Yes, I believe that I cannot create a rule which runs on a schedule to call my own custom endpoint.Vernitavernoleninsk
The error is because you have created a custom event bus. If you were using the default event bus I believe you would be able to do this. With a custom event bus you are correct that you will need to add a Lambda function.Heidt
If I select Rules > Create Rule > Name: somename > Event bus: default > Schedule > Next > Date and time: Now+1hr > Flexible time window: 15 > Next I end up on the Select target page where I do not have the option to choose my API Destination. So I can't cause my API to be triggered on a schedule.Vernitavernoleninsk
H
17

Create your scheduled rule on the *default bus* using the Create Rule interface. It appears you are right that the brand-new EventBridge Scheduler console interface does not yet (?) support API Destinations.

event-bridge-create-rule

The "create rule" scheduling supports cron-type use cases like yours. EventBridge Scheduler adds new functionality like ad-hoc scheduling of one-off events, flexible time windows, and TZ-aware scheduling.

Humanitarianism answered 21/11, 2022 at 8:23 Comment(5)
I actually discovered this yesterday, but as I can't remove the bounty I figured I wouldn't post my own answer. It was very irritating to discover that there are two paths through the Create Rule wizard but that only one of them lets you attach an API Destination.Vernitavernoleninsk
Hey there, does any of you two knows if we can trigger API destinations using EventBridge scheduler? I could not do it from the console nor from code. Do you have any ideas? Thanks!Emersion
I dug through the new EventBridge Scheduler options today and couldn't find a way to invoke an API destination. Crazy.Backstay
same status as on 27/7/23. AWS - note!Emelda
still not suported today. sadGimp
B
0

As of the date of my post you are able to get EventBridge Scheduler to send ETA tasks (or cron, etc) to API Destinations. You simply need to invoke EventBridge itself when you are choosing a destination AWS service! It will prompt you for things like the bus, and other details needed.

I found it easiest to make one schedule in the Console at first, and then retrieve it using your SDK of choice so you can see what options were sent to the API. You can then reverse-engineer all of the settings so you can call "create_schedule" yourself. https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/scheduler/client/get_schedule.html https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/scheduler/client/create_schedule.html

Buller answered 15/7 at 19:36 Comment(0)
C
0

Yes! The accepted solution still works, but I found it very hard to debug. I was getting invocation failures and getting the logs for those failures was quite painful. Also, creating a rule that has a schedule on it is kind of a weird mental model. I would expect rules to act more like filters/multiplexers along an event bus. And that mental model is what I ended up using to get the following solution working:

In EventBridge, create a new schedule that calls "PutEvent":

PutEvent

Then, create whatever json you want to send down the pipe to identify your event. I used "event_name" in my implementation - it gets added to a "details" object, so I don't think you need to worry about name collision.

custom json

Now you have an event generator that will fire your specified json down the bus according to the schedule that you configured.

In order to do something with that event, you need to create a rule that executes when it sees the json. Create a new rule that uses "Rule with an event pattern":

Rule with an event pattern

Now you need to create a custom pattern. The filtering API docs cover more complex filtering use cases if you'd like to use them. To test your pattern, you need to match according to the json that you specified in your schedule creation. If you want to log events to see the exact json that's being sent over your bus, you will want to set up a different rule to forward events to Cloudwatch. But at the time of this writing, the format of the custom events is as follows:

{
    "version": "<probably 0>",
    "id": "<guid>",
    "detail-type": "<whatever you configured in your schedule>",
    "source": "<the source that you specified in your schedule>",
    "account": "<your_account_id>",
    "time": "<timestamp>",
    "region": "us-east-1",
    "resources": [],
    "detail": {
        <the json that your schedule sends>
    }
}

Then associate your target to your API destination:

target associated to api destination

And now your API calls should be scheduled!

Crawly answered 31/8 at 22:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.