

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

# 为 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 事件和/或 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. 要添加 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>"
                           }
                       ]
                   }
               }
           }
       ]
   }
   ```