使用 “ EventBridge调度器” 管理基于时间的事件 AWS SAM - AWS Serverless Application Model

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

使用 “ EventBridge调度器” 管理基于时间的事件 AWS SAM

什么是 Amazon EventBridge 日程安排?

Amazon S EventBridge cheduler 是一项计划服务,允许您在所有 AWS 服务中创建、启动和管理数千万个事件和任务。此服务对于与时间相关的事件特别有用。您可以使用它来安排事件和基于时间的重复调用。它还支持一次性事件以及带有开始和结束时间的速率和 chron 表达式。

要了解有关 Amazon EventBridge 日程安排器的更多信息,请参阅什么是亚马逊 EventBridge 日程安排? 在《EventBridge 日程安排器用户指南》中。

EventBridge 中的调度器支持 AWS SAM

AWS Serverless Application Model (AWS SAM) 模板规范提供了一种简单的简短语法,您可以使用该语法通过 S EventBridge cheduler 为 AWS Lambda 和安排事件。 AWS Step Functions

在中创建 EventBridge 调度器事件 AWS SAM

在 AWS SAM 模板中将该ScheduleV2属性设置为事件类型,以定义您的 EventBridge 日程安排器事件。此属性支持 AWS::Serverless::FunctionAWS::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 资源。

示例

使用定义 EventBridge 调度器事件的基本示例 AWS SAM

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::FunctionScheduleV2

  • 用于 AWS::Serverless::StateMachineScheduleV2