Skip to content

How to list out Event bridge schedules programatically - boto3

0

Hi, I have schedules in EventBridge and not rules. I have to change a step-function for all the schedules at once but i have not be successful to list the schedules, can you please suggest

asked a year ago670 views
3 Answers
1

Thank you, that worked. I am looking to modify the schedule, and this is perfect. Thanks

answered a year ago
EXPERT
reviewed a year ago
0

Hello.

If you use the EventBridge scheduler, you can retrieve it in a list using the "list_schedules()" API.
https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/scheduler/client/list_schedules.html

EXPERT
answered a year ago
EXPERT
reviewed a year ago
EXPERT
reviewed a year ago
  • If you just want to display a list in json, you can get it with the code below.

    import boto3
    
    client = boto3.client('scheduler')
    
    response = client.list_schedules()
    
    print(response)
    
-1
Accepted Answer

Use the following Python script to list all EventBridge schedules. This script initializes a Boto3 client for EventBridge Scheduler and uses the list_schedules method to fetch the schedules.

import boto3

def list_eventbridge_schedules(region_name):
    # Initialize the boto3 client for EventBridge Scheduler
    client = boto3.client('scheduler', region_name=region_name)
    
    # List all schedules
    schedules = []
    response = client.list_schedules()
    schedules.extend(response['Schedules'])
    
    # Handle pagination if there are more schedules
    while 'NextToken' in response:
        response = client.list_schedules(NextToken=response['NextToken'])
        schedules.extend(response['Schedules'])
    
    return schedules

if __name__ == "__main__":
    region = "us-east-1"  # Replace with your AWS region
    schedules = list_eventbridge_schedules(region)
    
    # Print out the schedules
    for schedule in schedules:
        print(schedule)

Explanation

1. Initialize Boto3 Client:

client = boto3.client('scheduler', region_name=region_name): Initializes a Boto3 client for EventBridge Scheduler.

2. List Schedules:

client.list_schedules(): Retrieves a list of schedules. If there are more schedules than can be returned in a single response, the NextToken key is used for pagination.

The script handles pagination by checking for the NextToken key and making additional requests to retrieve all schedules.

3. Print Schedules:

The script iterates over the retrieved schedules and prints each one.

Additional Considerations

  • Filtering Schedules: If you need to filter schedules based on specific criteria (e.g., by name or state), you can add additional logic to the script to handle this.
  • Modifying Step Functions: Once you have the list of schedules, you can add logic to modify the associated Step Functions as required.
EXPERT
answered a year ago
EXPERT
reviewed a year ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.