

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

# 設定 Amazon EventBridge 排程器
<a name="setting-up"></a>

您必須先完成下列步驟，才能使用 EventBridge 排程器。

**Topics**
+ [註冊 AWS](#setting-up-aws-sign-up)
+ [建立 IAM 使用者](#setting-up-create-iam-user)
+ [使用 受管政策](#setting-up-managed-policies)
+ [設定執行角色](#setting-up-execution-role)
+ [設定目標](#setting-up-target)
+ [後續步驟？](#setting-up-whats-next)

## 註冊 AWS
<a name="setting-up-aws-sign-up"></a>

如果您沒有 AWS 帳戶，請完成下列步驟來建立一個。

**註冊 AWS 帳戶**

1. 開啟 [https://portal.aws.amazon.com/billing/signup](https://portal.aws.amazon.com/billing/signup)。

1. 請遵循線上指示進行。

   部分註冊程序需接收來電或簡訊，並在電話鍵盤輸入驗證碼。

   當您註冊 時 AWS 帳戶，*AWS 帳戶根使用者*會建立 。根使用者有權存取該帳戶中的所有 AWS 服務 和資源。作為安全最佳實務，請將管理存取權指派給使用者，並且僅使用根使用者來執行[需要根使用者存取權的任務](https://docs.aws.amazon.com/IAM/latest/UserGuide/id_root-user.html#root-user-tasks)。

## 建立 IAM 使用者
<a name="setting-up-create-iam-user"></a>

若要建立管理員使用者，請選擇下列其中一個選項。


****  

| 選擇一種管理管理員的方式 | 到 | 根據 | 您也可以 | 
| --- | --- | --- | --- | 
| 在 IAM Identity Center (建議) | 使用短期憑證存取 AWS。這與安全性最佳實務一致。有關最佳實務的資訊，請參閱 *IAM 使用者指南*中的 [IAM 安全最佳實務](https://docs.aws.amazon.com/IAM/latest/UserGuide/best-practices.html#bp-users-federation-idp)。 | 請遵循 AWS IAM Identity Center 使用者指南的[入門](https://docs.aws.amazon.com//singlesignon/latest/userguide/getting-started.html)中的說明。 | 透過在 AWS Command Line Interface 使用者指南中設定 [AWS CLI 以使用 來設定 AWS IAM Identity Center](https://docs.aws.amazon.com//cli/latest/userguide/cli-configure-sso.html)程式設計存取。 | 
| 在 IAM 中 (不建議使用) | 使用長期憑證存取 AWS。 | 請遵循《IAM 使用者指南》中[建立 IAM 使用者以進行緊急存取](https://docs.aws.amazon.com/IAM/latest/UserGuide/getting-started-emergency-iam-user.html) 的指示。 | 請依照《IAM 使用者指南》中的[管理 IAM 使用者的存取金鑰](https://docs.aws.amazon.com//IAM/latest/UserGuide/id_credentials_access-keys.html)設定以程式設計方式存取。 | 

## 使用 受管政策
<a name="setting-up-managed-policies"></a>

在上一個步驟中，您會使用登入資料設定 IAM 使用者來存取您的 AWS 資源。在大多數情況下，若要安全地使用 EventBridge 排程器，建議您建立單獨的使用者、群組或角色，只具有使用 EventBridge 排程器的必要許可。EventBridge 排程器支援下列常見使用案例的受管政策。
+  AmazonEventBridgeSchedulerFullAccess – 使用主控台和 API 授予 EventBridge 排程器的完整存取權。
+  AmazonEventBridgeSchedulerReadOnlyAccess – 授予 EventBridge 排程器的唯讀存取權。

您可以將這些受管政策連接到您的 IAM 主體，就像您在上一個步驟中連接`AdministratorAccess`政策一樣。如需使用身分型 IAM 政策管理 EventBridge 排程器存取權的詳細資訊，請參閱 [在 EventBridge 排程器中使用身分型政策](security_iam_id-based-policy-examples.md)。

## 設定執行角色
<a name="setting-up-execution-role"></a>

*執行角色*是 EventBridge 排程器擔任的 IAM 角色，以便 AWS 服務 代表您與其他 互動。您可以將許可政策連接至此角色，以授予 EventBridge 排程器調用目標的存取權。

 當您使用主控台建立新的[排程時，也可以建立新的](getting-started.md#getting-started-console)執行角色。如果您使用 主控台，EventBridge 排程器會根據您選擇的目標，以 許可代表您建立角色。當 EventBridge 排程器為您建立角色時，角色的信任政策包含[條件索引鍵](https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_policies_condition-keys.html)，以限制哪些委託人可以代表您擔任該角色。這可防止潛在的[混淆代理人安全問題](cross-service-confused-deputy-prevention.md)。

 下列步驟說明如何建立新的執行角色，以及如何授予 EventBridge 排程器叫用目標的存取權。本主題說明熱門範本目標的許可。如需新增其他目標許可的詳細資訊，請參閱 [在 EventBridge 排程器中使用範本目標](managing-targets-templated.md)。

**使用 建立執行角色 AWS CLI**

1. 複製下列擔任角色 JSON 政策，並將其儲存為 `Scheduler-Execution-Role.json`。此信任政策允許 EventBridge 排程器代表您擔任該角色。

------
#### [ JSON ]

****  

   ```
   {
       "Version":"2012-10-17",		 	 	 
       "Statement": [
           {
               "Effect": "Allow",
               "Principal": {
                   "Service": "scheduler.amazonaws.com"
               },
               "Action": "sts:AssumeRole"
           }
       ]
   }
   ```

------
**重要**  
 若要在生產環境中設定執行角色，建議您實作額外的保護措施，以防止混淆代理人問題。如需詳細資訊和範例政策，請參閱 [EventBridge 排程器中的混淆代理人預防](cross-service-confused-deputy-prevention.md)。

1. 從 AWS Command Line Interface (AWS CLI) 輸入下列命令來建立新的角色。`SchedulerExecutionRole` 將 取代為您要提供此角色的名稱。

   ```
   $ aws iam create-role --role-name SchedulerExecutionRole --assume-role-policy-document file://Scheduler-Execution-Role.json
   ```

   如果成功，您會看到下列輸出：

   ```
   {
       "Role": {
           "Path": "/",
           "RoleName": "Scheduler-Execution-Role",
           "RoleId": "BR1L2DZK3K4CTL5ZF9EIL",
           "Arn": "arn:aws:iam::123456789012:role/SchedulerExecutionRole",
           "CreateDate": "2022-03-10T18:45:01+00:00",
           "AssumeRolePolicyDocument": {
               "Version": "2012-10-17",		 	 	 
               "Statement": [
                   {
                       "Effect": "Allow",
                       "Principal": {
                           "Service": "scheduler.amazonaws.com"
                       },
                       "Action": "sts:AssumeRole"
                   }
               ]
           }
       }
   }
   ```

1. 若要建立新的政策以允許 EventBridge 排程器叫用目標，請選擇下列其中一個常見目標。複製 JSON 許可政策並將其儲存為`.json`檔案。

------
#### [ Amazon SQS – SendMessage ]

    以下允許 EventBridge 排程器在您帳戶中的所有 Amazon SQS 佇列上呼叫 `sqs:SendMessage`動作。

------
#### [ JSON ]

****  

   ```
   {
       "Version":"2012-10-17",		 	 	 
       "Statement": [
           {
               "Action": [
                   "sqs:SendMessage"
               ],
               "Effect": "Allow",
               "Resource": "*"
           }
       ]
   }
   ```

------

------
#### [ Amazon SNS – Publish ]

    以下允許 EventBridge 排程器呼叫您帳戶中所有 Amazon SNS 主題`sns:Publish`的動作。

------
#### [ JSON ]

****  

   ```
   {
       "Version":"2012-10-17",		 	 	 
       "Statement": [
           {
               "Action": [
                   "sns:Publish"
               ],
               "Effect": "Allow",
               "Resource": "*"
           }
       ]
   }
   ```

------

------
#### [ Lambda – Invoke ]

    以下允許 EventBridge Scheduler 呼叫您帳戶中所有 Lambda 函數`lambda:InvokeFunction`的動作。

------
#### [ JSON ]

****  

   ```
   {
       "Version":"2012-10-17",		 	 	 
       "Statement": [
           {
               "Action": [
                   "lambda:InvokeFunction"
               ],
               "Effect": "Allow",
               "Resource": "*"
           }
       ]
   }
   ```

------

------

1. 執行下列命令來建立新的許可政策。`PolicyName` 將 取代為您要提供此政策的名稱。

   ```
   $ aws iam create-policy --policy-name PolicyName --policy-document file://PermissionPolicy.json
   ```

   如果成功，您會看到下列輸出。請注意政策 ARN。您可以在下一個步驟中使用此 ARN，將政策連接至我們的執行角色。

   ```
   {
       "Policy": {
           "PolicyName": "PolicyName",
           "CreateDate": "2022-03-015T19:31:18.620Z",
           "AttachmentCount": 0,
           "IsAttachable": true,
           "PolicyId": "ZXR6A36LTYANPAI7NJ5UV",
           "DefaultVersionId": "v1",
           "Path": "/",
           "Arn": "arn:aws:iam::123456789012:policy/PolicyName",
           "UpdateDate": "2022-03-015T19:31:18.620Z"
       }
   }
   ```

1. 執行下列命令，將政策連接至您的執行角色。`your-policy-arn` 以您在上一個步驟中建立的政策 ARN 取代 。`SchedulerExecutionRole` 以執行角色的名稱取代 。

   ```
   $ aws iam attach-role-policy --policy-arn your-policy-arn --role-name SchedulerExecutionRole
   ```

   `attach-role-policy` 操作不會在命令列傳回回應。

## 設定目標
<a name="setting-up-target"></a>

建立 EventBridge 排程器排程之前，您至少需要一個目標才能叫用排程。您可以使用現有的 AWS 資源，或建立新的資源。下列步驟說明如何使用 建立新的標準 Amazon SQS 佇列 CloudFormation。

**建立新的 Amazon SQS 佇列**

1. 複製下列 JSON CloudFormation 範本，並將其儲存為 `Scheduler-Target-SQS.json`。

   ```
   {
      "AWSTemplateFormatVersion": "2010-09-09",
      "Resources": {
         "MyQueue": {
            "Type": "AWS::SQS::Queue",
            "Properties": {
               "QueueName": "MyQueue"
                }
            }
         },
      "Outputs": {
         "QueueName": {
            "Description": "The name of the queue",
            "Value": {
               "Fn::GetAtt": [
                  "MyQueue",
                  "QueueName"
               ]
            }
         },
         "QueueURL": {
            "Description": "The URL of the queue",
            "Value": {
               "Ref": "MyQueue"
            }
         },
         "QueueARN": {
            "Description": "The ARN of the queue",
            "Value": {
               "Fn::GetAtt": [
                  "MyQueue",
                  "Arn"
               ]
            }
         }
      }
   }
   ```

1. 從 AWS CLI執行下列命令，從`Scheduler-Target-SQS.json`範本建立 CloudFormation 堆疊。

   ```
   $ aws cloudformation create-stack --stack-name Scheduler-Target-SQS --template-body file://Scheduler-Target-SQS.json
   ```

   如果成功，您會看到下列輸出：

   ```
   {
       "StackId": "arn:aws:cloudformation:us-west-2:123456789012:stack/Scheduler-Target-SQS/1d2af345-a121-12eb-abc1-012e34567890"
   }
   ```

1. 執行下列命令以檢視 CloudFormation 堆疊的摘要資訊。此資訊包含堆疊的狀態和範本中指定的輸出。

   ```
   $ aws cloudformation describe-stacks --stack-name Scheduler-Target-SQS
   ```

   如果成功，命令會建立 Amazon SQS 佇列並傳回下列輸出：

   ```
   {
       "Stacks": [
           {
               "StackId": "arn:aws:cloudformation:us-west-2:123456789012:stack/Scheduler-Target-SQS/1d2af345-a121-12eb-abc1-012e34567890",
               "StackName": "Scheduler-Target-SQS",
               "CreationTime": "2022-03-17T16:21:29.442000+00:00",
               "RollbackConfiguration": {},
               "StackStatus": "CREATE_COMPLETE",
               "DisableRollback": false,
               "NotificationARNs": [],
               "Outputs": [
                   {
                       "OutputKey": "QueueName",
                       "OutputValue": "MyQueue",
                       "Description": "The name of the queue"
                   },
                   {
                       "OutputKey": "QueueARN",
                       "OutputValue": "arn:aws:sqs:us-west-2:123456789012:MyQueue",
                       "Description": "The ARN of the queue"
                   },
                   {
                       "OutputKey": "QueueURL",
                       "OutputValue": "https://sqs.us-west-2.amazonaws.com/123456789012/MyQueue",
                       "Description": "The URL of the queue"
                   }
               ],
               "Tags": [],
               "EnableTerminationProtection": false,
               "DriftInformation": {
                   "StackDriftStatus": "NOT_CHECKED"
               }
           }
       ]
   }
   ```

   稍後在本指南中，您將使用 的值`QueueARN`，將佇列設定為 EventBridge 排程器的目標。

## 後續步驟？
<a name="setting-up-whats-next"></a>

完成設定步驟後，請使用[入門](getting-started.md)指南來建立您的第一個 EventBridge 排程器排程器並叫用目標。