为 Amazon ECR 源创建 EventBridge 规则(CloudFormation 模板) - AWS CodePipeline

为 Amazon ECR 源创建 EventBridge 规则(CloudFormation 模板)

要使用 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}" } } ] } } ] } } } ...
  2. 在模板中,在 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 事件和 EventBridgeAmazon Elastic Container Registry 事件

  3. (可选)要为特定映像 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>'
  4. 将更新后的模板保存到本地计算机,然后打开 CloudFormation 控制台。

  5. 选择堆栈,然后选择为当前堆栈创建更改集

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

  7. 选择执行