在 中使用 EventBridge 排程器管理以時間為基礎的事件 AWS SAM - AWS Serverless Application Model

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

在 中使用 EventBridge 排程器管理以時間為基礎的事件 AWS SAM

本主題中的內容提供有關什麼是 Amazon EventBridge 排程器、支援 AWS SAM 提供什麼、如何建立排程器事件,以及在建立排程器事件時可以參考的範例的詳細資訊。

什麼是 Amazon EventBridge 排程器?

使用 EventBridge 排程器來排程 AWS SAM 範本中的事件。Amazon EventBridge Scheduler 是一種排程服務,可讓您在所有 AWS 服務中建立、啟動和管理數百萬個事件和任務。此服務對於時間相關事件特別有用。您可以使用它來排程事件和週期性的時間型調用。它還支援一次性事件,以及具有開始和結束時間的速率和時間運算式。

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

中的 EventBridge 排程器支援 AWS SAM

AWS Serverless Application Model (AWS SAM) 範本規格提供簡單的速記語法,可讓您使用 AWS Lambda 和 的 EventBridge 排程器來排程事件 AWS Step Functions。

在 中建立 EventBridge 排程器事件 AWS SAM

ScheduleV2 屬性設定為 AWS SAM 範本中的事件類型,以定義 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 排程器設定無效字母佇列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: Schedule: Type: ScheduleV2 Properties: ScheduleExpression: rate(1 minute) Input: '{"hello": "simple"}'

進一步了解

若要進一步了解如何定義 ScheduleV2 EventBridge 排程器屬性,請參閱:

  • ScheduleV2 適用於 AWS::Serverless::Function

  • ScheduleV2 適用於 AWS::Serverless::StateMachine