Skip to content

Commit e74a570

Browse files
authored
Python:EventBridge Scheduler, follow (awsdocs#6999)
1 parent 0ee372d commit e74a570

19 files changed

+1316
-1
lines changed

.doc_gen/metadata/scheduler_metadata.yaml

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,15 @@ scheduler_hello:
2121
genai: some
2222
snippet_tags:
2323
- Scheduler.dotnetv3.HelloScheduler
24+
Python:
25+
versions:
26+
- sdk_version: 3
27+
github: python/example_code/scheduler
28+
sdkguide:
29+
excerpts:
30+
- description:
31+
snippet_tags:
32+
- python.example_code.scheduler.Hello
2433
services:
2534
scheduler: {ListSchedules}
2635
scheduler_CreateSchedule:
@@ -42,6 +51,16 @@ scheduler_CreateSchedule:
4251
genai: most
4352
snippet_tags:
4453
- Scheduler.dotnetv3.CreateSchedule
54+
Python:
55+
versions:
56+
- sdk_version: 3
57+
github: python/example_code/scheduler
58+
sdkguide:
59+
excerpts:
60+
- description:
61+
snippet_tags:
62+
- python.example_code.scheduler.EventSchedulerWrapper.decl
63+
- python.example_code.scheduler.CreateSchedule
4564
services:
4665
scheduler: {CreateSchedule}
4766
scheduler_CreateScheduleGroup:
@@ -63,6 +82,16 @@ scheduler_CreateScheduleGroup:
6382
genai: most
6483
snippet_tags:
6584
- Scheduler.dotnetv3.CreateScheduleGroup
85+
Python:
86+
versions:
87+
- sdk_version: 3
88+
github: python/example_code/scheduler
89+
sdkguide:
90+
excerpts:
91+
- description:
92+
snippet_tags:
93+
- python.example_code.scheduler.EventSchedulerWrapper.decl
94+
- python.example_code.scheduler.CreateScheduleGroup
6695
services:
6796
scheduler: {CreateScheduleGroup}
6897
scheduler_DeleteSchedule:
@@ -76,6 +105,16 @@ scheduler_DeleteSchedule:
76105
genai: most
77106
snippet_tags:
78107
- Scheduler.dotnetv3.DeleteSchedule
108+
Python:
109+
versions:
110+
- sdk_version: 3
111+
github: python/example_code/scheduler
112+
sdkguide:
113+
excerpts:
114+
- description:
115+
snippet_tags:
116+
- python.example_code.scheduler.EventSchedulerWrapper.decl
117+
- python.example_code.scheduler.DeleteSchedule
79118
Java:
80119
versions:
81120
- sdk_version: 2
@@ -105,6 +144,16 @@ scheduler_DeleteScheduleGroup:
105144
genai: most
106145
snippet_tags:
107146
- Scheduler.dotnetv3.DeleteScheduleGroup
147+
Python:
148+
versions:
149+
- sdk_version: 3
150+
github: python/example_code/scheduler
151+
sdkguide:
152+
excerpts:
153+
- description:
154+
snippet_tags:
155+
- python.example_code.scheduler.EventSchedulerWrapper.decl
156+
- python.example_code.scheduler.DeleteScheduleGroup
108157
services:
109158
scheduler: {DeleteScheduleGroup}
110159
scheduler_ScheduledEventsWorkflow:
@@ -145,5 +194,19 @@ scheduler_ScheduledEventsWorkflow:
145194
genai: most
146195
snippet_tags:
147196
- Scheduler.dotnetv3.SchedulerWrapper
197+
Python:
198+
versions:
199+
- sdk_version: 3
200+
github: python/example_code/scheduler
201+
sdkguide:
202+
excerpts:
203+
- description: Run an interactive scenario at a command prompt.
204+
genai: some
205+
snippet_tags:
206+
- python.example_code.scheduler.FeatureScenario
207+
- description: SchedulerWrapper class that wraps &EVlong; Scheduler actions.
208+
genai: some
209+
snippet_tags:
210+
- python.example_code.scheduler.EventSchedulerWrapper.class
148211
services:
149212
scheduler: {CreateSchedule, CreateScheduleGroup, DeleteSchedule, DeleteScheduleGroups}
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
# EventBridge Scheduler code examples for the SDK for Python
2+
3+
## Overview
4+
5+
Shows how to use the AWS SDK for Python (Boto3) to work with Amazon EventBridge Scheduler.
6+
7+
<!--custom.overview.start-->
8+
<!--custom.overview.end-->
9+
10+
_EventBridge Scheduler allows you to create, run, and manage tasks on a schedule from one central, managed service._
11+
12+
## ⚠ Important
13+
14+
* 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/).
15+
* Running the tests might result in charges to your AWS account.
16+
* 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).
17+
* 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).
18+
19+
<!--custom.important.start-->
20+
<!--custom.important.end-->
21+
22+
## Code examples
23+
24+
### Prerequisites
25+
26+
For prerequisites, see the [README](../../README.md#Prerequisites) in the `python` folder.
27+
28+
Install the packages required by these examples by running the following in a virtual environment:
29+
30+
```
31+
python -m pip install -r requirements.txt
32+
```
33+
34+
<!--custom.prerequisites.start-->
35+
<!--custom.prerequisites.end-->
36+
37+
### Get started
38+
39+
- [Hello EventBridge Scheduler](hello/hello_scheduler.py#L4) (`ListSchedules`)
40+
41+
42+
### Single actions
43+
44+
Code excerpts that show you how to call individual service functions.
45+
46+
- [CreateSchedule](scheduler_wrapper.py#L38)
47+
- [CreateScheduleGroup](scheduler_wrapper.py#L131)
48+
- [DeleteSchedule](scheduler_wrapper.py#L104)
49+
- [DeleteScheduleGroup](scheduler_wrapper.py#L160)
50+
51+
### Scenarios
52+
53+
Code examples that show you how to accomplish a specific task by calling multiple
54+
functions within the same service.
55+
56+
- [Scheduled Events workflow](scenario/scheduler_scenario.py)
57+
58+
59+
<!--custom.examples.start-->
60+
<!--custom.examples.end-->
61+
62+
## Run the examples
63+
64+
### Instructions
65+
66+
67+
<!--custom.instructions.start-->
68+
<!--custom.instructions.end-->
69+
70+
#### Hello EventBridge Scheduler
71+
72+
This example shows you how to get started using EventBridge Scheduler.
73+
74+
```
75+
python hello/hello_scheduler.py
76+
```
77+
78+
79+
#### Scheduled Events workflow
80+
81+
This example shows you how to do the following:
82+
83+
- Deploy a CloudFormation stack with required resources.
84+
- Create a EventBridge Scheduler schedule group.
85+
- Create a one-time EventBridge Scheduler schedule with a flexible time window.
86+
- Create a recurring EventBridge Scheduler schedule with a specified rate.
87+
- Delete EventBridge Scheduler the schedule and schedule group.
88+
- Clean up resources and delete the stack.
89+
90+
<!--custom.scenario_prereqs.scheduler_ScheduledEventsWorkflow.start-->
91+
<!--custom.scenario_prereqs.scheduler_ScheduledEventsWorkflow.end-->
92+
93+
Start the example by running the following at a command prompt:
94+
95+
```
96+
python scenario/scheduler_scenario.py
97+
```
98+
99+
100+
<!--custom.scenarios.scheduler_ScheduledEventsWorkflow.start-->
101+
<!--custom.scenarios.scheduler_ScheduledEventsWorkflow.end-->
102+
103+
### Tests
104+
105+
⚠ Running tests might result in charges to your AWS account.
106+
107+
108+
To find instructions for running these tests, see the [README](../../README.md#Tests)
109+
in the `python` folder.
110+
111+
112+
113+
<!--custom.tests.start-->
114+
<!--custom.tests.end-->
115+
116+
## Additional resources
117+
118+
- [EventBridge Scheduler User Guide](https://docs.aws.amazon.com/scheduler/latest/userguide/intro.html)
119+
- [EventBridge Scheduler API Reference](https://docs.aws.amazon.com/scheduler/latest/apireference/Welcome.html)
120+
- [SDK for Python EventBridge Scheduler reference](https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/scheduler.html)
121+
122+
<!--custom.resources.start-->
123+
<!--custom.resources.end-->
124+
125+
---
126+
127+
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
128+
129+
SPDX-License-Identifier: Apache-2.0
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
# snippet-start:[python.example_code.scheduler.Hello]
5+
import boto3
6+
7+
8+
def hello_scheduler(scheduler_client):
9+
"""
10+
Use the AWS SDK for Python (Boto3) to create an Amazon EventBridge Scheduler
11+
client and list the schedules in your account.
12+
This example uses the default settings specified in your shared credentials
13+
and config files.
14+
15+
:param scheduler_client: A Boto3 Amazon EventBridge Scheduler Client object. This object wraps
16+
the low-level Amazon EventBridge Scheduler service API.
17+
"""
18+
print("Hello, Amazon EventBridge Scheduler! Let's list some of your schedules:\n")
19+
paginator = scheduler_client.get_paginator("list_schedules")
20+
page_iterator = paginator.paginate(PaginationConfig={"MaxItems": 10})
21+
22+
schedule_names: [str] = []
23+
for page in page_iterator:
24+
for schedule in page["Schedules"]:
25+
schedule_names.append(schedule["Name"])
26+
27+
print(f"{len(schedule_names)} schedule(s) retrieved.")
28+
for schedule_name in schedule_names:
29+
print(f"\t{schedule_name}")
30+
31+
32+
if __name__ == "__main__":
33+
hello_scheduler(boto3.client("scheduler"))
34+
# snippet-end:[python.example_code.scheduler.Hello]
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
boto3>=1.35.38
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
boto3>=1.35.38
2+
pytest>=8.3.3
3+
botocore>=1.35.38
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# Amazon EventBridge Scheduler Workflow
2+
3+
## Overview
4+
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.
5+
6+
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.
7+
8+
![Scheduler scenario diagram](resources/scheduler-workflow.png)
9+
10+
This workflow demonstrates the following steps and tasks:
11+
12+
1. **Prepare the Application**
13+
14+
- Prompts the user for an email address to use for the subscription for the SNS topic.
15+
- Prompts the user for a name for the Cloud Formation stack.
16+
- The user must confirm the email subscription to receive event emails.
17+
- Deploys the Cloud Formation template in resources/cfn_template.yaml for resource creation.
18+
- Stores the outputs of the stack into variables for use in the workflow.
19+
- Creates a schedule group for all workflow schedules.
20+
21+
2. **Create a one-time Schedule**
22+
23+
- Creates a one-time schedule to send an initial event.
24+
- Prompts the user for a name for the one-time schedule.
25+
- The user must confirm the email subscription to receive an event email.
26+
- The content of the email should include the name of the newly created schedule.
27+
- Use a Flexible Time Window of 10 minutes and set the schedule to delete after completion.
28+
29+
3. **Create a time-based schedule**
30+
31+
- Prompts the user for a rate per minutes (example: every 2 minutes) for a scheduled recurring event.
32+
- Creates the scheduled event for X times per hour for 1 hour.
33+
- Deletes the schedule when the user is finished.
34+
- Prompts the user to confirm when they are ready to delete the schedule.
35+
36+
4. **Clean up**
37+
38+
- Prompts the user to confirm they want to destroy the stack and clean up all resources.
39+
- Deletes the schedule group.
40+
- Destroys the Cloud Formation stack and wait until the stack has been removed.
41+
42+
## Prerequisites
43+
44+
Before running this workflow, ensure you have:
45+
46+
- An AWS account with proper permissions to use Amazon EventBridge Scheduler and Amazon EventBridge.
47+
48+
## AWS Services Used
49+
50+
This workflow uses the following AWS services:
51+
52+
- Amazon EventBridge Scheduler
53+
- Amazon EventBridge
54+
- Amazon Simple Notification Service (SNS)
55+
- AWS CloudFormation
56+
57+
### Resources
58+
59+
The workflow scenario deploys the AWS CloudFormation stack with the required resources.
60+
61+
## Amazon EventBridge Scheduler Actions
62+
63+
The workflow covers the following EventBridge Scheduler API actions:
64+
65+
- [`CreateSchedule`](https://docs.aws.amazon.com/scheduler/latest/APIReference/API_CreateSchedule.html)
66+
- [`CreateScheduleGroup`](https://docs.aws.amazon.com/scheduler/latest/APIReference/API_CreateScheduleGroup.html)
67+
- [`DeleteSchedule`](https://docs.aws.amazon.com/scheduler/latest/APIReference/API_DeleteSchedule.html)
68+
- [`DeleteScheduleGroup`](https://docs.aws.amazon.com/scheduler/latest/APIReference/API_DeleteScheduleGroup.html)
69+
70+
71+
## Additional resources
72+
73+
* [EventBridge Scheduler User Guide](https://docs.aws.amazon.com/scheduler/latest/UserGuide/what-is-scheduler.html)
74+
* [EventBridge Scheduler API Reference](https://docs.aws.amazon.com/scheduler/latest/APIReference/Welcome.html)
75+
76+
---
77+
78+
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
79+
SPDX-License-Identifier: Apache-2.0

0 commit comments

Comments
 (0)