

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

# 在 中使用 EventBridge 排程器管理以時間為基礎的事件 AWS SAM
<a name="using-eventbridge-scheduler"></a>

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

## 什麼是 Amazon EventBridge 排程器？
<a name="using-eventbridge-scheduler-intro"></a>

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

若要進一步了解 Amazon EventBridge 排程器，請參閱 [ EventBridge 排程器使用者指南中的什麼是 Amazon EventBridge 排程器？](https://docs.aws.amazon.com/scheduler/latest/UserGuide/what-is-scheduler.html)。 *EventBridge *

**Topics**
+ [什麼是 Amazon EventBridge 排程器？](#using-eventbridge-scheduler-intro)
+ [中的 EventBridge 排程器支援 AWS SAM](#using-eventbridge-scheduler-sam-support)
+ [在 中建立 EventBridge 排程器事件 AWS SAM](#using-eventbridge-scheduler-sam-create)
+ [範例](#using-eventbridge-scheduler-examples)
+ [進一步了解](#using-eventbridge-scheduler-learn)

## 中的 EventBridge 排程器支援 AWS SAM
<a name="using-eventbridge-scheduler-sam-support"></a>

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

## 在 中建立 EventBridge 排程器事件 AWS SAM
<a name="using-eventbridge-scheduler-sam-create"></a>

將 `ScheduleV2` 屬性設定為 AWS SAM 範本中的事件類型，以定義 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 排程器設定無效字母佇列](https://docs.aws.amazon.com/scheduler/latest/UserGuide/configuring-schedule-dlq.html)。 *EventBridge *

指定 DLQ ARN 時， AWS SAM 會設定排程器排程的許可，以傳送訊息至 DLQ。未指定 DLQ ARN 時， AWS SAM 會建立 DLQ 資源。

## 範例
<a name="using-eventbridge-scheduler-examples"></a>

### 使用 定義 EventBridge 排程器事件的基本範例 AWS SAM
<a name="using-eventbridge-scheduler-examples-example1"></a>

```
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"}'
```

## 進一步了解
<a name="using-eventbridge-scheduler-learn"></a>

若要進一步了解如何定義 `ScheduleV2` EventBridge 排程器屬性，請參閱：
+ [ScheduleV2](sam-property-function-schedulev2.md) 適用於 `AWS::Serverless::Function`。
+ [ScheduleV2](sam-property-statemachine-statemachineschedulev2.md) 適用於 `AWS::Serverless::StateMachine`。