-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Python:EventBridge Scheduler, follow #6999
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
ford-at-aws
merged 9 commits into
awsdocs:main
from
meyertst-aws:6957-python-follow-eventbridge-scheduler-workflow
Oct 24, 2024
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
28fe399
Start of scheduler workflow
meyertst-aws 4f484b1
implementation complete
meyertst-aws f1aebbd
tests working
meyertst-aws 14ceb50
Pretty much done
meyertst-aws c5e6d1a
Final stuff
meyertst-aws b244baa
Merge branch 'main' into 6957-python-follow-eventbridge-scheduler-wor…
meyertst-aws 9069ecb
changes for code review
meyertst-aws 4dbd175
Merge branch 'main' into 6957-python-follow-eventbridge-scheduler-wor…
meyertst-aws bfd81ca
Merge branch 'main' into 6957-python-follow-eventbridge-scheduler-wor…
meyertst-aws File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,129 @@ | ||
| # EventBridge Scheduler code examples for the SDK for Python | ||
|
|
||
| ## Overview | ||
|
|
||
| Shows how to use the AWS SDK for Python (Boto3) to work with Amazon EventBridge Scheduler. | ||
|
|
||
| <!--custom.overview.start--> | ||
| <!--custom.overview.end--> | ||
|
|
||
| _EventBridge Scheduler allows you to create, run, and manage tasks on a schedule from one central, managed service._ | ||
|
|
||
| ## ⚠ Important | ||
|
|
||
| * Running this code might result in charges to your AWS account. For more details, see [AWS Pricing](https://aws.amazon.com/pricing/) and [Free Tier](https://aws.amazon.com/free/). | ||
| * Running the tests might result in charges to your AWS account. | ||
| * We recommend that you grant your code least privilege. At most, grant only the minimum permissions required to perform the task. For more information, see [Grant least privilege](https://docs.aws.amazon.com/IAM/latest/UserGuide/best-practices.html#grant-least-privilege). | ||
| * This code is not tested in every AWS Region. For more information, see [AWS Regional Services](https://aws.amazon.com/about-aws/global-infrastructure/regional-product-services). | ||
|
|
||
| <!--custom.important.start--> | ||
| <!--custom.important.end--> | ||
|
|
||
| ## Code examples | ||
|
|
||
| ### Prerequisites | ||
|
|
||
| For prerequisites, see the [README](../../README.md#Prerequisites) in the `python` folder. | ||
|
|
||
| Install the packages required by these examples by running the following in a virtual environment: | ||
|
|
||
| ``` | ||
| python -m pip install -r requirements.txt | ||
| ``` | ||
|
|
||
| <!--custom.prerequisites.start--> | ||
| <!--custom.prerequisites.end--> | ||
|
|
||
| ### Get started | ||
|
|
||
| - [Hello EventBridge Scheduler](hello/hello_scheduler.py#L4) (`ListSchedules`) | ||
|
|
||
|
|
||
| ### Single actions | ||
|
|
||
| Code excerpts that show you how to call individual service functions. | ||
|
|
||
| - [CreateSchedule](scheduler_wrapper.py#L38) | ||
| - [CreateScheduleGroup](scheduler_wrapper.py#L131) | ||
| - [DeleteSchedule](scheduler_wrapper.py#L104) | ||
| - [DeleteScheduleGroup](scheduler_wrapper.py#L160) | ||
|
|
||
| ### Scenarios | ||
|
|
||
| Code examples that show you how to accomplish a specific task by calling multiple | ||
| functions within the same service. | ||
|
|
||
| - [Scheduled Events workflow](scenario/scheduler_scenario.py) | ||
|
|
||
|
|
||
| <!--custom.examples.start--> | ||
| <!--custom.examples.end--> | ||
|
|
||
| ## Run the examples | ||
|
|
||
| ### Instructions | ||
|
|
||
|
|
||
| <!--custom.instructions.start--> | ||
| <!--custom.instructions.end--> | ||
|
|
||
| #### Hello EventBridge Scheduler | ||
|
|
||
| This example shows you how to get started using EventBridge Scheduler. | ||
|
|
||
| ``` | ||
| python hello/hello_scheduler.py | ||
| ``` | ||
|
|
||
|
|
||
| #### Scheduled Events workflow | ||
|
|
||
| This example shows you how to do the following: | ||
|
|
||
| - Deploy a CloudFormation stack with required resources. | ||
| - Create a EventBridge Scheduler schedule group. | ||
| - Create a one-time EventBridge Scheduler schedule with a flexible time window. | ||
| - Create a recurring EventBridge Scheduler schedule with a specified rate. | ||
| - Delete EventBridge Scheduler the schedule and schedule group. | ||
| - Clean up resources and delete the stack. | ||
|
|
||
| <!--custom.scenario_prereqs.scheduler_ScheduledEventsWorkflow.start--> | ||
| <!--custom.scenario_prereqs.scheduler_ScheduledEventsWorkflow.end--> | ||
|
|
||
| Start the example by running the following at a command prompt: | ||
|
|
||
| ``` | ||
| python scenario/scheduler_scenario.py | ||
| ``` | ||
|
|
||
|
|
||
| <!--custom.scenarios.scheduler_ScheduledEventsWorkflow.start--> | ||
| <!--custom.scenarios.scheduler_ScheduledEventsWorkflow.end--> | ||
|
|
||
| ### Tests | ||
|
|
||
| ⚠ Running tests might result in charges to your AWS account. | ||
|
|
||
|
|
||
| To find instructions for running these tests, see the [README](../../README.md#Tests) | ||
| in the `python` folder. | ||
|
|
||
|
|
||
|
|
||
| <!--custom.tests.start--> | ||
| <!--custom.tests.end--> | ||
|
|
||
| ## Additional resources | ||
|
|
||
| - [EventBridge Scheduler User Guide](https://docs.aws.amazon.com/scheduler/latest/userguide/intro.html) | ||
| - [EventBridge Scheduler API Reference](https://docs.aws.amazon.com/scheduler/latest/apireference/Welcome.html) | ||
| - [SDK for Python EventBridge Scheduler reference](https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/scheduler.html) | ||
|
|
||
| <!--custom.resources.start--> | ||
| <!--custom.resources.end--> | ||
|
|
||
| --- | ||
|
|
||
| Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
|
|
||
| SPDX-License-Identifier: Apache-2.0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| # Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| # snippet-start:[python.example_code.scheduler.Hello] | ||
| import boto3 | ||
|
|
||
|
|
||
| def hello_scheduler(scheduler_client): | ||
| """ | ||
| Use the AWS SDK for Python (Boto3) to create an Amazon EventBridge Scheduler | ||
| client and list the schedules in your account. | ||
| This example uses the default settings specified in your shared credentials | ||
| and config files. | ||
|
|
||
| :param scheduler_client: A Boto3 Amazon EventBridge Scheduler Client object. This object wraps | ||
| the low-level Amazon EventBridge Scheduler service API. | ||
| """ | ||
| print("Hello, Amazon EventBridge Scheduler! Let's list some of your schedules:\n") | ||
| paginator = scheduler_client.get_paginator("list_schedules") | ||
| page_iterator = paginator.paginate(PaginationConfig={"MaxItems": 10}) | ||
|
|
||
| schedule_names: [str] = [] | ||
| for page in page_iterator: | ||
| for schedule in page["Schedules"]: | ||
| schedule_names.append(schedule["Name"]) | ||
|
|
||
| print(f"{len(schedule_names)} schedule(s) retrieved.") | ||
| for schedule_name in schedule_names: | ||
| print(f"\t{schedule_name}") | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| hello_scheduler(boto3.client("scheduler")) | ||
| # snippet-end:[python.example_code.scheduler.Hello] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| boto3>=1.35.38 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| boto3>=1.35.38 | ||
| pytest>=8.3.3 | ||
| botocore>=1.35.38 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| # Amazon EventBridge Scheduler Workflow | ||
|
|
||
| ## Overview | ||
| This example shows how to use AWS SDK for Python (Boto3) to work with Amazon EventBridge Scheduler with schedules and schedule groups. The workflow demonstrates how to create and delete one-time and recurring schedules within a schedule group to generate events on a specified target, such as an Amazon Simple Notification Service (Amazon SNS) Topic. | ||
|
|
||
| The target SNS topic and the AWS Identity and Access Management (IAM) role used with the schedules are created as part of an AWS CloudFormation stack that is deployed at the start of the workflow, and deleted when the workflow is complete. | ||
|
|
||
|  | ||
|
|
||
| This workflow demonstrates the following steps and tasks: | ||
|
|
||
| 1. **Prepare the Application** | ||
|
|
||
| - Prompts the user for an email address to use for the subscription for the SNS topic. | ||
| - Prompts the user for a name for the Cloud Formation stack. | ||
| - The user must confirm the email subscription to receive event emails. | ||
| - Deploys the Cloud Formation template in resources/cfn_template.yaml for resource creation. | ||
| - Stores the outputs of the stack into variables for use in the workflow. | ||
| - Creates a schedule group for all workflow schedules. | ||
|
|
||
| 2. **Create a one-time Schedule** | ||
|
|
||
| - Creates a one-time schedule to send an initial event. | ||
| - Prompts the user for a name for the one-time schedule. | ||
| - The user must confirm the email subscription to receive an event email. | ||
| - The content of the email should include the name of the newly created schedule. | ||
| - Use a Flexible Time Window of 10 minutes and set the schedule to delete after completion. | ||
|
|
||
| 3. **Create a time-based schedule** | ||
|
|
||
| - Prompts the user for a rate per minutes (example: every 2 minutes) for a scheduled recurring event. | ||
| - Creates the scheduled event for X times per hour for 1 hour. | ||
| - Deletes the schedule when the user is finished. | ||
| - Prompts the user to confirm when they are ready to delete the schedule. | ||
|
|
||
| 4. **Clean up** | ||
|
|
||
| - Prompts the user to confirm they want to destroy the stack and clean up all resources. | ||
| - Deletes the schedule group. | ||
| - Destroys the Cloud Formation stack and wait until the stack has been removed. | ||
|
|
||
| ## Prerequisites | ||
|
|
||
| Before running this workflow, ensure you have: | ||
|
|
||
| - An AWS account with proper permissions to use Amazon EventBridge Scheduler and Amazon EventBridge. | ||
|
|
||
| ## AWS Services Used | ||
|
|
||
| This workflow uses the following AWS services: | ||
|
|
||
| - Amazon EventBridge Scheduler | ||
| - Amazon EventBridge | ||
| - Amazon Simple Notification Service (SNS) | ||
| - AWS CloudFormation | ||
|
|
||
| ### Resources | ||
|
|
||
| The workflow scenario deploys the AWS CloudFormation stack with the required resources. | ||
|
|
||
| ## Amazon EventBridge Scheduler Actions | ||
|
|
||
| The workflow covers the following EventBridge Scheduler API actions: | ||
|
|
||
| - [`CreateSchedule`](https://docs.aws.amazon.com/scheduler/latest/APIReference/API_CreateSchedule.html) | ||
| - [`CreateScheduleGroup`](https://docs.aws.amazon.com/scheduler/latest/APIReference/API_CreateScheduleGroup.html) | ||
| - [`DeleteSchedule`](https://docs.aws.amazon.com/scheduler/latest/APIReference/API_DeleteSchedule.html) | ||
| - [`DeleteScheduleGroup`](https://docs.aws.amazon.com/scheduler/latest/APIReference/API_DeleteScheduleGroup.html) | ||
|
|
||
|
|
||
| ## Additional resources | ||
|
|
||
| * [EventBridge Scheduler User Guide](https://docs.aws.amazon.com/scheduler/latest/UserGuide/what-is-scheduler.html) | ||
| * [EventBridge Scheduler API Reference](https://docs.aws.amazon.com/scheduler/latest/APIReference/Welcome.html) | ||
|
|
||
| --- | ||
|
|
||
| Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| SPDX-License-Identifier: Apache-2.0 | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.