

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

# 为 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`上调用该规则。

   **我为何做出此更改？** 您必须创建具有相应规则（指定必须如何进行映像推送）和目标（指定由事件启动的管道）的事件。

   此代码段使用标签为 `latest`、名为 `eb-test` 的映像。

------
#### [ 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 事件和/或 EventBridge](https://docs.aws.amazon.com/AmazonECR/latest/userguide/ecr-eventbridge.html)[亚马逊弹性容器注册表](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. 选择堆栈，然后选择**为当前堆栈创建更改集**。

1. 上传模板，然后查看 CloudFormation中列出的更改。这些是要对堆栈进行的更改。您应在列表中看到新资源。

1. 选择**执行**。