

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

# 為 Amazon ECR 來源建立 EventBridge 規則 (CloudFormation 範本）
<a name="create-cwe-ecr-source-cfn"></a>



若要使用 CloudFormation 建立規則，請使用範本程式碼片段，如下所示。

**更新您的管道 CloudFormation 範本並建立 EventBridge 規則**

1. 在 範本的 下`Resources`，使用 `AWS::IAM::Role` CloudFormation 資源來設定允許事件啟動管道的 IAM 角色。此項目會建立一個使用兩個政策的角色：
   + 第一個政策允許要承擔的角色。
   + 第二個政策提供啟動管道的許可。

   **為什麼我會做出此變更？** 您必須建立 EventBridge 可擔任的角色，才能在我們的管道中開始執行。

------
#### [ YAML ]

   ```
     EventRole:
       Type: AWS::IAM::Role
       Properties:
         AssumeRolePolicyDocument:
           Version: 2012-10-17		 	 	 
           Statement:
             -
               Effect: Allow
               Principal:
                 Service:
                   - events.amazonaws.com
               Action: sts:AssumeRole
         Path: /
         Policies:
           -
             PolicyName: eb-pipeline-execution
             PolicyDocument:
               Version: 2012-10-17		 	 	 
               Statement:
                 -
                   Effect: Allow
                   Action: codepipeline:StartPipelineExecution
                   Resource: !Sub arn:aws:codepipeline:${AWS::Region}:${AWS::AccountId}:${AppPipeline}
   ```

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

   ```
   {
       "EventRole": {
           "Type": "AWS::IAM::Role",
           "Properties": {
               "AssumeRolePolicyDocument": {
                   "Version": "2012-10-17",		 	 	 
                   "Statement": [
                       {
                           "Effect": "Allow",
                           "Principal": {
                               "Service": [
                                   "events.amazonaws.com"
                               ]
                           },
                           "Action": "sts:AssumeRole"
                       }
                   ]
               },
               "Path": "/",
               "Policies": [
                   {
                       "PolicyName": "eb-pipeline-execution",
                       "PolicyDocument": {
                           "Version": "2012-10-17",		 	 	 
                           "Statement": [
                               {
                                   "Effect": "Allow",
                                   "Action": "codepipeline:StartPipelineExecution",
                                   "Resource": {
                                       "Fn::Sub": "arn:aws:codepipeline:${AWS::Region}:${AWS::AccountId}:${AppPipeline}"
                                   }
                               }
                           ]
                       }
                   }
               ]
           }
       }
   }
   ...
   ```

------

1. 在 範本的 下`Resources`，使用 `AWS::Events::Rule` CloudFormation 資源為 Amazon ECR 來源新增 EventBridge 規則。此事件模式會建立監控遞交至儲存庫的事件。當 EventBridge 偵測到儲存庫狀態變更時，規則會在您的目標管道`StartPipelineExecution`上叫用 。

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

   此程式碼片段使用名為 `eb-test` 的映像，並具有 `latest` 標籤。

------
#### [ YAML ]

   ```
   EventRule:
     Type: 'AWS::Events::Rule'
     Properties:
       EventPattern:
         detail:
           action-type: [PUSH]
           image-tag: [latest]
           repository-name: [eb-test]
           result: [SUCCESS]
         detail-type: [ECR Image Action]
         source: [aws.ecr]
       Targets:
         - Arn: !Sub arn:aws:codepipeline:${AWS::Region}:${AWS::AccountId}:${AppPipeline}
           RoleArn: !GetAtt 
             - EventRole
             - Arn
           Id: codepipeline-AppPipeline
   ```

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

   ```
   {
       "EventRule": {
           "Type": "AWS::Events::Rule",
           "Properties": {
               "EventPattern": {
                   "detail": {
                       "action-type": [
                           "PUSH"
                       ],
                       "image-tag": [
                           "latest"
                       ],
                       "repository-name": [
                           "eb-test"
                       ],
                       "result": [
                           "SUCCESS"
                       ]
                   },
                   "detail-type": [
                       "ECR Image Action"
                   ],
                   "source": [
                       "aws.ecr"
                   ]
               },
               "Targets": [
                   {
                       "Arn": {
                           "Fn::Sub": "arn:aws:codepipeline:${AWS::Region}:${AWS::AccountId}:${AppPipeline}"
                       },
                       "RoleArn": {
                           "Fn::GetAtt": [
                               "EventRole",
                               "Arn"
                           ]
                       },
                       "Id": "codepipeline-AppPipeline"
                   }
               ]
           }
       }
   },
   ```

------
**注意**  
若要檢視 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. （選用） 若要為特定映像 ID 設定具有來源覆寫的輸入轉換器，請使用下列 YAML 程式碼片段。下列範例會設定覆寫，其中：
   + 在此`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}}>'
   ```

1. 將更新的範本儲存到本機電腦，然後開啟 CloudFormation 主控台。

1. 選擇您的堆疊，然後選擇 **Create Change Set for Current Stack (建立目前堆疊的變更集)**。

1. 上傳範本，然後檢視 CloudFormation中所列的變更。這些是會針對堆疊進行的變更。您應該會在清單中看到新資源。

1. 選擇 **Execute (執行)**。