本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。
本主题中的内容详细介绍了什么是 Amazon EventBridge 调度器、AWS SAM 提供哪些支持、如何创建调度器事件以及创建调度器事件时可以参考的示例。
什么是 Amazon EventBridge 调度器?
使用 EventBridge 调度器在 AWS SAM 模板中计划事件。Amazon EventBridge 调度器是一项计划服务,让您可以在所有 AWS 服务中创建、启动和管理数千万个事件和任务。此服务对于与时间相关的事件特别有用。您可以使用它来计划事件和基于时间的重复调用。它还支持一次性事件以及带有开始和结束时间的 rate 和 chron 表达式。
有关 Amazon EventBridge 调度器的更多信息,请参阅 EventBridge 调度器用户指南中的什么是 Amazon EventBridge 调度器?。
AWS SAM 中的 EventBridge 调度器支持
AWS Serverless Application Model (AWS SAM) 模板规范提供了简单的速记语法,供您用于通过 EventBridge 调度器为 AWS Lambda 和 AWS Step Functions 计划事件。
在 AWS SAM 中创建 EventBridge 调度器事件
在 AWS SAM 模板中将 ScheduleV2
属性设置为事件类型,以定义 EventBridge 调度器事件。此属性支持 AWS::Serverless::Function
和 AWS::Serverless::StateMachine
资源类型。
MyFunction:
Type: AWS::Serverless::Function
Properties:
Events:
CWSchedule:
Type: ScheduleV2
Properties:
ScheduleExpression: 'rate(1 minute)'
Name: TestScheduleV2Function
Description: Test schedule event
MyStateMachine:
Type: AWS::Serverless::StateMachine
Properties:
Events:
CWSchedule:
Type: ScheduleV2
Properties:
ScheduleExpression: 'rate(1 minute)'
Name: TestScheduleV2StateMachine
Description: Test schedule event
EventBridge 调度器事件计划功能还支持未处理事件的死信队列 (DLQ)。有关死信队列的更多信息,请参阅《EventBridge 调度器用户指南》中的为 EventBridge 调度器配置死信队列。
如果已指定 DLQ ARN,AWS SAM 会配置调度器计划向 DLQ 发送消息所需的权限。如果未指定 DLQ ARN,AWS SAM 会创建 DLQ 资源。
示例
基本示例:使用 AWS SAM 定义 EventBridge 调度器事件
Transform: AWS::Serverless-2016-10-31
Resources:
MyLambdaFunction:
Type: AWS::Serverless::Function
Properties:
Handler: index.handler
Runtime: python3.8
InlineCode: |
def handler(event, context):
print(event)
return {'body': 'Hello World!', 'statusCode': 200}
MemorySize: 128
Events:
Schedule:
Type: ScheduleV2
Properties:
ScheduleExpression: rate(1 minute)
Input: '{"hello": "simple"}'
MySFNFunction:
Type: AWS::Serverless::Function
Properties:
Handler: index.handler
Runtime: python3.8
InlineCode: |
def handler(event, context):
print(event)
return {'body': 'Hello World!', 'statusCode': 200}
MemorySize: 128
StateMachine:
Type: AWS::Serverless::StateMachine
Properties:
Type: STANDARD
Definition:
StartAt: MyLambdaState
States:
MyLambdaState:
Type: Task
Resource: !GetAtt MySFNFunction.Arn
End: true
Policies:
- LambdaInvokePolicy:
FunctionName: !Ref MySFNFunction
Events:
Events:
Schedule:
Type: ScheduleV2
Properties:
ScheduleExpression: rate(1 minute)
Input: '{"hello": "simple"}'
了解更多
要了解有关定义 ScheduleV2
EventBridge 调度器属性的更多信息,请参阅:
-
用于
AWS::Serverless::Function
的 ScheduleV2。 -
用于
AWS::Serverless::StateMachine
的 ScheduleV2。