亚马逊事件Bridge 调度器构造库
此API 可能会发出警告 。 后向兼容性没有保证 .
所有构造已移入 aws-cdk-lib/aws-scheduler。 。 。 .
亚马逊事件Bridge 调度器 是一个来自亚马逊事件Bridge的特性 用于创建、运行和管理预定任务。 使用 EventBridge 调度器,您可以在不提供或管理基础基础设施的情况下,为各种AWS服务安排数百万个一次性或重复的任务.
- 时间表: 调度是您使用 Amazon EventBridge 调度器创建,配置和管理的主要资源. 每个调度都有一个调度表达式,可以决定调度的时间和频率. EventBridge调度器支持三种调度:速率,克龙和一次性调度. 当您创建一个调度时,您会配置一个调度引用的目标 .
- 目标: 一个目标是一个 API 操作, EventBridge 调度器每次运行时都会代表您调用. 事件桥排程器 支持两类目标:模板化目标和普遍目标。 模板化目标在核心组别中援引共同的API操作 服务。 例如, EventBridge 调度器支持模板化的目标用于引用 AWS Lambda 函数或开始执行 Step 函数状态 机器。 机器。 。 对于没有模板目标支持的API操作,您可以使用自定义的通用目标. 通用目标支助 超过270个AWS服务的6000多个API业务.
- 时间表组: 调度组是一个Amazon EventBridge调度器资源,用于组织您的调度. 你的AWS账户来了 带有默认调度器组。 排程组总是会添加一个新的排程. 如果您不提供要添加的排程组, 它 将被添加到默认的调度组。 您可以在您的 AWS 账户中创建最多500个调度组 。 可以利用小组组织 从逻辑上,在组颗粒度时访问表度量并管理权限(详见下文). 调度组支持标记 . 使用 EventBridge 调度器,您对调度组应用标记,而不是对单个调度来组织您的资源 .
确定时间表
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.',
});
调度表达式
在配置您的调度时, 您可以从三个调度类型中选择 : 速率计数、 克龙计数和一次性调度 .
按费率计算和按月计算的时间表都是经常性的时间表。 您可以使用一个调度表达式配置每个经常性调度类型.
对于 您可以指定一个时区, EventBridge 调度器将评价表达式.
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',
});
一次性时间表是只引用目标一次的时间表. 您通过指定一天的时间、日期来配置一个一次性时间表, 和用于评估时间表的时区.
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',
});
分组时间表
您的 AWS 账户带有默认调度器组 。 您可以使用 :
const defaultScheduleGroup = ScheduleGroup.fromDefaultScheduleGroup(this, "DefaultGroup");
您可以在您管理的自定义调度组中添加一个调度 。 如果未指定自定义组,则会添加到默认组中.
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,
});
禁用时间表
默认情况下, 将会启用一个调度 。 您可以通过设置 enabled 属性改为虚假 :
declare const target: targets.LambdaInvoke;
new Schedule(this, 'Schedule', {
schedule: ScheduleExpression.rate(Duration.minutes(10)),
target: target,
enabled: false,
});
配置时间表的起止时间
如果您选择了经常性时间表, 您可以通过指定 start 财务报告和审定财务报表 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'),
});
调度器目标
这个 @aws-cdk/aws-scheduler-targets-alpha 模块包括执行 IScheduleTarget 接口
各种AWS服务。 EventBridge调度器支持两类目标:
- 模板目标 援引共同API的 跨核心服务组的业务,以及
- 普遍目标 您可以自定义以调用更多 270多项服务的6 000多项业务.
所支持的目标清单可见于 @aws-cdk/aws-scheduler-targets-alpha。 。 。 .
投入
可以通过自定义输入来引用目标. 这个 ScheduleTargetInput 类支持自由形式文本输入和JSON格式对象输入:
const input = ScheduleTargetInput.fromObject({
'QueueName': 'MyQueue'
});
您可以在目标有效载荷中包含上下文属性。 EventBridge 调度器将替换每个关键字为 其价值和交付目标。 见 完整支持的上下文属性列表数字 :
ContextAttribute.scheduleArn()– 时刻表的ARN.ContextAttribute.scheduledTime()- 您指定时间表的时间以引用目标, 例如 2022-03-22T18: 59: 43Z .ContextAttribute.executionId()- Event Bridge 调度器为每次尝试引用目标而指定的独有的ID,例如 d32c5kddcf5bb8c3.ContextAttribute.attemptNumber()- 用于确定当前引用的尝试编号的计数器,例如 1.
const text = `Attempt number: ${ContextAttribute.attemptNumber}`;
const input = ScheduleTargetInput.fromText(text);
指定执行角色
执行角色是事件Bridge调度器为代表您与其它AWS服务互动而承担的IAM角色.
模板化时间表目标的类自动创建了IAM角色,并具有所有最低限度的必要
与模板目标交互的权限。 如果您希望指定自己的 IAM 角色, 那么模板化的目标
将给予最低要求的许可。 例如 LambdaInvoke 目标将给予
IAM执行角色 lambda:InvokeFunction 允许引用 Lambda 函数.
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,
});
指定加密密钥
EventBridge 调度器与 AWS 密钥管理服务(AWS KMS)整合,使用 AWS KMS 密钥加密并解密您的数据. EventBridge调度器支持两种类型的KMS密钥:AWS自有密钥和客户所管理的密钥.
默认情况下,调度器中的所有事件都会用AWS拥有和管理的密钥加密.
如果您也希望您能够提供客户管理的密钥, 以加密和解密您的日程交付给目标的有效载荷 key 属性.
目标类会自动添加 AWS kms:Decrypt 允许您执行任务 .
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,
});
见 亚马逊事件Bridge调度器中的数据保护 更多细节.
配置灵活时间窗口
您可以通过指定时间窗口来配置灵活的时间窗口 timeWindow 属性.
弹性时间窗口默认被禁用 .
declare const target: targets.LambdaInvoke;
const schedule = new Schedule(this, 'Schedule', {
schedule: ScheduleExpression.rate(Duration.hours(12)),
target,
timeWindow: TimeWindow.flexible(Duration.hours(10)),
});
见 配置灵活时间窗口 更多细节.
处理错误
当事件Bridge调度器无法发送事件时, 您可以配置您的调度处理失败的方式 通过使用两种主要机制,即重试政策和死字母队列(DLQ),成功实现目标.
重试策略决定 EventBridge 调度器必须重试失败事件的次数, 以及时间长度 以保留未处理的事件.
DLQ 是一个标准的 Amazon SQS 队列 EventBridge 排程器用于将失败的事件发送到重试后 政策已经用尽。 您可以使用 DLQ 来排除与您的日程或下游目标相关的故障 . 如果您已经为您的日程安排配置了重试策略, EventBridge 调度器会在之后发送死信事件 用尽重试策略中设置的最大重试次数.
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
});
监测
您可以使用收集原始数据的 Cloud Watch 监视 Amazon EventBridge 调度器 并将其加工成可读的、接近实时的度量衡。 事件Bridge 调度器发射 所有时间表的一套衡量标准,以及时间表的一套额外衡量标准 有相关的死字队列( DLQ) 。 如果您为您的日程配置了 DLQ, EventBridge 排程器在您的排程耗尽其重试策略时会发布额外的度量.
所有时间表的计量
这个 Schedule 类提供了静态方法,用于访问带有默认配置的所有调度度量,例如 metricAllErrors 用于执行目标时查看错误.
new cloudwatch.Alarm(this, 'SchedulesErrorAlarm', {
metric: Schedule.metricAllErrors(),
threshold: 0,
evaluationPeriods: 1,
});
调度组的计量
要查看特定组的参数, 您可以在类上使用方法 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
});
见完整的衡量尺度清单及其描述,载于 利用云表量表进行监测 输入 AWS 事件Bridge 调度器用户指南。 。 。 .