AWS CodeCommit and CodeBuild CI/CD Example

Keywords: AWS CodeCommit, Code Commit, CodeBuild, Code Build, CI, CD, CICD

Summary

一套 CI/CD Pipeline 主要包含两个部分, 一个 Git 代码仓库, 一个 CI Job Run 环境.

  • Git Repo:
    • 目的: 开发者在上面进行开发, 保存代码的历史记录. 所有对 Git 的操作, 包括 commit, branch, PR, comment, approve, merge 都会触发 Git Event, 根据这些 event 来决定是否, 如何进行后续的 CI Job Run.

    • 可选的产品: 市场上的 Git 托管产品很多, GitHub, GitLab, BitBucket, AWS CodeCommit 等, AWS CodeCommit 的好处是会将详细的 Git Event 给用户, 让用户自己决定如何对这些 event 做出响应.

  • CI Job Run:
    • 目的: 提供一个轻量的代码运行环境, 能运行自动化脚本, 构建代码, 测试代码, 部署 APP. 这里有基于虚拟机 VM 的, 也有基于容器 Container 的.

    • 可选的产品: 有 GitHub action, CircleCI, AWS CodeBuild 等. AWS CodeBuild 的好处是跟 AWS 结合的比较紧密, 省去了配置跟 AWS 相关的权限的麻烦.

总结下来就是 AWS 的这两个服务的特点是自定义程度极高, 虽然配置起来稍微复杂了点, 但是你有最高的自由度.

本文主要介绍如何用 AWS CodeCommit 和 AWS CodeBuild 这两个产品搭建一个 CI/CD Pipeline.

Architecture

首先我们来看一下架构.

  1. Trigger Build Job: 目前 Git Event 和 Build Job 之间并没有打通. 在 GitHub 上这是通过服务器上的 Web hook 实现的. 原理上就是通过 GitHub 服务器上的 hook 自动化脚本, 将 git event 转化成用户友好的 JSON, 然后选择发送到其他外部系统上, 从而实现自动化. 而 AWS CodeCommit 的内置了 Notification Rule 功能, 能将 Git Event 发送到 SNS Topic, 然后 SNS Topic 触发 AWS Lambda, 由于 Lambda 可以是任何语言任何代码, 基本上能做到任何事情, 这就实现了 AWS CodeCommit 和任何外部系统的打通. 当然本文主要说的是与 CodeBuild 之间的打通.

  2. Post Build Job Automation: CodeBuild Job 本身也有 Notification Rule 功能, 能将例如 Start, Failed, Success 以及每个 Phase 的开始结束的这些 event 发送到 SNS. 和之前一样, 我们可以用 AWS Lambda 对其进行处理, 从而实现自动通知, 报警等功能.

  3. Artifacts: Build Job Run 的输出有 test report 结果. 这些结果可以被发送到办公聊天软件中自动通报. 并且 build 的成功失败消息也能被发送到聊天软件中. 另外 Build Job Run 的输出还包括 Artifacts, 以供后续的部署. 而这些 Artifacts 则可以保存到 S3 中.

当代码 Push 到 CodeCommit 时, 如何自动触发 CodeBuild?

AWS CodeBuild 是 AWS 提供的 CI 持续集成服务, 能从 Git 服务器上拉取代码执行自动构建 / 测试 / 部署. 你可能已经使用过 GitHub 以及各种免费的 CI 系统例如 travis ci, circle ci, github action. 这些 CI 和 GitHub 的集成原理是 Webhook, 也就是每次 GitHub 收到 Push, Merge, Commit, Create Branch, Create Pull Request 之后, 后台都会生成一个 Event, 然后通过 webhook 发送给这些 CI 系统的服务器. 这些 CI 系统把常用的这些 event 集成好了, 只需要用图形界面点几下就可以在 Push 代码后自动 Build 了.

AWS CodeCommit 也有 event, 你在 Console -> CodeCommit -> Repositories -> Notify -> Create Notification Rule 下面可以看到所支持的 Event 的列表.

  • Comments
    • On commits

    • On pull requests Approvals

  • Status changed
    • Rule override

    • Pull request

  • Source updated
    • Created

    • Status changed

    • Merged

  • Branches and tags
    • Created

    • Deleted

    • Updated

你可以将这些 Event 发送到 SNS topic, 然后用 SNS topic trigger AWS Lambda, 对这些 event 进行分析过滤, 看到符合条件的 event 就用它来 trigger 一个 AWS CodeBuild 即可. 这里要注意的是, 你的 SNS topic 的 access policy 里 Policy 需要是 "Service": "codestar-notifications.amazonaws.com", 还有因为 SNS 自动创建的 Access Policy 会根据你创建 Topic 的时候用的 IAM User/Role 自动给予你自己权限, 所以会有一段 "Condition": ... 的定义, 这将阻止 codestar-notification 调用 SNS:Publish API, 所以一定要去掉. 下面有一个例子:

{
    "Version": "2008-10-17",
    "Statement": [
        {
            "Sid": "CodeNotification_publish",
            "Effect": "Allow",
            "Principal": {
                "Service": "codestar-notifications.amazonaws.com"
            },
            "Action": "SNS:Publish",
            "Resource": "arn:aws:sns:us-east-2:111122223333:${your-sns-topic-name}"
        }
    ]
}

本质上来说这个 Trigger 的规则是由 AWS 用户自己实现的, 而不是像 circle ci 等一样由 ci 平台托管的. 虽然带来了额外工作, 但是给予了用户最大的权限和开放度能自定义想要的 CI 流程. 可以适应任何复杂的企业级项目管理流程.

这里列出了常见的 Event 所对应的 SNS Notification Message (不是 Lambda event, 只是 notification 的 string message 被 json 解析后的形式)

approval-message.json:

{
    "account": "669508176277",
    "detailType": "CodeCommit Pull Request State Change",
    "region": "us-east-2",
    "source": "aws.codecommit",
    "time": "2022-05-21T16:00:18Z",
    "notificationRuleArn": "arn:aws:codestar-notifications:us-east-2:669508176277:notificationrule/b4b0b3a068d4f7add4ed0e7c965b88cd1ba26af2",
    "detail": {
        "approvalStatus": "APPROVE",
        "sourceReference": "refs/heads/f2",
        "lastModifiedDate": "Sat May 21 15:56:38 UTC 2022",
        "author": "arn:aws:iam::669508176277:user/sanhe",
        "isMerged": "False",
        "pullRequestStatus": "Open",
        "notificationBody": "A pull request event occurred in the following AWS CodeCommit repository: learn_codebuild. User: arn:aws:sts::669508176277:assumed-role/sanhe-assume-role-for-iam-test/sanhe. Event: Updated. Pull request name: 2. Additional information: A user has changed their approval state for the pull request. State change: APPROVE. For more information, go to the AWS CodeCommit console https://us-east-2.console.aws.amazon.com/codesuite/codecommit/repositories/learn_codebuild/pull-requests/2?region=us-east-2.",
        "destinationReference": "refs/heads/master",
        "callerUserArn": "arn:aws:sts::669508176277:assumed-role/sanhe-assume-role-for-iam-test/sanhe",
        "creationDate": "Sat May 21 15:50:51 UTC 2022",
        "pullRequestId": "2",
        "title": "f2",
        "revisionId": "2f21662363cd735c44f3e9ef66c62a2bb157a4c7049edf6d021c6d87bcce7f22",
        "repositoryNames": [
            "learn_codebuild"
        ],
        "destinationCommit": "ad4def2926fb6e8873d036b5c1fcd369e9b06687",
        "event": "pullRequestApprovalStateChanged",
        "sourceCommit": "6b1b688dd10c83db424d62790891855ac6c9968d"
    },
    "resources": [
        "arn:aws:codecommit:us-east-2:669508176277:learn_codebuild"
    ],
    "additionalAttributes": {}
}

comment-to-pull-request-message.json:

{
    "account": "669508176277",
    "detailType": "CodeCommit Comment on Pull Request",
    "region": "us-east-2",
    "source": "aws.codecommit",
    "time": "2022-05-21T15:54:58Z",
    "notificationRuleArn": "arn:aws:codestar-notifications:us-east-2:669508176277:notificationrule/b4b0b3a068d4f7add4ed0e7c965b88cd1ba26af2",
    "detail": {
        "beforeCommitId": "ad4def2926fb6e8873d036b5c1fcd369e9b06687",
        "notificationBody": "A pull request event occurred in the following AWS CodeCommit repository: learn_codebuild. The user: arn:aws:iam::669508176277:user/sanhe made a comment or replied to a comment. The comment was made on the following Pull Request: 2. For more information, go to the AWS CodeCommit console https://us-east-2.console.aws.amazon.com/codesuite/codecommit/repositories/learn_codebuild/pull-requests/2/activity#ec20a16a-8a59-428a-8cf6-21e13f9e1905%3A4da0c6fa-4f25-4f05-ae9a-257abab10b9a?region=us-east-2",
        "repositoryId": "8cf05543-b91f-4e87-8a1b-3a7feda723c4",
        "commentId": "ec20a16a-8a59-428a-8cf6-21e13f9e1905:4da0c6fa-4f25-4f05-ae9a-257abab10b9a",
        "afterCommitId": "6b1b688dd10c83db424d62790891855ac6c9968d",
        "callerUserArn": "arn:aws:iam::669508176277:user/sanhe",
        "event": "commentOnPullRequestCreated",
        "pullRequestId": "2",
        "repositoryName": "learn_codebuild"
    },
    "resources": [
        "arn:aws:codecommit:us-east-2:669508176277:learn_codebuild"
    ],
    "additionalAttributes": {
        "commentedLine": null,
        "resourceArn": "arn:aws:codecommit:us-east-2:669508176277:learn_codebuild",
        "comments": [
            {
                "authorArn": "arn:aws:iam::669508176277:user/sanhe",
                "commentText": "My comment"
            }
        ],
        "commentedLineNumber": null,
        "filePath": null
    }
}

commit-to-branch-message.json:

{
    "account": "669508176277",
    "detailType": "CodeCommit Repository State Change",
    "region": "us-east-2",
    "source": "aws.codecommit",
    "time": "2022-05-21T15:43:27Z",
    "notificationRuleArn": "arn:aws:codestar-notifications:us-east-2:669508176277:notificationrule/b4b0b3a068d4f7add4ed0e7c965b88cd1ba26af2",
    "detail": {
        "referenceFullName": "refs/heads/f2",
        "repositoryId": "8cf05543-b91f-4e87-8a1b-3a7feda723c4",
        "referenceType": "branch",
        "commitId": "8e2d5964dad30b67fc92c2604d5d0616f1a9fba6",
        "callerUserArn": "arn:aws:iam::669508176277:user/sanhe",
        "event": "referenceUpdated",
        "repositoryName": "learn_codebuild",
        "oldCommitId": "222f6054f137a5b3b64e838da2ce50ddf395cfdb",
        "referenceName": "f2"
    },
    "resources": [
        "arn:aws:codecommit:us-east-2:669508176277:learn_codebuild"
    ],
    "additionalAttributes": {}
}

commit-to-master-message.json:

{
    "account": "669508176277",
    "detailType": "CodeCommit Repository State Change",
    "region": "us-east-2",
    "source": "aws.codecommit",
    "time": "2022-05-21T15:35:52Z",
    "notificationRuleArn": "arn:aws:codestar-notifications:us-east-2:669508176277:notificationrule/0d060b4676ba9c289413cb9ec3a005095eb06970",
    "detail": {
        "referenceFullName": "refs/heads/master",
        "repositoryId": "8cf05543-b91f-4e87-8a1b-3a7feda723c4",
        "referenceType": "branch",
        "commitId": "ad4def2926fb6e8873d036b5c1fcd369e9b06687",
        "callerUserArn": "arn:aws:iam::669508176277:user/sanhe",
        "event": "referenceUpdated",
        "repositoryName": "learn_codebuild",
        "oldCommitId": "d24c305f0dd3ba47fea7a8895b1753486f53d896",
        "referenceName": "master"
    },
    "resources": [
        "arn:aws:codecommit:us-east-2:669508176277:learn_codebuild"
    ],
    "additionalAttributes": {}
}

commit-to-pull-request-branch-message.json:

{
    "account": "669508176277",
    "detailType": "CodeCommit Pull Request State Change",
    "region": "us-east-2",
    "source": "aws.codecommit",
    "time": "2022-05-21T15:53:12Z",
    "notificationRuleArn": "arn:aws:codestar-notifications:us-east-2:669508176277:notificationrule/b4b0b3a068d4f7add4ed0e7c965b88cd1ba26af2",
    "detail": {
        "sourceReference": "refs/heads/f2",
        "lastModifiedDate": "Sat May 21 15:53:07 UTC 2022",
        "author": "arn:aws:iam::669508176277:user/sanhe",
        "isMerged": "False",
        "pullRequestStatus": "Open",
        "notificationBody": "A pull request event occurred in the following AWS CodeCommit repository: learn_codebuild. User: arn:aws:iam::669508176277:user/sanhe. Event: Updated. Pull request name: 2. Additional information: The pull request was updated with one or more commits to the source branch: refs/heads/f2. For more information, go to the AWS CodeCommit console https://us-east-2.console.aws.amazon.com/codesuite/codecommit/repositories/learn_codebuild/pull-requests/2?region=us-east-2.",
        "destinationReference": "refs/heads/master",
        "callerUserArn": "arn:aws:iam::669508176277:user/sanhe",
        "creationDate": "Sat May 21 15:50:51 UTC 2022",
        "pullRequestId": "2",
        "title": "f2",
        "revisionId": "2f21662363cd735c44f3e9ef66c62a2bb157a4c7049edf6d021c6d87bcce7f22",
        "repositoryNames": [
            "learn_codebuild"
        ],
        "destinationCommit": "ad4def2926fb6e8873d036b5c1fcd369e9b06687",
        "event": "pullRequestSourceBranchUpdated",
        "sourceCommit": "6b1b688dd10c83db424d62790891855ac6c9968d"
    },
    "resources": [
        "arn:aws:codecommit:us-east-2:669508176277:learn_codebuild"
    ],
    "additionalAttributes": {}
}

create-branch-message.json:

{
    "account": "669508176277",
    "detailType": "CodeCommit Repository State Change",
    "region": "us-east-2",
    "source": "aws.codecommit",
    "time": "2022-05-21T15:38:31Z",
    "notificationRuleArn": "arn:aws:codestar-notifications:us-east-2:669508176277:notificationrule/0d060b4676ba9c289413cb9ec3a005095eb06970",
    "detail": {
        "referenceFullName": "refs/heads/f2",
        "repositoryId": "8cf05543-b91f-4e87-8a1b-3a7feda723c4",
        "referenceType": "branch",
        "commitId": "ad4def2926fb6e8873d036b5c1fcd369e9b06687",
        "callerUserArn": "arn:aws:iam::669508176277:user/sanhe",
        "event": "referenceCreated",
        "repositoryName": "learn_codebuild",
        "referenceName": "f2"
    },
    "resources": [
        "arn:aws:codecommit:us-east-2:669508176277:learn_codebuild"
    ],
    "additionalAttributes": {}
}

create-pull-request-message.json:

{
    "account": "669508176277",
    "detailType": "CodeCommit Pull Request State Change",
    "region": "us-east-2",
    "source": "aws.codecommit",
    "time": "2022-05-21T15:51:02Z",
    "notificationRuleArn": "arn:aws:codestar-notifications:us-east-2:669508176277:notificationrule/b4b0b3a068d4f7add4ed0e7c965b88cd1ba26af2",
    "detail": {
        "sourceReference": "refs/heads/f2",
        "lastModifiedDate": "Sat May 21 15:50:51 UTC 2022",
        "author": "arn:aws:iam::669508176277:user/sanhe",
        "isMerged": "False",
        "pullRequestStatus": "Open",
        "notificationBody": "A pull request event occurred in the following AWS CodeCommit repository: learn_codebuild. User: arn:aws:iam::669508176277:user/sanhe. Event: Created. Pull request name: 2. Additional information: A pull request was created with the following ID: 2. The title of the pull request is: f2. For more information, go to the AWS CodeCommit console https://us-east-2.console.aws.amazon.com/codesuite/codecommit/repositories/learn_codebuild/pull-requests/2?region=us-east-2.",
        "destinationReference": "refs/heads/master",
        "callerUserArn": "arn:aws:iam::669508176277:user/sanhe",
        "creationDate": "Sat May 21 15:50:51 UTC 2022",
        "pullRequestId": "2",
        "title": "f2",
        "revisionId": "8a5884301e95f115a66cda8863542e105612ad5ba3d921fdb2827ba9dede4d29",
        "repositoryNames": [
            "learn_codebuild"
        ],
        "destinationCommit": "ad4def2926fb6e8873d036b5c1fcd369e9b06687",
        "event": "pullRequestCreated",
        "sourceCommit": "8e2d5964dad30b67fc92c2604d5d0616f1a9fba6"
    },
    "resources": [
        "arn:aws:codecommit:us-east-2:669508176277:learn_codebuild"
    ],
    "additionalAttributes": {
        "numberOfFilesAdded": "0",
        "numberOfFilesDeleted": "0",
        "numberOfFilesModified": "1",
        "changedFiles": [
            {
                "changeType": "M",
                "filePath": "README.rst"
            }
        ]
    }
}

merge-to-master-message.json:

{
    "account": "669508176277",
    "detailType": "CodeCommit Repository State Change",
    "region": "us-east-2",
    "source": "aws.codecommit",
    "time": "2022-05-21T16:01:29Z",
    "notificationRuleArn": "arn:aws:codestar-notifications:us-east-2:669508176277:notificationrule/b4b0b3a068d4f7add4ed0e7c965b88cd1ba26af2",
    "detail": {
        "referenceFullName": "refs/heads/master",
        "referenceType": "branch",
        "commitId": "6b1b688dd10c83db424d62790891855ac6c9968d",
        "callerUserArn": "arn:aws:iam::669508176277:user/sanhe",
        "repositoryName": "learn_codebuild",
        "oldCommitId": "ad4def2926fb6e8873d036b5c1fcd369e9b06687",
        "destinationCommitId": "ad4def2926fb6e8873d036b5c1fcd369e9b06687",
        "repositoryId": "8cf05543-b91f-4e87-8a1b-3a7feda723c4",
        "sourceCommitId": "6b1b688dd10c83db424d62790891855ac6c9968d",
        "event": "referenceUpdated",
        "mergeOption": "FAST_FORWARD_MERGE",
        "referenceName": "master"
    },
    "resources": [
        "arn:aws:codecommit:us-east-2:669508176277:learn_codebuild"
    ],
    "additionalAttributes": {}
}

reply-to-comment-message.json:

{
    "account": "669508176277",
    "detailType": "CodeCommit Comment on Pull Request",
    "region": "us-east-2",
    "source": "aws.codecommit",
    "time": "2022-05-21T15:56:48Z",
    "notificationRuleArn": "arn:aws:codestar-notifications:us-east-2:669508176277:notificationrule/b4b0b3a068d4f7add4ed0e7c965b88cd1ba26af2",
    "detail": {
        "beforeCommitId": "ad4def2926fb6e8873d036b5c1fcd369e9b06687",
        "inReplyTo": "ec20a16a-8a59-428a-8cf6-21e13f9e1905:4da0c6fa-4f25-4f05-ae9a-257abab10b9a",
        "notificationBody": "A pull request event occurred in the following AWS CodeCommit repository: learn_codebuild. The user: arn:aws:iam::669508176277:user/sanhe made a comment or replied to a comment. The comment was made on the following Pull Request: 2. For more information, go to the AWS CodeCommit console https://us-east-2.console.aws.amazon.com/codesuite/codecommit/repositories/learn_codebuild/pull-requests/2/activity#ec20a16a-8a59-428a-8cf6-21e13f9e1905%3Afac1aad9-f376-4894-8b6a-58991671b124?region=us-east-2",
        "repositoryId": "8cf05543-b91f-4e87-8a1b-3a7feda723c4",
        "commentId": "ec20a16a-8a59-428a-8cf6-21e13f9e1905:fac1aad9-f376-4894-8b6a-58991671b124",
        "afterCommitId": "6b1b688dd10c83db424d62790891855ac6c9968d",
        "callerUserArn": "arn:aws:iam::669508176277:user/sanhe",
        "event": "commentOnPullRequestCreated",
        "pullRequestId": "2",
        "repositoryName": "learn_codebuild"
    },
    "resources": [
        "arn:aws:codecommit:us-east-2:669508176277:learn_codebuild"
    ],
    "additionalAttributes": {
        "commentedLine": null,
        "resourceArn": "arn:aws:codecommit:us-east-2:669508176277:learn_codebuild",
        "comments": [
            {
                "authorArn": "arn:aws:iam::669508176277:user/sanhe",
                "commentText": "My comment"
            },
            {
                "authorArn": "arn:aws:iam::669508176277:user/sanhe",
                "commentText": "Whats up"
            }
        ],
        "commentedLineNumber": null,
        "filePath": null
    }
}

rule-override-message.json:

{
    "account": "669508176277",
    "detailType": "CodeCommit Pull Request State Change",
    "region": "us-east-2",
    "source": "aws.codecommit",
    "time": "2022-05-21T15:58:42Z",
    "notificationRuleArn": "arn:aws:codestar-notifications:us-east-2:669508176277:notificationrule/b4b0b3a068d4f7add4ed0e7c965b88cd1ba26af2",
    "detail": {
        "sourceReference": "refs/heads/f2",
        "lastModifiedDate": "Sat May 21 15:56:38 UTC 2022",
        "author": "arn:aws:iam::669508176277:user/sanhe",
        "isMerged": "False",
        "pullRequestStatus": "Open",
        "notificationBody": "A pull request event occurred in the following AWS CodeCommit repository: learn_codebuild. User: arn:aws:iam::669508176277:user/sanhe. Event: Updated. Pull request name: 2. Additional information: An override event has occurred for the approval rules for this pull request. Override status: OVERRIDE. For more information, go to the AWS CodeCommit console https://us-east-2.console.aws.amazon.com/codesuite/codecommit/repositories/learn_codebuild/pull-requests/2?region=us-east-2.",
        "destinationReference": "refs/heads/master",
        "callerUserArn": "arn:aws:iam::669508176277:user/sanhe",
        "creationDate": "Sat May 21 15:50:51 UTC 2022",
        "pullRequestId": "2",
        "title": "f2",
        "revisionId": "2f21662363cd735c44f3e9ef66c62a2bb157a4c7049edf6d021c6d87bcce7f22",
        "repositoryNames": [
            "learn_codebuild"
        ],
        "destinationCommit": "ad4def2926fb6e8873d036b5c1fcd369e9b06687",
        "event": "pullRequestApprovalRuleOverridden",
        "sourceCommit": "6b1b688dd10c83db424d62790891855ac6c9968d",
        "overrideStatus": "OVERRIDE"
    },
    "resources": [
        "arn:aws:codecommit:us-east-2:669508176277:learn_codebuild"
    ],
    "additionalAttributes": {}
}

AWS CodeCommit Lambda Trigger

熟悉 Lambda Trigger 的开发者可能会发现 AWS Lambda 的 Trigger 里有 CodeCommit 的选项. 里面只支持三种 event:

  • Create branch or tag

  • Push to existing branch

  • Delete branch or tag

这可以理解为一个简化版的 event trigger. 只有在代码实实在在发生改变, 产生了新的 commit 或是 tag 的时候才会触发. 而像是工作流: create Pull Request 则是不会触发 build 的. 这适合个人开发者单独维护一个代码库的情况.

不同的 event 以及对应的 json 的参考:

commit-to-branch.json:

{
    "Records": [
        {
            "awsRegion": "us-east-2",
            "codecommit": {
                "references": [
                    {
                        "commit": "b741de430b1febc1998217456b4cd0845b3c1865",
                        "ref": "refs/heads/f1/act1"
                    }
                ]
            },
            "customData": null,
            "eventId": "c027e758-2fe2-4cf2-a4c9-1b79513d7b88",
            "eventName": "ReferenceChanges",
            "eventPartNumber": 1,
            "eventSource": "aws:codecommit",
            "eventSourceARN": "arn:aws:codecommit:us-east-2:111122223333:learn_codebuild",
            "eventTime": "2022-05-21T14:09:45.539+0000",
            "eventTotalParts": 1,
            "eventTriggerConfigId": "8ac5c8d3-2831-4974-97f2-fa08be2e6c82",
            "eventTriggerName": "codecommit-learn_codebuild-trigger",
            "eventVersion": "1.0",
            "userIdentityARN": "arn:aws:iam::111122223333:user/sanhe"
        }
    ]
}

commit-to-master.json:

{
    "Records": [
        {
            "awsRegion": "us-east-2",
            "codecommit": {
                "references": [
                    {
                        "commit": "8abc50790a8c99e9377437028516202e55700659",
                        "ref": "refs/heads/master"
                    }
                ]
            },
            "customData": null,
            "eventId": "eda5c8af-4b33-4a8d-b879-05bd785bb943",
            "eventName": "ReferenceChanges",
            "eventPartNumber": 1,
            "eventSource": "aws:codecommit",
            "eventSourceARN": "arn:aws:codecommit:us-east-2:111122223333:learn_codebuild",
            "eventTime": "2022-05-21T14:05:31.227+0000",
            "eventTotalParts": 1,
            "eventTriggerConfigId": "8ac5c8d3-2831-4974-97f2-fa08be2e6c82",
            "eventTriggerName": "codecommit-learn_codebuild-trigger",
            "eventVersion": "1.0",
            "userIdentityARN": "arn:aws:iam::111122223333:user/sanhe"
        }
    ]
}

create-branch.json:

{
    "Records": [
        {
            "awsRegion": "us-east-2",
            "codecommit": {
                "references": [
                    {
                        "commit": "8abc50790a8c99e9377437028516202e55700659",
                        "created": true,
                        "ref": "refs/heads/f1/act1"
                    }
                ]
            },
            "customData": null,
            "eventId": "2d9253a5-be24-49e2-952a-747a73dc5af5",
            "eventName": "ReferenceChanges",
            "eventPartNumber": 1,
            "eventSource": "aws:codecommit",
            "eventSourceARN": "arn:aws:codecommit:us-east-2:111122223333:learn_codebuild",
            "eventTime": "2022-05-21T14:07:10.995+0000",
            "eventTotalParts": 1,
            "eventTriggerConfigId": "8ac5c8d3-2831-4974-97f2-fa08be2e6c82",
            "eventTriggerName": "codecommit-learn_codebuild-trigger",
            "eventVersion": "1.0",
            "userIdentityARN": "arn:aws:iam::111122223333:user/sanhe"
        }
    ]
}

merge-to-master.json:

{
    "Records": [
        {
            "awsRegion": "us-east-2",
            "codecommit": {
                "references": [
                    {
                        "commit": "b741de430b1febc1998217456b4cd0845b3c1865",
                        "deleted": true,
                        "ref": "refs/heads/f1/act1"
                    }
                ]
            },
            "customData": null,
            "eventId": "298ddd4d-533e-49b2-8db1-070cf16d7032",
            "eventName": "ReferenceChanges",
            "eventPartNumber": 1,
            "eventSource": "aws:codecommit",
            "eventSourceARN": "arn:aws:codecommit:us-east-2:111122223333:learn_codebuild",
            "eventTime": "2022-05-21T14:27:54.311+0000",
            "eventTotalParts": 1,
            "eventTriggerConfigId": "8ac5c8d3-2831-4974-97f2-fa08be2e6c82",
            "eventTriggerName": "codecommit-learn_codebuild-trigger",
            "eventVersion": "1.0",
            "userIdentityARN": "arn:aws:iam::111122223333:user/sanhe"
        }
    ]
}

Use boto3 to start CodeBuild Job

示例代码请参考 Trigger Build Job with boto3