The CDK Construct Library for Amazon Scheduler
Project description
Amazon EventBridge Scheduler Construct Library
---The APIs of higher level constructs in this module are experimental and under active development. They are subject to non-backward compatible changes or removal in any future version. These are not subject to the Semantic Versioning model and breaking changes will be announced in the release notes. This means that while you may use them, you may need to update your source code when upgrading to a newer version of this package.
Amazon EventBridge Scheduler is a feature from Amazon EventBridge that allows you to create, run, and manage scheduled tasks at scale. With EventBridge Scheduler, you can schedule one-time or recurrently tens of millions of tasks across many AWS services without provisioning or managing underlying infrastructure.
- Schedule: A schedule is the main resource you create, configure, and manage using Amazon EventBridge Scheduler. Every schedule has a schedule expression that determines when, and with what frequency, the schedule runs. EventBridge Scheduler supports three types of schedules: rate, cron, and one-time schedules. When you create a schedule, you configure a target for the schedule to invoke.
- Targets: A target is an API operation that EventBridge Scheduler calls on your behalf every time your schedule runs. EventBridge Scheduler supports two types of targets: templated targets and universal targets. Templated targets invoke common API operations across a core groups of services. For example, EventBridge Scheduler supports templated targets for invoking AWS Lambda Function or starting execution of Step Function state machine. For API operations that are not supported by templated targets you can use customizeable universal targets. Universal targets support calling more than 6,000 API operations across over 270 AWS services.
- Schedule Group: A schedule group is an Amazon EventBridge Scheduler resource that you use to organize your schedules. Your AWS account comes with a default scheduler group. A new schedule will always be added to a scheduling group. If you do not provide a scheduling group to add to, it will be added to the default scheduling group. You can create up to 500 schedule groups in your AWS account. Groups can be used to organize the schedules logically, access the schedule metrics and manage permissions at group granularity (see details below). Scheduling groups support tagging: with EventBridge Scheduler, you apply tags to schedule groups, not to individual schedules to organize your resources.
This module is part of the AWS Cloud Development Kit project. It allows you to define Event Bridge Schedules.
This module is in active development. Some features may not be implemented yet.
Defining a schedule
# fn: lambda.Function
target = targets.LambdaInvoke(fn,
input=ScheduleTargetInput.from_object({
"payload": "useful"
})
)
schedule = Schedule(self, "Schedule",
schedule=ScheduleExpression.rate(Duration.minutes(10)),
target=target,
description="This is a test schedule that invokes lambda function every 10 minutes."
)
Schedule Expressions
You can choose from three schedule types when configuring your schedule: rate-based, cron-based, and one-time schedules.
Both rate-based and cron-based schedules are recurring schedules. You can configure each recurring schedule type using a schedule expression. For cron-based schedule you can specify a time zone in which EventBridge Scheduler evaluates the expression.
ScheduleExpression should be used together with class Schedule, which is not yet implemented.
# target: targets.LambdaInvoke
rate_based_schedule = Schedule(self, "Schedule",
schedule=ScheduleExpression.rate(Duration.minutes(10)),
target=target,
description="This is a test rate-based schedule"
)
cron_based_schedule = Schedule(self, "Schedule",
schedule=ScheduleExpression.cron(
minute="0",
hour="23",
day="20",
month="11",
time_zone=TimeZone.AMERICA_NEW_YORK
),
target=target,
description="This is a test cron-based schedule that will run at 11:00 PM, on day 20 of the month, only in November in New York timezone"
)
A one-time schedule is a schedule that invokes a target only once. You configure a one-time schedule when by specifying the time of the day, date, and time zone in which EventBridge Scheduler evaluates the schedule.
# target: targets.LambdaInvoke
one_time_schedule = Schedule(self, "Schedule",
schedule=ScheduleExpression.at(
Date(2022, 10, 20, 19, 20, 23), TimeZone.AMERICA_NEW_YORK),
target=target,
description="This is a one-time schedule in New York timezone"
)
Grouping Schedules
Your AWS account comes with a default scheduler group. You can access default group in CDK with:
default_group = Group.from_default_group(self, "DefaultGroup")
If not specified a schedule is added to the default group. However, you can also add the schedule to a custom scheduling group managed by you:
# target: targets.LambdaInvoke
group = Group(self, "Group",
group_name="MyGroup"
)
Schedule(self, "Schedule",
schedule=ScheduleExpression.rate(Duration.minutes(10)),
target=target,
group=group
)
Scheduler Targets
The @aws-cdk/aws-schedule-targets-alpha
module includes classes that implement the IScheduleTarget
interface for
various AWS services. EventBridge Scheduler supports two types of targets: templated targets invoke common API
operations across a core groups of services, and customizeable universal targets that you can use to call more
than 6,000 operations across over 270 services. A list of supported targets can be found at @aws-cdk/aws-schedule-targets-alpha
.
Input
Target can be invoked with a custom input. Class ScheduleTargetInput
supports free form text input and JSON-formatted object input:
input = ScheduleTargetInput.from_object({
"QueueName": "MyQueue"
})
You can include context attributes in your target payload. EventBridge Scheduler will replace each keyword with its respective value and deliver it to the target. See full list of supported context attributes:
ContextAttribute.scheduleArn()
– The ARN of the schedule.ContextAttribute.scheduledTime()
– The time you specified for the schedule to invoke its target, for example, 2022-03-22T18:59:43Z.ContextAttribute.executionId()
– The unique ID that EventBridge Scheduler assigns for each attempted invocation of a target, for example, d32c5kddcf5bb8c3.ContextAttribute.attemptNumber()
– A counter that identifies the attempt number for the current invocation, for example, 1.
text = f"Attempt number: {ContextAttribute.attemptNumber}"
input = ScheduleTargetInput.from_text(text)
Specifying Execution Role
An execution role is an IAM role that EventBridge Scheduler assumes in order to interact with other AWS services on your behalf.
The classes for templated schedule targets automatically create an IAM role with all the minimum necessary
permissions to interact with the templated target. If you wish you may specify your own IAM role, then the templated targets
will grant minimal required permissions. For example: for invoking Lambda function target LambdaInvoke
will grant
execution IAM role permission to lambda:InvokeFunction
.
# fn: lambda.Function
role = iam.Role(self, "Role",
assumed_by=iam.ServicePrincipal("scheduler.amazonaws.com")
)
target = targets.LambdaInvoke(fn,
input=ScheduleTargetInput.from_object({
"payload": "useful"
}),
role=role
)
Cross-account and cross-region targets
Executing cross-account and cross-region targets are not supported yet.
Specifying Encryption key
TODO: Not yet implemented. See section in L2 Event Bridge Scheduler RFC
Error-handling
You can configure how your schedule handles failures, when EventBridge Scheduler is unable to deliver an event successfully to a target, by using two primary mechanisms: a retry policy, and a dead-letter queue (DLQ).
A retry policy determines the number of times EventBridge Scheduler must retry a failed event, and how long to keep an unprocessed event.
A DLQ is a standard Amazon SQS queue EventBridge Scheduler uses to deliver failed events to, after the retry policy has been exhausted. You can use a DLQ to troubleshoot issues with your schedule or its downstream target. If you've configured a retry policy for your schedule, EventBridge Scheduler delivers the dead-letter event after exhausting the maximum number of retries you set in the retry policy.
# fn: lambda.Function
dlq = sqs.Queue(self, "DLQ",
queue_name="MyDLQ"
)
target = targets.LambdaInvoke(fn,
dead_letter_queue=dlq,
max_event_age=Duration.minutes(1),
retry_attempts=3
)
Overriding Target Properties
TODO: Not yet implemented. See section in L2 Event Bridge Scheduler RFC
Monitoring
You can monitor Amazon EventBridge Scheduler using CloudWatch, which collects raw data and processes it into readable, near real-time metrics. EventBridge Scheduler emits a set of metrics for all schedules, and an additional set of metrics for schedules that have an associated dead-letter queue (DLQ). If you configure a DLQ for your schedule, EventBridge Scheduler publishes additional metrics when your schedule exhausts its retry policy.
Metrics for all schedules
TODO: Not yet implemented. See section in L2 Event Bridge Scheduler RFC
Metrics for a Group
To view metrics for a specific group you can use methods on class Group
:
group = Group(self, "Group",
group_name="MyGroup"
)
cloudwatch.Alarm(self, "MyGroupErrorAlarm",
metric=group.metric_target_errors(),
evaluation_periods=1,
threshold=0
)
# Or use default group
default_group = Group.from_default_group(self, "DefaultGroup")
cloudwatch.Alarm(self, "DefaultGroupErrorAlarm",
metric=default_group.metric_target_errors(),
evaluation_periods=1,
threshold=0
)
See full list of metrics and their description at Monitoring Using CloudWatch Metrics in the AWS Event Bridge Scheduler User Guide.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for aws-cdk.aws-scheduler-alpha-2.94.0a0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | cc580ba9ed5fdb12e71619d5384d0398a8fdb6ec10bd80ef5b7446ccf68a41c3 |
|
MD5 | 47c730e6bdd77915dd82bd3995d8d1db |
|
BLAKE2b-256 | 2ceba733ac4ba2bd0d7ee9109cb96f0b1f1f9d880a236852f85fcbbf1e6c756c |
Hashes for aws_cdk.aws_scheduler_alpha-2.94.0a0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2f3e0e6e4c1858474e94b2e0e36af48049235f74721063d42a1f7403b40d37e8 |
|
MD5 | 96756520eadf90f5f0b118385f1167b8 |
|
BLAKE2b-256 | b00d396dad3de1e70dede1ff10ee32af2c775d6a19ae3e069025d84d75259618 |