使用 EventBridge排程器管理基於時間的事件 AWS SAM - AWS Serverless Application Model

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

使用 EventBridge排程器管理基於時間的事件 AWS SAM

什麼是 Amazon EventBridge 調度程序?

Amazon EventBridge Scheduler 是一種排程服務,可讓您跨所有服務建立、啟動和管理數千萬個事件和任 AWS 務。此服務對於與時間有關的事件特別有用。您可以使用它來安排事件和基於時間的循環調用。它還支持一次性事件以及帶有開始和結束時間的速率和 chron 表達式。

若要進一步了解 Amazon EventBridge 排程器,請參閱什麼是 Amazon EventBridge 排程器? 在「EventBridge 排程器使用指南」中。

EventBridge 排程器支援 AWS SAM

AWS Serverless Application Model (AWS SAM) 範本規格提供簡單、簡短的語法,您可以使用這些語法來 EventBridge 排程與的 Scheduler 事件。 AWS Lambda AWS Step Functions

建立 EventBridge 排程器事件 AWS SAM

將內ScheduleV2容設定為 AWS SAM 範本中的事件類型,以定義您的 EventBridge Scheduler 事件。此內容支援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 排程器」屬性,請參閱: