@aws-cdk/aws-scheduler-alpha

中文文档
2.186.0-alpha.0发布于 2025/03/27Apache-2.0TypeScriptCommonJS零依赖近 30 天下载 3.6万

This module is deprecated. All constructs are now available under aws-cdk-lib/aws-scheduler.

此软件包已被标记为弃用

This package has been stabilized and moved to aws-cdk-lib

原始使用文档
文档目录14 节
  1. Defining a schedule
  2. Schedule Expressions
  3. Grouping Schedules
  4. Disabling Schedules
  5. Configuring a start and end time of the Schedule
  6. Scheduler Targets
  7. Input
  8. Specifying an execution role
  9. Specifying an encryption key
  10. Configuring flexible time windows
  11. Error-handling
  12. Monitoring
  13. Metrics for all schedules
  14. Metrics for a Schedule Group

Amazon EventBridge Scheduler Construct Library


Deprecated

This API may emit warnings. Backward compatibility is not guaranteed.


All constructs moved to aws-cdk-lib/aws-scheduler.

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.

  1. 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.
  2. Target: 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 Functions state machine. For API operations that are not supported by templated targets you can use customizable universal targets. Universal targets support calling more than 6,000 API operations across over 270 AWS services.
  3. 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). Schedule groups support tagging. With EventBridge Scheduler, you apply tags to schedule groups, not to individual schedules to organize your resources.

Defining a schedule

declare const fn: lambda.Function;

const target = new targets.LambdaInvoke(fn, {
    input: ScheduleTargetInput.fromObject({
        "payload": "useful",
    }),
});

const schedule = new Schedule(this, 'Schedule', {
    schedule: ScheduleExpression.rate(Duration.minutes(10)),
    target,
    description: 'This is a test schedule that invokes a 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 schedules you can specify a time zone in which EventBridge Scheduler evaluates the expression.

declare const target: targets.LambdaInvoke;

const rateBasedSchedule = new Schedule(this, 'Schedule', {
    schedule: ScheduleExpression.rate(Duration.minutes(10)),
    target,
    description: 'This is a test rate-based schedule',
});

const cronBasedSchedule = new Schedule(this, 'Schedule', {
    schedule: ScheduleExpression.cron({ 
        minute: '0',
        hour: '23',
        day: '20',
        month: '11',
        timeZone: TimeZone.AMERICA_NEW_YORK,
    }),
    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.

declare const target: targets.LambdaInvoke;

const oneTimeSchedule = new Schedule(this, 'Schedule', {
    schedule: ScheduleExpression.at(
        new Date(2022, 10, 20, 19, 20, 23),
        TimeZone.AMERICA_NEW_YORK,
    ),
    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 the default group in CDK with:

const defaultScheduleGroup = ScheduleGroup.fromDefaultScheduleGroup(this, "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.

declare const target: targets.LambdaInvoke;

const scheduleGroup = new ScheduleGroup(this, "ScheduleGroup", {
    scheduleGroupName: "MyScheduleGroup",
});

new Schedule(this, 'Schedule', {
    schedule: ScheduleExpression.rate(Duration.minutes(10)),
    target,
    scheduleGroup,
});

Disabling Schedules

By default, a schedule will be enabled. You can disable a schedule by setting the enabled property to false:

declare const target: targets.LambdaInvoke;

new Schedule(this, 'Schedule', {
    schedule: ScheduleExpression.rate(Duration.minutes(10)),
    target: target,
    enabled: false,
});

Configuring a start and end time of the Schedule

If you choose a recurring schedule, you can set the start and end time of the Schedule by specifying the start and end.

declare const target: targets.LambdaInvoke;

new Schedule(this, 'Schedule', {
    schedule: ScheduleExpression.rate(cdk.Duration.hours(12)),
    target: target,
    start: new Date('2023-01-01T00:00:00.000Z'),
    end: new Date('2023-02-01T00:00:00.000Z'),
});

Scheduler Targets

The @aws-cdk/aws-scheduler-targets-alpha module includes classes that implement the IScheduleTarget interface for various AWS services. EventBridge Scheduler supports two types of targets:

  1. Templated targets which invoke common API operations across a core groups of services, and
  2. Universal targets that you can customize to call more than 6,000 operations across over 270 services.

A list of supported targets can be found at @aws-cdk/aws-scheduler-targets-alpha.

Input

Targets can be invoked with a custom input. The ScheduleTargetInput class supports free-form text input and JSON-formatted object input:

const input = ScheduleTargetInput.fromObject({
    '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:

  1. ContextAttribute.scheduleArn() – The ARN of the schedule.
  2. ContextAttribute.scheduledTime() – The time you specified for the schedule to invoke its target, e.g., 2022-03-22T18:59:43Z.
  3. ContextAttribute.executionId() – The unique ID that EventBridge Scheduler assigns for each attempted invocation of a target, e.g., d32c5kddcf5bb8c3.
  4. ContextAttribute.attemptNumber() – A counter that identifies the attempt number for the current invocation, e.g., 1.
const text = `Attempt number: ${ContextAttribute.attemptNumber}`;
const input = ScheduleTargetInput.fromText(text);

Specifying an 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, the LambdaInvoke target will grant the IAM execution role lambda:InvokeFunction permission to invoke the Lambda function.

declare const fn: lambda.Function;

const role = new iam.Role(this, 'Role', {
    assumedBy: new iam.ServicePrincipal('scheduler.amazonaws.com'),
});

const target = new targets.LambdaInvoke(fn, {
    input: ScheduleTargetInput.fromObject({
        "payload": "useful"
    }),
    role,
});

Specifying an encryption key

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.

declare const key: kms.Key;
declare const fn: lambda.Function;

const target = new targets.LambdaInvoke(fn, {
    input: ScheduleTargetInput.fromObject({
        payload: 'useful',
    }),
});

const schedule = new Schedule(this, 'Schedule', {
    schedule: ScheduleExpression.rate(Duration.minutes(10)),
    target,
    key,
});

See Data protection in Amazon EventBridge Scheduler for more details.

Configuring flexible time windows

You can configure flexible time windows by specifying the timeWindow property. Flexible time windows are disabled by default.

declare const target: targets.LambdaInvoke;

const schedule = new Schedule(this, 'Schedule', {
    schedule: ScheduleExpression.rate(Duration.hours(12)),
    target,
    timeWindow: TimeWindow.flexible(Duration.hours(10)),
});

See Configuring flexible time windows for more details.

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.

declare const fn: lambda.Function;

const dlq = new sqs.Queue(this, "DLQ", {
    queueName: 'MyDLQ',
});

const target = new targets.LambdaInvoke(fn, {
    deadLetterQueue: dlq,
    maxEventAge: Duration.minutes(1),
    retryAttempts: 3
});

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

The Schedule class provides static methods for accessing all schedules metrics with default configuration, such as metricAllErrors for viewing errors when executing targets.

new cloudwatch.Alarm(this, 'SchedulesErrorAlarm', {
   metric: Schedule.metricAllErrors(),
   threshold: 0,
   evaluationPeriods: 1,
});

Metrics for a Schedule Group

To view metrics for a specific group you can use methods on class ScheduleGroup:

const scheduleGroup = new ScheduleGroup(this, "ScheduleGroup", {
    scheduleGroupName: "MyScheduleGroup",
});

new cloudwatch.Alarm(this, 'MyGroupErrorAlarm', {
    metric: scheduleGroup.metricTargetErrors(),
    evaluationPeriods: 1,
    threshold: 0
});

// Or use default group
const defaultScheduleGroup = ScheduleGroup.fromDefaultScheduleGroup(this, "DefaultScheduleGroup");
new cloudwatch.Alarm(this, 'DefaultScheduleGroupErrorAlarm', {
    metric: defaultScheduleGroup.metricTargetErrors(),
    evaluationPeriods: 1,
    threshold: 0
});

See full list of metrics and their description at Monitoring Using CloudWatch Metrics in the AWS EventBridge Scheduler User Guide.

版本与发布记录

@aws-cdk/aws-scheduler-alpha 共发布 151 个版本,首次发布于 2023/05/20;过去 12 个月发布了 0 个版本。

版本发布日期距今
2.186.0-alpha.0latest2025/03/271.6 年前
2.185.0-alpha.02025/03/201.6 年前
2.184.1-alpha.02025/03/141.7 年前
2.184.0-alpha.02025/03/141.7 年前
2.183.0-alpha.02025/03/121.7 年前
2.182.0-alpha.02025/03/061.7 年前

关于 @aws-cdk/aws-scheduler-alpha 的常见问题

@aws-cdk/aws-scheduler-alpha 是什么?

This module is deprecated. All constructs are now available under aws-cdk-lib/aws-scheduler.注意:该包已被作者标记为弃用——This package has been stabilized and moved to aws-cdk-lib

如何安装 @aws-cdk/aws-scheduler-alpha?

在项目目录运行 npm install @aws-cdk/aws-scheduler-alpha,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:constructs(^10.0.0)、aws-cdk-lib(^2.186.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@aws-cdk/aws-scheduler-alpha 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@aws-cdk/aws-scheduler-alpha 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@aws-cdk/aws-scheduler-alpha 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >= 14.15.0。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@aws-cdk/aws-scheduler-alpha 可以商用吗?

@aws-cdk/aws-scheduler-alpha 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@aws-cdk/aws-scheduler-alpha 的下载量有多少?

根据 npm 官方下载统计,@aws-cdk/aws-scheduler-alpha 在 2026/10/08 – 2026/11/06 这 30 天内被下载 36,017 次,日均约 1,201 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@aws-cdk/aws-scheduler-alpha 还在维护吗?

最新版本 2.186.0-alpha.0 发布于 2025/03/27(1.6 年前);至今共发布 151 个版本,其中过去 12 个月发布了 0 个;首次发布于 2023/05/20。作者已将其标记为弃用,新项目建议寻找替代方案。