

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

# 為 Amazon ECR 來源 (CLI) 建立 EventBridge 規則
<a name="create-cwe-ecr-source-cli"></a>

呼叫 **put-rule** 命令，並指定：
+ 可唯一識別您所建立規則的名稱。此名稱在您使用與 AWS 帳戶相關聯的 CodePipeline 建立的所有管道中必須是唯一的。
+ 規則所使用來源和詳細資訊欄位的事件模式。如需詳細資訊，請參閱 [Amazon EventBridge 和事件模式](https://docs.aws.amazon.com/eventbridge/latest/userguide/eb-events.html)。

**以 Amazon ECR 做為事件來源和 CodePipeline 做為目標來建立 EventBridge 規則**

1. 新增 EventBridge 使用 CodePipeline 叫用規則的許可。如需詳細資訊，請參閱[使用 Amazon EventBridge 的資源型政策](https://docs.aws.amazon.com/eventbridge/latest/userguide/eb-use-resource-based.html)。

   1. 使用下列範例建立信任政策，允許 EventBridge 擔任服務角色。將信任政策命名為 `trustpolicyforEB.json`。

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

****  

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

------

   1. 使用下列命令來建立 `Role-for-MyRule` 角色，並連接信任政策。

      ```
      aws iam create-role --role-name Role-for-MyRule --assume-role-policy-document file://trustpolicyforEB.json
      ```

   1. 為名為 `MyFirstPipeline` 的管道建立許可政策 JSON，如這個範例所示。將許可政策命名為 `permissionspolicyforEB.json`。

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

****  

      ```
      {
          "Version":"2012-10-17",		 	 	 
          "Statement": [
              {
                  "Effect": "Allow",
                  "Action": [
                      "codepipeline:StartPipelineExecution"
                  ],
                  "Resource": [
                      "arn:aws:codepipeline:us-west-2:111122223333:MyFirstPipeline"
                  ]
              }
          ]
      }
      ```

------

   1. 使用執行以下命令，將 `CodePipeline-Permissions-Policy-for-EB` 許可政策連接到 `Role-for-MyRule` 角色。

      **為什麼我會做出此變更？** 將此政策新增至角色會建立 EventBridge 的許可。

      ```
      aws iam put-role-policy --role-name Role-for-MyRule --policy-name CodePipeline-Permissions-Policy-For-EB --policy-document file://permissionspolicyforEB.json
      ```

1. 呼叫 **put-rule** 命令，並包含 `--name`、`--event-pattern` 和 `--role-arn` 參數。

   **為什麼我會做出此變更？** 您必須使用指定影像推送方式的規則建立事件，以及指定要由事件啟動之管道的目標。

   以下範例命令會建立名為 `MyECRRepoRule` 的規則。

   ```
   aws events put-rule --name "MyECRRepoRule" --event-pattern "{\"detail-type\":[\"ECR Image Action\"],\"source\":[\"aws.ecr\"],\"detail\":{\"action-type\":[\"PUSH\"],\"image-tag\":[\"latest\"],\"repository-name\":[\"eb-test\"],\"result\":[\"SUCCESS\"]}}}" --role-arn "arn:aws:iam::ACCOUNT_ID:role/Role-for-MyRule"
   ```
**注意**  
若要檢視 Amazon ECR 事件支援的完整事件模式，請參閱 [Amazon ECR Events 和 EventBridge](https://docs.aws.amazon.com/AmazonECR/latest/userguide/ecr-eventbridge.html) 或 [Amazon Elastic Container Registry Events](https://docs.aws.amazon.com/eventbridge/latest/userguide/event-types.html#ecr-event-types)。

1. 若要新增 CodePipeline 做為目標，請呼叫 **put-targets**命令並包含下列參數：
   + `--rule` 參數與您使用 **put-rule** 所建立的 `rule_name` 搭配使用。
   + `--targets` 參數與目標清單中目標的清單 `Id` 和目標管道的 `ARN` 搭配使用。

   以下命令範例指定名為 `MyECRRepoRule` 的規則，該目標 `Id` 是由數字 1 組成，指出在規則的目標清單中，這是目標 1。這個範例命令也會指定管線範例 `Arn`，以及規則範例 `RoleArn`。儲存庫中若發生變更，管道就會啟動。

   ```
   aws events put-targets --rule MyECRRepoRule --targets Id=1,Arn=arn:aws:codepipeline:us-west-2:80398EXAMPLE:TestPipeline,RoleArn=arn:aws:iam::80398EXAMPLE:role/Role-for-MyRule
   ```

1. （選用） 若要為特定映像 ID 設定具有來源覆寫的輸入轉換器，請在 CLI 命令中使用下列 JSON。下列範例會設定覆寫，其中：
   + 在此`actionName``Source`範例中， 是在管道建立時定義的動態值，不是衍生自來源事件。
   + 在此`revisionType``IMAGE_DIGEST`範例中， 是在管道建立時定義的動態值，不是衍生自來源事件。
   + 此範例中的 `revisionValue`<*revisionValue*> 衍生自來源事件變數。

   ```
   {
       "Rule": "my-rule",
       "Targets": [
           {
               "Id": "MyTargetId",
               "Arn": "ARN",
               "InputTransformer": {
                   "InputPathsMap": {
                       "revisionValue": "$.detail.image-digest"
                   },
                   "InputTemplate": {
                       "sourceRevisions": [
                           {
                               "actionName": "Source",
                               "revisionType": "IMAGE_DIGEST",
                               "revisionValue": "<revisionValue>"
                           }
                       ]
                   }
               }
           }
       ]
   }
   ```