Skip to content

Commit

Permalink
Python:EventBridge Scheduler, follow (awsdocs#6999)
Browse files Browse the repository at this point in the history
  • Loading branch information
meyertst-aws authored Oct 24, 2024
1 parent 0ee372d commit e74a570
Show file tree
Hide file tree
Showing 19 changed files with 1,316 additions and 1 deletion.
63 changes: 63 additions & 0 deletions .doc_gen/metadata/scheduler_metadata.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,15 @@ scheduler_hello:
genai: some
snippet_tags:
- Scheduler.dotnetv3.HelloScheduler
Python:
versions:
- sdk_version: 3
github: python/example_code/scheduler
sdkguide:
excerpts:
- description:
snippet_tags:
- python.example_code.scheduler.Hello
services:
scheduler: {ListSchedules}
scheduler_CreateSchedule:
Expand All @@ -42,6 +51,16 @@ scheduler_CreateSchedule:
genai: most
snippet_tags:
- Scheduler.dotnetv3.CreateSchedule
Python:
versions:
- sdk_version: 3
github: python/example_code/scheduler
sdkguide:
excerpts:
- description:
snippet_tags:
- python.example_code.scheduler.EventSchedulerWrapper.decl
- python.example_code.scheduler.CreateSchedule
services:
scheduler: {CreateSchedule}
scheduler_CreateScheduleGroup:
Expand All @@ -63,6 +82,16 @@ scheduler_CreateScheduleGroup:
genai: most
snippet_tags:
- Scheduler.dotnetv3.CreateScheduleGroup
Python:
versions:
- sdk_version: 3
github: python/example_code/scheduler
sdkguide:
excerpts:
- description:
snippet_tags:
- python.example_code.scheduler.EventSchedulerWrapper.decl
- python.example_code.scheduler.CreateScheduleGroup
services:
scheduler: {CreateScheduleGroup}
scheduler_DeleteSchedule:
Expand All @@ -76,6 +105,16 @@ scheduler_DeleteSchedule:
genai: most
snippet_tags:
- Scheduler.dotnetv3.DeleteSchedule
Python:
versions:
- sdk_version: 3
github: python/example_code/scheduler
sdkguide:
excerpts:
- description:
snippet_tags:
- python.example_code.scheduler.EventSchedulerWrapper.decl
- python.example_code.scheduler.DeleteSchedule
Java:
versions:
- sdk_version: 2
Expand Down Expand Up @@ -105,6 +144,16 @@ scheduler_DeleteScheduleGroup:
genai: most
snippet_tags:
- Scheduler.dotnetv3.DeleteScheduleGroup
Python:
versions:
- sdk_version: 3
github: python/example_code/scheduler
sdkguide:
excerpts:
- description:
snippet_tags:
- python.example_code.scheduler.EventSchedulerWrapper.decl
- python.example_code.scheduler.DeleteScheduleGroup
services:
scheduler: {DeleteScheduleGroup}
scheduler_ScheduledEventsWorkflow:
Expand Down Expand Up @@ -145,5 +194,19 @@ scheduler_ScheduledEventsWorkflow:
genai: most
snippet_tags:
- Scheduler.dotnetv3.SchedulerWrapper
Python:
versions:
- sdk_version: 3
github: python/example_code/scheduler
sdkguide:
excerpts:
- description: Run an interactive scenario at a command prompt.
genai: some
snippet_tags:
- python.example_code.scheduler.FeatureScenario
- description: SchedulerWrapper class that wraps &EVlong; Scheduler actions.
genai: some
snippet_tags:
- python.example_code.scheduler.EventSchedulerWrapper.class
services:
scheduler: {CreateSchedule, CreateScheduleGroup, DeleteSchedule, DeleteScheduleGroups}
129 changes: 129 additions & 0 deletions python/example_code/scheduler/README.md
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
34 changes: 34 additions & 0 deletions python/example_code/scheduler/hello/hello_scheduler.py
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]
1 change: 1 addition & 0 deletions python/example_code/scheduler/hello/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
boto3>=1.35.38
3 changes: 3 additions & 0 deletions python/example_code/scheduler/requirements.txt
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
79 changes: 79 additions & 0 deletions python/example_code/scheduler/scenario/README.md
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.

![Scheduler scenario diagram](resources/scheduler-workflow.png)

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
Loading

0 comments on commit e74a570

Please sign in to comment.