18 types
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 millions of one-time or recurring tasks across various AWS services without provisioning or managing underlying infrastructure.
fn = nil # AWSCDK::Lambda::Function
target = AWSCDK::SchedulerTargets::LambdaInvoke.new(fn, {
input: AWSCDK::Scheduler::ScheduleTargetInput.from_object({
"payload" => "useful",
}),
})
schedule = AWSCDK::Scheduler::Schedule.new(self, "Schedule", {
schedule: AWSCDK::Scheduler::ScheduleExpression.rate(AWSCDK::Duration.minutes(10)),
target: target,
description: "This is a test schedule that invokes a lambda function every 10 minutes.",
})
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 schedules you can specify a time zone in which EventBridge Scheduler evaluates the expression.
target = nil # AWSCDK::SchedulerTargets::LambdaInvoke
rate_based_schedule = AWSCDK::Scheduler::Schedule.new(self, "Schedule", {
schedule: AWSCDK::Scheduler::ScheduleExpression.rate(AWSCDK::Duration.minutes(10)),
target: target,
description: "This is a test rate-based schedule",
})
cron_based_schedule = AWSCDK::Scheduler::Schedule.new(self, "Schedule", {
schedule: AWSCDK::Scheduler::ScheduleExpression.cron({
minute: "0",
hour: "23",
day: "20",
month: "11",
time_zone: AWSCDK::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 by specifying the time of day, date, and time zone in which EventBridge Scheduler evaluates the schedule.
target = nil # AWSCDK::SchedulerTargets::LambdaInvoke
one_time_schedule = AWSCDK::Scheduler::Schedule.new(self, "Schedule", {
schedule: AWSCDK::Scheduler::ScheduleExpression.at(
Date.new(2022, 10, 20, 19, 20, 23), AWSCDK::TimeZone.AMERICA_NEW_YORK),
target: target,
description: "This is a one-time schedule in New York timezone",
})
Your AWS account comes with a default scheduler group. You can access the default group in CDK with:
default_schedule_group = AWSCDK::Scheduler::ScheduleGroup.from_default_schedule_group(self, "DefaultGroup")
You can add a schedule to a custom scheduling group managed by you. If a custom group is not specified, the schedule is added to the default group.
target = nil # AWSCDK::SchedulerTargets::LambdaInvoke
schedule_group = AWSCDK::Scheduler::ScheduleGroup.new(self, "ScheduleGroup", {
schedule_group_name: "MyScheduleGroup",
})
AWSCDK::Scheduler::Schedule.new(self, "Schedule", {
schedule: AWSCDK::Scheduler::ScheduleExpression.rate(AWSCDK::Duration.minutes(10)),
target: target,
schedule_group: schedule_group,
})
By default, a schedule will be enabled. You can disable a schedule by setting the enabled property to false:
target = nil # AWSCDK::SchedulerTargets::LambdaInvoke
AWSCDK::Scheduler::Schedule.new(self, "Schedule", {
schedule: AWSCDK::Scheduler::ScheduleExpression.rate(AWSCDK::Duration.minutes(10)),
target: target,
enabled: false,
})
If you choose a recurring schedule, you can set the start and end time of the Schedule by specifying the start and end.
target = nil # AWSCDK::SchedulerTargets::LambdaInvoke
AWSCDK::Scheduler::Schedule.new(self, "Schedule", {
schedule: AWSCDK::Scheduler::ScheduleExpression.rate(AWSCDK::Duration.hours(12)),
target: target,
start: Date.new("2023-01-01T00:00:00.000Z"),
_end: Date.new("2023-02-01T00:00:00.000Z"),
})
The aws-cdk-lib/aws-scheduler-targets module includes classes that implement the IScheduleTarget interface for
various AWS services. EventBridge Scheduler supports two types of targets:
A list of supported targets can be found at aws-cdk-lib/aws-scheduler-targets.
Targets can be invoked with a custom input. The ScheduleTargetInput class supports free-form text input and JSON-formatted object input:
input = AWSCDK::Scheduler::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, e.g., 2022-03-22T18:59:43Z.ContextAttribute.executionId() – The unique ID that EventBridge Scheduler assigns for each attempted invocation of a target, e.g., d32c5kddcf5bb8c3.ContextAttribute.attemptNumber() – A counter that identifies the attempt number for the current invocation, e.g., 1.text = "Attempt number: #{AWSCDK::Scheduler::ContextAttribute.attempt_number}"
input = AWSCDK::Scheduler::ScheduleTargetInput.from_text(text)
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, the LambdaInvoke target will grant the
IAM execution role lambda:InvokeFunction permission to invoke the Lambda function.
fn = nil # AWSCDK::Lambda::Function
role = AWSCDK::IAM::Role.new(self, "Role", {
assumed_by: AWSCDK::IAM::ServicePrincipal.new("scheduler.amazonaws.com"),
})
target = AWSCDK::SchedulerTargets::LambdaInvoke.new(fn, {
input: AWSCDK::Scheduler::ScheduleTargetInput.from_object({
"payload" => "useful",
}),
role: role,
})
EventBridge Scheduler integrates with AWS Key Management Service (AWS KMS) to encrypt and decrypt your data using an AWS KMS key. EventBridge Scheduler supports two types of KMS keys: AWS owned keys, and customer managed keys.
By default, all events in Scheduler are encrypted with a key that AWS owns and manages.
If you wish you can also provide a customer managed key to encrypt and decrypt the payload that your schedule delivers to its target using the key property.
Target classes will automatically add AWS kms:Decrypt permission to your schedule's execution role permissions policy.
key = nil # AWSCDK::KMS::Key
fn = nil # AWSCDK::Lambda::Function
target = AWSCDK::SchedulerTargets::LambdaInvoke.new(fn, {
input: AWSCDK::Scheduler::ScheduleTargetInput.from_object({
payload: "useful",
}),
})
schedule = AWSCDK::Scheduler::Schedule.new(self, "Schedule", {
schedule: AWSCDK::Scheduler::ScheduleExpression.rate(AWSCDK::Duration.minutes(10)),
target: target,
key: key,
})
See Data protection in Amazon EventBridge Scheduler for more details.
You can configure flexible time windows by specifying the time_window property.
Flexible time windows are disabled by default.
target = nil # AWSCDK::SchedulerTargets::LambdaInvoke
schedule = AWSCDK::Scheduler::Schedule.new(self, "Schedule", {
schedule: AWSCDK::Scheduler::ScheduleExpression.rate(AWSCDK::Duration.hours(12)),
target: target,
time_window: AWSCDK::Scheduler::TimeWindow.flexible(AWSCDK::Duration.hours(10)),
})
See Configuring flexible time windows for more details.
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 = nil # AWSCDK::Lambda::Function
dlq = AWSCDK::SQS::Queue.new(self, "DLQ", {
queue_name: "MyDLQ",
})
target = AWSCDK::SchedulerTargets::LambdaInvoke.new(fn, {
dead_letter_queue: dlq,
max_event_age: AWSCDK::Duration.minutes(1),
retry_attempts: 3,
})
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.
The Schedule class provides static methods for accessing all schedules metrics with default configuration, such as metric_all_errors for viewing errors when executing targets.
AWSCDK::CloudWatch::Alarm.new(self, "SchedulesErrorAlarm", {
metric: AWSCDK::Scheduler::Schedule.metric_all_errors,
threshold: 0,
evaluation_periods: 1,
})
To view metrics for a specific group you can use methods on class ScheduleGroup:
schedule_group = AWSCDK::Scheduler::ScheduleGroup.new(self, "ScheduleGroup", {
schedule_group_name: "MyScheduleGroup",
})
AWSCDK::CloudWatch::Alarm.new(self, "MyGroupErrorAlarm", {
metric: schedule_group.metric_target_errors,
evaluation_periods: 1,
threshold: 0,
})
# Or use default group
default_schedule_group = AWSCDK::Scheduler::ScheduleGroup.from_default_schedule_group(self, "DefaultScheduleGroup")
AWSCDK::CloudWatch::Alarm.new(self, "DefaultScheduleGroupErrorAlarm", {
metric: default_schedule_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 EventBridge Scheduler User Guide.