forked from awsdocs/aws-doc-sdk-examples
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Python:EventBridge Scheduler, follow (awsdocs#6999)
- Loading branch information
1 parent
0ee372d
commit e74a570
Showing
19 changed files
with
1,316 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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 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 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 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 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 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.