

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

# 使用 的身分型政策 AWS CodeBuild
<a name="auth-and-access-control-iam-identity-based-access-control"></a>

本主題提供身分類型政策範例，示範帳戶管理員如何將許可政策連接至 IAM 身分 (即使用者、群組和角色)，並藉此授予許可，以對 AWS CodeBuild 資源執行操作。

**重要**  
建議您先檢閱簡介主題，說明可用於管理 CodeBuild 資源存取權的基本概念和選項。如需詳細資訊，請參閱[管理 AWS CodeBuild 資源存取許可的概觀](auth-and-access-control-iam-access-control-identity-based.md)。

**Topics**
+ [使用 AWS CodeBuild 主控台所需的許可](#console-permissions)
+ [AWS CodeBuild 連線至 Amazon Elastic Container Registry 所需的許可](#ecr-policies)
+ [AWS CodeBuild 主控台連線至來源提供者所需的許可](#console-policies)
+ [AWS 的 受管 （預先定義） 政策 AWS CodeBuild](#managed-policies)
+ [CodeBuild 受管政策和通知](#notifications-permissions)
+ [CodeBuild 受 AWS 管政策的更新](#security-iam-awsmanpol-updates)
+ [客戶受管政策範例](#customer-managed-policies)

以下顯示的許可政策範例，可讓使用者取得組建專案的相關資訊，僅針對 `us-east-2` 區域的帳戶 `123456789012` 中名稱開頭為 `my` 的組建專案：

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

****  

```
{
  "Version":"2012-10-17",		 	 	 
  "Statement": [
    {
      "Effect": "Allow",
      "Action": "codebuild:BatchGetProjects",
      "Resource": "arn:aws:codebuild:us-east-2:111122223333:project/my*"
    }
  ]
}
```

------

## 使用 AWS CodeBuild 主控台所需的許可
<a name="console-permissions"></a>

使用 AWS CodeBuild 主控台的使用者必須擁有一組最低許可，以允許使用者描述 AWS 帳戶的其他 AWS 資源。您必須具備下列服務的許可：
+ AWS CodeBuild
+ Amazon CloudWatch
+ CodeCommit （如果您將原始程式碼存放在 AWS CodeCommit 儲存庫）
+ Amazon Elastic Container Registry (Amazon ECR) （如果您使用的是依賴 Amazon ECR 儲存庫中 Docker 映像的建置環境）
**注意**  
截至 2022 年 7 月 26 日，預設 IAM 政策已更新。如需詳細資訊，請參閱[AWS CodeBuild 連線至 Amazon Elastic Container Registry 所需的許可](#ecr-policies)。
+ Amazon Elastic Container Service (Amazon ECS) （如果您使用的是依賴 Amazon ECR 儲存庫中 Docker 映像的建置環境）
+ AWS Identity and Access Management (IAM)
+ AWS Key Management Service (AWS KMS)
+ Amazon Simple Storage Service (Amazon S3)

如果您建立比最低必要許可更嚴格的 IAM 政策，主控台將無法如預期般運作。

## AWS CodeBuild 連線至 Amazon Elastic Container Registry 所需的許可
<a name="ecr-policies"></a>

自 2022 年 7 月 26 日起， AWS CodeBuild 已更新其 Amazon ECR 許可的預設 IAM 政策。下列許可已從預設政策中移除：

```
"ecr:PutImage",
"ecr:InitiateLayerUpload",
"ecr:UploadLayerPart",
"ecr:CompleteLayerUpload"
```

對於 2022 年 7 月 26 日之前建立的 CodeBuild 專案，我們建議您使用下列 Amazon ECR 政策更新您的政策：

```
"Action": [
    "ecr:BatchCheckLayerAvailability",
    "ecr:GetDownloadUrlForLayer",
    "ecr:BatchGetImage"
]
```

如需更新政策的詳細資訊，請參閱 [允許使用者與 CodeBuild 互動](setting-up-service-permissions-group.md)。

## AWS CodeBuild 主控台連線至來源提供者所需的許可
<a name="console-policies"></a>

 AWS CodeBuild 主控台使用以下 API 動作連線至來源提供者 （例如 GitHub 儲存庫）。
+ `codebuild:ListConnectedOAuthAccounts`
+ `codebuild:ListRepositories`
+ `codebuild:PersistOAuthToken`
+ `codebuild:ImportSourceCredentials`

您可以使用 AWS CodeBuild 主控台將來源提供者 （例如 GitHub 儲存庫） 與建置專案建立關聯。若要這樣做，您必須先將上述 API 動作新增至與您用來存取 AWS CodeBuild 主控台的使用者相關聯的 IAM 存取政策。

`ListConnectedOAuthAccounts`、`ListRepositories` 和 `PersistOAuthToken` API 動作不是為了供您的程式碼呼叫。因此，這些 API 動作不包含在 AWS CLI 和 AWS SDKs中。

## AWS 的 受管 （預先定義） 政策 AWS CodeBuild
<a name="managed-policies"></a>

AWS 提供由 建立和管理的獨立 IAM 政策，以解決許多常見的使用案例 AWS。這些 AWS 受管政策會授予常見使用案例的必要許可，因此您可以避免調查需要哪些許可。CodeBuild 的受管政策也提供在其他 服務中執行操作的許可，例如 IAM AWS CodeCommit、Amazon EC2、Amazon ECR、Amazon SNS 和 Amazon CloudWatch Events，以滿足已授予相關政策之使用者的責任所需。例如，`AWSCodeBuildAdminAccess`政策是一種管理層級使用者政策，可讓使用者使用此政策來建立和管理專案組建的 CloudWatch Events 規則，以及針對專案相關事件 （名稱字首為 的主題`arn:aws:codebuild:`) 通知的 Amazon SNS 主題，以及在 CodeBuild 中管理專案和報告群組。如需詳細資訊，請參閱《*IAM 使用者指南*》中的 [AWS 受管政策](https://docs.aws.amazon.com/IAM/latest/UserGuide/access_policies_managed-vs-inline.html#aws-managed-policies)。

下列 AWS 受管政策是特定的，您可以連接到您帳戶中的使用者 AWS CodeBuild。

**AWSCodeBuildAdminAccess**  
提供 CodeBuild 的完整存取權，包括管理 CodeBuild 組建專案的許可。

**AWSCodeBuildDeveloperAccess**  
提供 CodeBuild 的存取權，但不允許組建專案管理。

**AWSCodeBuildReadOnlyAccess**  
提供 CodeBuild 的唯讀存取權。

若要存取 CodeBuild 建立的建置輸出成品，您還必須連接名為 的 AWS 受管政策`AmazonS3ReadOnlyAccess`。

若要建立和管理 CodeBuild 服務角色，您還必須連接名為 的 AWS 受管政策`IAMFullAccess`。

您也可以建立自己的自訂 IAM 政策，以允許 CodeBuild 動作和資源的許可。您可以將這些自訂政策連接至需要這些許可的使用者或群組。

**Topics**
+ [AWSCodeBuildAdminAccess](#admin-access-policy)
+ [AWSCodeBuildDeveloperAccess](#developer-access-policy)
+ [AWSCodeBuildReadOnlyAccess](#read-only-access-policy)

### AWSCodeBuildAdminAccess
<a name="admin-access-policy"></a>

此`AWSCodeBuildAdminAccess`政策提供 CodeBuild 的完整存取權，包括管理 CodeBuild 組建專案的許可。僅將此政策套用至管理層級使用者，以授予他們對 AWS 帳戶中 CodeBuild 專案、報告群組和相關資源的完整控制權，包括刪除專案和報告群組的能力。

如需完整的受管政策，請參閱 IAM 受管政策參考中的 [ AWSCodeBuildAdminAccess](https://docs.aws.amazon.com/aws-managed-policy/latest/reference/AWSCodeBuildAdminAccess.html)。

### AWSCodeBuildDeveloperAccess
<a name="developer-access-policy"></a>

此`AWSCodeBuildDeveloperAccess`政策允許存取 CodeBuild 的所有功能，以及專案和報告群組相關資源。此政策不允許使用者刪除 CodeBuild 專案或報告群組，或其他 AWS 服務中的相關資源，例如 CloudWatch Events。建議將此政策套用到大多數使用者。

如需完整的受管政策，請參閱 IAM 受管政策參考中的 [ AWSCodeBuildDeveloperAccess](https://docs.aws.amazon.com/aws-managed-policy/latest/reference/AWSCodeBuildDeveloperAccess.html)。

### AWSCodeBuildReadOnlyAccess
<a name="read-only-access-policy"></a>

此`AWSCodeBuildReadOnlyAccess`政策授予對 CodeBuild 和其他 AWS 服務中相關資源的唯讀存取權。將此政策套用至可檢視和執行組建、檢視專案，以及檢視報告群組，但無法對其進行任何變更的使用者。

如需完整的受管政策，請參閱 IAM 受管政策參考中的 [ AWSCodeBuildReadOnlyAccess](https://docs.aws.amazon.com/aws-managed-policy/latest/reference/AWSCodeBuildReadOnlyAccess.xml)。

## CodeBuild 受管政策和通知
<a name="notifications-permissions"></a>

CodeBuild 支援通知，可通知使用者建置專案的重要變更。CodeBuild 的受管政策包含通知功能的政策陳述式。如需詳細資訊，請參閱[什麼是通知？](https://docs.aws.amazon.com/codestar-notifications/latest/userguide/welcome.html)。

### 唯讀受管政策中的通知相關許可
<a name="notifications-readonly"></a>

`AWSCodeBuildReadOnlyAccess` 受管政策包含下列陳述式，允許對通知的唯讀存取權限。適用此受管政策的使用者可以檢視資源的通知，但無法建立、管理或訂閱通知。

```
   {
        "Sid": "CodeStarNotificationsPowerUserAccess",
        "Effect": "Allow",
        "Action": [
            "codestar-notifications:DescribeNotificationRule"
        ],
        "Resource": "*",
        "Condition" : {
            "ArnLike" : {"codestar-notifications:NotificationsForResource" : "arn:aws:codebuild:*:*:project/*"}
        }
    },    
    {
        "Sid": "CodeStarNotificationsListAccess",
        "Effect": "Allow",
        "Action": [
            "codestar-notifications:ListNotificationRules",
            "codestar-notifications:ListEventTypes",
            "codestar-notifications:ListTargets"
        ],
        "Resource": "*"
    }
```

### 其他受管政策中的通知相關許可
<a name="notifications-otheraccess"></a>

`AWSCodeBuildDeveloperAccess` 受管政策包含下列陳述式，允許使用者建立、編輯和訂閱通知。使用者無法刪除通知規則或管理資源的標籤。

```
    {
        "Sid": "CodeStarNotificationsReadWriteAccess",
        "Effect": "Allow",
        "Action": [
            "codestar-notifications:CreateNotificationRule",
            "codestar-notifications:DescribeNotificationRule",
            "codestar-notifications:UpdateNotificationRule",
            "codestar-notifications:Subscribe",
            "codestar-notifications:Unsubscribe"
        ],
        "Resource": "*",
        "Condition" : {
            "ArnLike" : {"codestar-notifications:NotificationsForResource" : "arn:aws:codebuild:*:*:project/*"}
        }
    },    
    {
        "Sid": "CodeStarNotificationsListAccess",
        "Effect": "Allow",
        "Action": [
            "codestar-notifications:ListNotificationRules",
            "codestar-notifications:ListTargets",
            "codestar-notifications:ListTagsforResource",
            "codestar-notifications:ListEventTypes"
        ],
        "Resource": "*"
    },
    {
        "Sid": "SNSTopicListAccess",
        "Effect": "Allow",
        "Action": [
            "sns:ListTopics"
        ],
        "Resource": "*"
    },
    {
        "Sid": "CodeStarNotificationsChatbotAccess",
        "Effect": "Allow",
        "Action": [
            "chatbot:DescribeSlackChannelConfigurations",
            "chatbot:ListMicrosoftTeamsChannelConfigurations"
          ],
       "Resource": "*"
    }
```

如需 IAM 和通知的詳細資訊，請參閱 [Identity and Access Management 以取得AWS CodeStar通知](https://docs.aws.amazon.com/codestar-notifications/latest/userguide/security-iam.html)。

## CodeBuild 受 AWS 管政策的更新
<a name="security-iam-awsmanpol-updates"></a>

檢視自此服務開始追蹤這些變更以來 CodeBuild AWS 受管政策更新的詳細資訊。如需此頁面變更的自動提醒，請訂閱 上的 RSS 摘要[AWS CodeBuild 使用者指南文件歷史記錄](history.md)。




| 變更 | 描述 | Date | 
| --- | --- | --- | 
|  `AWSCodeBuildAdminAccess` 和 `AWSCodeBuildDeveloperAccess` – 更新現有政策  |  CodeBuild 新增了這些政策的`ssmmessages:OpenDataChannel`許可，以支援 Session Manager 互動式建置偵錯。 `AWSCodeBuildAdminAccess` 和 `AWSCodeBuildDeveloperAccess`政策現在包含 Session Manager 工作階段資源 (`arn:aws:ssm:*:*:session/*`) `ssmmessages:OpenDataChannel`的動作，以支援此 WebSocket API 上的 SigV4 強制執行。  | 2025 年 12 月 1 日 | 
|  `AWSCodeBuildAdminAccess`、 `AWSCodeBuildDeveloperAccess`和 `AWSCodeBuildReadOnlyAccess` – 更新現有政策  |  CodeBuild 已更新這些政策的資源。 `AWSCodeBuildAdminAccess`、 `AWSCodeBuildDeveloperAccess`和 `AWSCodeBuildReadOnlyAccess`政策已變更為更新現有資源。原始資源`arn:aws:codebuild:*`已更新為 `arn:aws:codebuild:*:*:project/*`。  | 2024 年 11 月 15 日 | 
|  `AWSCodeBuildAdminAccess`、 `AWSCodeBuildDeveloperAccess`和 `AWSCodeBuildReadOnlyAccess` – 更新現有政策  |  CodeBuild 已將資源新增至這些政策，以支援品牌 AWS CodeConnections 重塑。 `AWSCodeBuildAdminAccess`、 `AWSCodeBuildDeveloperAccess`和 `AWSCodeBuildReadOnlyAccess`政策已變更為新增資源 `arn:aws:codeconnections:*:*:*`。  | 2024 年 4 月 18 日 | 
|  `AWSCodeBuildAdminAccess` 和 `AWSCodeBuildDeveloperAccess` – 更新現有政策  |  CodeBuild 新增了這些政策的許可，以支援在聊天應用程式中使用 Amazon Q Developer 的其他通知類型。 `AWSCodeBuildAdminAccess` 和 `AWSCodeBuildDeveloperAccess`政策已變更為新增許可 `chatbot:ListMicrosoftTeamsChannelConfigurations`。  | 2023 年 5 月 16 日 | 
|  CodeBuild 已開始追蹤變更  |  CodeBuild 開始追蹤其 AWS 受管政策的變更。  | 2021 年 5 月 16 日 | 

## 客戶受管政策範例
<a name="customer-managed-policies"></a>

在本節中，您可以找到授予 AWS CodeBuild 動作許可的使用者政策範例。當您使用 CodeBuild API、 AWS SDKs或 時，這些政策會運作 AWS CLI。當您使用主控台時，您必須授予其他主控台特定的許可。如需相關資訊，請參閱[使用 AWS CodeBuild 主控台所需的許可](#console-permissions)。

您可以使用下列範例 IAM 政策來限制使用者和角色的 CodeBuild 存取。

**Topics**
+ [允許使用者取得建置專案的相關資訊](#customer-managed-policies-example-batch-get-projects)
+ [允許使用者取得機群的相關資訊](#customer-managed-policies-get-information-about-fleets)
+ [允許使用者取得報告群組的相關資訊](#customer-managed-policies-get-information-about-report-group)
+ [允許使用者取得報告的相關資訊](#customer-managed-policies-get-information-about-reports)
+ [允許使用者建立建置專案](#customer-managed-policies-example-create-project)
+ [允許使用者建立機群](#customer-managed-policies-example-create-fleet)
+ [允許使用者建立報告群組](#customer-managed-policies-example-create-report-group)
+ [允許使用者刪除機群](#customer-managed-policies-example-delete-fleet)
+ [允許使用者刪除報告群組](#customer-managed-policies-example-delete-report-group)
+ [允許使用者刪除報告](#customer-managed-policies-example-delete-report)
+ [允許使用者刪除建置專案](#customer-managed-policies-example-delete-project)
+ [允許使用者取得建置專案名稱的清單](#customer-managed-policies-example-list-projects)
+ [允許使用者變更建置專案的相關資訊](#customer-managed-policies-example-update-project)
+ [允許使用者變更機群](#customer-managed-policies-example-change-fleet)
+ [允許使用者變更報告群組](#customer-managed-policies-example-change-report-group)
+ [允許使用者取得建置的相關資訊](#customer-managed-policies-example-batch-get-builds)
+ [允許使用者取得建置專案之建置 ID 的清單](#customer-managed-policies-example-list-builds-for-project)
+ [允許使用者取得建置 ID 的清單](#customer-managed-policies-example-list-builds)
+ [允許使用者取得機群清單](#customer-managed-policies-example-get-list-of-fleets)
+ [允許使用者取得報告群組的清單](#customer-managed-policies-example-get-list-of-report-groups)
+ [允許使用者取得報告的清單](#customer-managed-policies-example-get-list-of-reports)
+ [允許使用者取得報告群組的報告清單](#customer-managed-policies-example-get-list-of-reports-for-report-group)
+ [允許使用者取得報告的測試案例清單](#customer-managed-policies-example-get-list-of-test-cases-for-report)
+ [允許使用者開始執行建置](#customer-managed-policies-example-start-build)
+ [允許使用者嘗試停止建置](#customer-managed-policies-example-stop-build)
+ [允許使用者嘗試刪除建置](#customer-managed-policies-example-delete-builds)
+ [允許使用者取得 CodeBuild 管理之 Docker 映像的相關資訊](#customer-managed-policies-example-list-curated-environment-images)
+ [允許使用者新增機群服務角色的許可政策](#customer-managed-policies-example-permission-policy-fleet-service-role)
+ [允許 CodeBuild 存取建立 VPC 網路介面所需的 AWS 服務](#customer-managed-policies-example-create-vpc-network-interface)
+ [使用拒絕陳述式來 AWS CodeBuild 防止 中斷與來源提供者的連線](#customer-managed-policies-example-deny-disconnect)

### 允許使用者取得建置專案的相關資訊
<a name="customer-managed-policies-example-batch-get-projects"></a>

以下顯示的政策陳述式範例，會允許使用者取得組建專案的相關資訊，針對 `us-east-2` 區域的帳戶 `123456789012` 中名稱開頭為 `my` 的組建專案：

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

****  

```
{
  "Version":"2012-10-17",		 	 	 
  "Statement": [
    {
      "Effect": "Allow",
      "Action": "codebuild:BatchGetProjects",
      "Resource": "arn:aws:codebuild:us-east-2:111122223333:project/my*"      
    }
  ]
}
```

------

### 允許使用者取得機群的相關資訊
<a name="customer-managed-policies-get-information-about-fleets"></a>

下列範例政策陳述式可讓使用者取得帳戶 `us-east-2`區域中機群的相關資訊`123456789012`：

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

****  

```
{
  "Version":"2012-10-17",		 	 	 
  "Statement": [
    {
      "Effect": "Allow",
      "Action": "codebuild:BatchGetFleets",
      "Resource": "arn:aws:codebuild:us-east-2:111122223333:fleet/*"
    }
  ]
}
```

------

### 允許使用者取得報告群組的相關資訊
<a name="customer-managed-policies-get-information-about-report-group"></a>

以下顯示的政策陳述式範例，會允許使用者取得帳戶 `123456789012` 在 `us-east-2` 區域的相關報告群組資訊：

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

****  

```
{
  "Version":"2012-10-17",		 	 	 
  "Statement": [
    {
      "Effect": "Allow",
      "Action": "codebuild:BatchGetReportGroups",
      "Resource": "arn:aws:codebuild:us-east-2:111122223333:report-group/*"
    }
  ]
}
```

------

### 允許使用者取得報告的相關資訊
<a name="customer-managed-policies-get-information-about-reports"></a>

以下顯示的政策陳述式範例，會允許使用者取得帳戶 `123456789012` 在 `us-east-2` 區域的相關報告資訊：

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

****  

```
{
  "Version":"2012-10-17",		 	 	 
  "Statement": [
    {
      "Effect": "Allow",
      "Action": "codebuild:BatchGetReports",
      "Resource": "arn:aws:codebuild:us-east-2:111122223333:report-group/*"
    }
  ]
}
```

------

### 允許使用者建立建置專案
<a name="customer-managed-policies-example-create-project"></a>

下列範例政策陳述式允許使用者建立具有任何名稱的組建專案，但僅限於帳戶 `us-east-2`區域中`123456789012`，且僅使用指定的 CodeBuild 服務角色：

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

****  

```
{
  "Version":"2012-10-17",		 	 	 
  "Statement": [
    {
      "Effect": "Allow",
      "Action": "codebuild:CreateProject",
      "Resource": "arn:aws:codebuild:us-east-2:111122223333:project/*"
    },
    {
      "Effect": "Allow",
      "Action": "iam:PassRole",
      "Resource": "arn:aws:iam::111122223333:role/CodeBuildServiceRole"
    }
  ]
}
```

------

下列範例政策陳述式允許使用者建立任何名稱的組建專案，但僅限於 帳戶的 `us-east-2` 區域中`123456789012`，且只能使用指定的 CodeBuild 服務角色。它也會強制使用者只能搭配 使用指定的服務角色 AWS CodeBuild ，而非任何其他 AWS 服務。

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

****  

```
{
  "Version":"2012-10-17",		 	 	 
  "Statement": [
    {
      "Effect": "Allow",
      "Action": "codebuild:CreateProject",
      "Resource": "arn:aws:codebuild:us-east-2:111122223333:project/*"
    },
    {
      "Effect": "Allow",
      "Action": "iam:PassRole",
      "Resource": "arn:aws:iam::111122223333:role/CodeBuildServiceRole",
      "Condition": {
          "StringEquals": {"iam:PassedToService": "codebuild.amazonaws.com"}
      }
    }
  ]
}
```

------

### 允許使用者建立機群
<a name="customer-managed-policies-example-create-fleet"></a>

下列範例政策陳述式允許使用者在 `us-east-2` 區域中為帳戶 建立機群`123456789012`：

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

****  

```
{
  "Version":"2012-10-17",		 	 	 
  "Statement": [
    {
      "Effect": "Allow",
      "Action": "codebuild:CreateFleet",
      "Resource": "arn:aws:codebuild:us-east-2:111122223333:fleet/*"
    }
  ]
}
```

------

### 允許使用者建立報告群組
<a name="customer-managed-policies-example-create-report-group"></a>

以下顯示的政策陳述式範例，會允許使用者建立帳戶 `123456789012` 在 `us-east-2` 區域中的報告群組：

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

****  

```
{
  "Version":"2012-10-17",		 	 	 
  "Statement": [
    {
      "Effect": "Allow",
      "Action": "codebuild:CreateReportGroup",
      "Resource": "arn:aws:codebuild:us-east-2:111122223333:report-group/*"
    }
  ]
}
```

------

### 允許使用者刪除機群
<a name="customer-managed-policies-example-delete-fleet"></a>

下列範例政策陳述式允許使用者刪除帳戶 `us-east-2`區域中的機群`123456789012`：

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

****  

```
{
  "Version":"2012-10-17",		 	 	 
  "Statement": [
    {
      "Effect": "Allow",
      "Action": "codebuild:DeleteFleet",
      "Resource": "arn:aws:codebuild:us-east-2:111122223333:fleet/*"
    }
  ]
}
```

------

### 允許使用者刪除報告群組
<a name="customer-managed-policies-example-delete-report-group"></a>

以下顯示的政策陳述式範例，會允許使用者刪除帳戶 `123456789012` 在 `us-east-2` 區域中的報告群組：

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

****  

```
{
  "Version":"2012-10-17",		 	 	 
  "Statement": [
    {
      "Effect": "Allow",
      "Action": "codebuild:DeleteReportGroup",
      "Resource": "arn:aws:codebuild:us-east-2:111122223333:report-group/*"
    }
  ]
}
```

------

### 允許使用者刪除報告
<a name="customer-managed-policies-example-delete-report"></a>

以下顯示的政策陳述式範例，會允許使用者刪除帳戶 `123456789012` 在 `us-east-2` 區域中的報告：

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

****  

```
{
  "Version":"2012-10-17",		 	 	 
  "Statement": [
    {
      "Effect": "Allow",
      "Action": "codebuild:DeleteReport",
      "Resource": "arn:aws:codebuild:us-east-2:111122223333:report-group/*"
    }
  ]
}
```

------

### 允許使用者刪除建置專案
<a name="customer-managed-policies-example-delete-project"></a>

以下顯示的政策陳述式範例，會允許使用者刪除組建專案，針對 `us-east-2` 區域的帳戶 `123456789012` 中名稱開頭為 `my` 的組建專案：

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

****  

```
{
  "Version":"2012-10-17",		 	 	 
  "Statement": [
    {
      "Effect": "Allow",
      "Action": "codebuild:DeleteProject",
      "Resource": "arn:aws:codebuild:us-east-2:111122223333:project/my*"
    }
  ]
}
```

------

### 允許使用者取得建置專案名稱的清單
<a name="customer-managed-policies-example-list-projects"></a>

下列政策陳述式的範例，會允許使用者取得相同帳戶之組建專案名稱的清單：

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

****  

```
{
  "Version":"2012-10-17",		 	 	 
  "Statement": [
    {
      "Effect": "Allow",
      "Action": "codebuild:ListProjects",
      "Resource": "*"
    }
  ]
}
```

------

### 允許使用者變更建置專案的相關資訊
<a name="customer-managed-policies-example-update-project"></a>

以下顯示的政策陳述式範例，會允許使用者變更使用任何名稱組建專案的相關資訊，但只能針對 `us-east-2` 區域中的帳戶 `123456789012`，並只能使用指定的 AWS CodeBuild 服務角色：

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

****  

```
{
  "Version":"2012-10-17",		 	 	 
  "Statement": [
    {
      "Effect": "Allow",
      "Action": "codebuild:UpdateProject",
      "Resource": "arn:aws:codebuild:us-east-2:111122223333:project/*"
    },
    {
      "Effect": "Allow",
      "Action": "iam:PassRole",
      "Resource": "arn:aws:iam::111122223333:role/CodeBuildServiceRole"
    }
  ]
}
```

------

### 允許使用者變更機群
<a name="customer-managed-policies-example-change-fleet"></a>

下列範例政策陳述式允許使用者變更帳戶 `us-east-2`區域中的機群`123456789012`：

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

****  

```
{
  "Version":"2012-10-17",		 	 	 
  "Statement": [
    {
      "Effect": "Allow",
      "Action": "codebuild:UpdateFleet",
      "Resource": "arn:aws:codebuild:us-east-2:111122223333:fleet/*"
    }
  ]
}
```

------

### 允許使用者變更報告群組
<a name="customer-managed-policies-example-change-report-group"></a>

以下顯示的政策陳述式範例，會允許使用者變更帳戶 `123456789012` 在 `us-east-2` 區域中的報告群組：

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

****  

```
{
  "Version":"2012-10-17",		 	 	 
  "Statement": [
    {
      "Effect": "Allow",
      "Action": "codebuild:UpdateReportGroup",
      "Resource": "arn:aws:codebuild:us-east-2:111122223333:report-group/*"
    }
  ]
}
```

------

### 允許使用者取得建置的相關資訊
<a name="customer-managed-policies-example-batch-get-builds"></a>

以下顯示的政策陳述式範例，會允許使用者取得組建的相關資訊，針對 `us-east-2` 區域的帳戶 `123456789012` 中名稱為 `my-build-project` 和 `my-other-build-project` 的組建專案：

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

****  

```
{
  "Version":"2012-10-17",		 	 	 
  "Statement": [
    {
      "Effect": "Allow",
      "Action": "codebuild:BatchGetBuilds",
      "Resource": [
        "arn:aws:codebuild:us-east-2:111122223333:project/my-build-project",
        "arn:aws:codebuild:us-east-2:111122223333:project/my-other-build-project"
      ]
    }
  ]
}
```

------

### 允許使用者取得建置專案之建置 ID 的清單
<a name="customer-managed-policies-example-list-builds-for-project"></a>

以下顯示的政策陳述式範例，會允許使用者取得組建 ID 的清單，針對 `us-east-2` 區域的帳戶 `123456789012` 中名稱為 `my-build-project` 和 `my-other-build-project` 的組建專案：

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

****  

```
{
  "Version":"2012-10-17",		 	 	 
  "Statement": [
    {
      "Effect": "Allow",
      "Action": "codebuild:ListBuildsForProject",
      "Resource": [
        "arn:aws:codebuild:us-east-2:111122223333:project/my-build-project",
        "arn:aws:codebuild:us-east-2:111122223333:project/my-other-build-project"
      ]
    }
  ]
}
```

------

### 允許使用者取得建置 ID 的清單
<a name="customer-managed-policies-example-list-builds"></a>

下列政策陳述式的範例，會允許使用者取得相同帳戶之所有組建 ID 的清單：

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

****  

```
{
  "Version":"2012-10-17",		 	 	 
  "Statement": [
    {
      "Effect": "Allow",
      "Action": "codebuild:ListBuilds",
      "Resource": "*"
    }
  ]
}
```

------

### 允許使用者取得機群清單
<a name="customer-managed-policies-example-get-list-of-fleets"></a>

下列範例政策陳述式允許使用者取得帳戶 `us-east-2`區域中機群的清單`123456789012`：

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

****  

```
{
  "Version":"2012-10-17",		 	 	 
  "Statement": [
    {
      "Effect": "Allow",
      "Action": "codebuild:ListFleets",
      "Resource": "*"
    }
  ]
}
```

------

### 允許使用者取得報告群組的清單
<a name="customer-managed-policies-example-get-list-of-report-groups"></a>

以下顯示的政策陳述式範例，會允許使用者取得帳戶 `123456789012` 在 `us-east-2` 區域的報告群組清單：

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

****  

```
{
  "Version":"2012-10-17",		 	 	 
  "Statement": [
    {
      "Effect": "Allow",
      "Action": "codebuild:ListReportGroups",
      "Resource": "*"
    }
  ]
}
```

------

### 允許使用者取得報告的清單
<a name="customer-managed-policies-example-get-list-of-reports"></a>

以下顯示的政策陳述式範例，會允許使用者取得帳戶 `123456789012` 在 `us-east-2` 區域的報告清單：

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

****  

```
{
  "Version":"2012-10-17",		 	 	 
  "Statement": [
    {
      "Effect": "Allow",
      "Action": "codebuild:ListReports",
      "Resource": "*"
    }
  ]
}
```

------

### 允許使用者取得報告群組的報告清單
<a name="customer-managed-policies-example-get-list-of-reports-for-report-group"></a>

以下顯示的政策陳述式範例，會允許使用者取得帳戶 `123456789012` 在 `us-east-2` 區域中報告群組的報告清單：

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

****  

```
{
  "Version":"2012-10-17",		 	 	 
  "Statement": [
    {
      "Effect": "Allow",
      "Action": "codebuild:ListReportsForReportGroup",
      "Resource": "arn:aws:codebuild:us-east-2:111122223333:report-group/*"
    }
  ]
}
```

------

### 允許使用者取得報告的測試案例清單
<a name="customer-managed-policies-example-get-list-of-test-cases-for-report"></a>

以下顯示的政策陳述式範例，會允許使用者取得帳戶 `123456789012` 在 `us-east-2` 區域中報告的測試案例清單：

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

****  

```
{
  "Version":"2012-10-17",		 	 	 
  "Statement": [
    {
      "Effect": "Allow",
      "Action": "codebuild:DescribeTestCases",
      "Resource": "arn:aws:codebuild:us-east-2:111122223333:report-group/*"
    }
  ]
}
```

------

### 允許使用者開始執行建置
<a name="customer-managed-policies-example-start-build"></a>

以下顯示的政策陳述式範例，會允許使用者執行組建，針對 `us-east-2` 區域的帳戶 `123456789012` 中名稱開頭為 `my` 的組建專案：

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

****  

```
{
  "Version":"2012-10-17",		 	 	 
  "Statement": [
    {
      "Effect": "Allow",
      "Action": "codebuild:StartBuild",
      "Resource": "arn:aws:codebuild:us-east-2:111122223333:project/my*"
    }
  ]
}
```

------

### 允許使用者嘗試停止建置
<a name="customer-managed-policies-example-stop-build"></a>

以下顯示的政策陳述式範例，會允許使用者嘗試停止執行中的組建，僅針對 `us-east-2` 區域的帳戶 `123456789012` 中名稱開頭為 `my` 的組建專案：

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

****  

```
{
  "Version":"2012-10-17",		 	 	 
  "Statement": [
    {
      "Effect": "Allow",
      "Action": "codebuild:StopBuild",
      "Resource": "arn:aws:codebuild:us-east-2:111122223333:project/my*"
    }
  ]
}
```

------

### 允許使用者嘗試刪除建置
<a name="customer-managed-policies-example-delete-builds"></a>

以下顯示的政策陳述式範例，會允許使用者嘗試刪除建置，僅針對 `us-east-2` 區域的帳戶 `123456789012` 中名稱開頭為 `my` 的建置專案：

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

****  

```
{
  "Version":"2012-10-17",		 	 	 
  "Statement": [
    {
      "Effect": "Allow",
      "Action": "codebuild:BatchDeleteBuilds",
      "Resource": "arn:aws:codebuild:us-east-2:111122223333:project/my*"
    }
  ]
}
```

------

### 允許使用者取得 CodeBuild 管理之 Docker 映像的相關資訊
<a name="customer-managed-policies-example-list-curated-environment-images"></a>

下列範例政策陳述式允許使用者取得 CodeBuild 管理之所有 Docker 映像的相關資訊：

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

****  

```
{
  "Version":"2012-10-17",		 	 	 
  "Statement": [
    {
      "Effect": "Allow",
      "Action": "codebuild:ListCuratedEnvironmentImages",
      "Resource": "*"
    }
  ]
}
```

------

### 允許使用者新增機群服務角色的許可政策
<a name="customer-managed-policies-example-permission-policy-fleet-service-role"></a>

下列範例資源政策陳述式允許使用者為機群服務角色新增 VPC 許可政策：

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

****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [
        {
            "Sid": "CodeBuildFleetVpcCreateNI",
            "Effect": "Allow",
            "Action": [
                "ec2:CreateNetworkInterface"
            ],
            "Resource": [
                "arn:aws:ec2:us-west-2:111122223333:subnet/subnet-id-1",
                "arn:aws:ec2:us-west-2:111122223333:security-group/security-group-id-1",
                "arn:aws:ec2:us-west-2:111122223333:network-interface/*"
            ]
        },
        {
            "Sid": "CodeBuildFleetVpcPermission",
            "Effect": "Allow",
            "Action": [
                "ec2:DescribeDhcpOptions",
                "ec2:DescribeNetworkInterfaces",
                "ec2:DescribeSecurityGroups",
                "ec2:DescribeSubnets",
                "ec2:DescribeVpcs",
                "ec2:ModifyNetworkInterfaceAttribute",
                "ec2:DeleteNetworkInterface"
            ],
            "Resource": "*"
        },
        {
            "Sid": "CodeBuildFleetVpcNIPermission",
            "Effect": "Allow",
            "Action": [
                "ec2:CreateNetworkInterfacePermission"
            ],
            "Resource": "arn:aws:ec2:us-west-2:111122223333:network-interface/*",
            "Condition": {
                "StringEquals": {
                    "ec2:Subnet": [
                        "arn:aws:ec2:us-west-2:111122223333:subnet/subnet-id-1"
                    ]
                }
            }
        }
    ]
}
```

------

下列範例資源政策陳述式允許使用者為機群服務角色新增自訂 Amazon Managed Image (AMI) 許可政策：

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

****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [
        {
            "Effect": "Allow",
            "Action": "ec2:DescribeImages",
            "Resource": "*"
        } 
    ]
}
```

------

下列信任政策陳述式範例允許使用者新增機群服務角色的許可政策：

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

****  

```
{
  "Version":"2012-10-17",		 	 	 
  "Statement": [
    {
      "Sid": "CodeBuildFleetVPCTrustPolicy",
      "Effect": "Allow",
      "Principal": {
        "Service": "codebuild.amazonaws.com"
      },
      "Action": "sts:AssumeRole",
      "Condition": {
        "StringEquals": {
          "aws:SourceAccount": "111122223333"
        }
      }
    }
  ]
}
```

------

### 允許 CodeBuild 存取建立 VPC 網路介面所需的 AWS 服務
<a name="customer-managed-policies-example-create-vpc-network-interface"></a>

下列範例政策陳述式授予在具有兩個子網路的 VPC 中建立網路介面的 AWS CodeBuild 許可：

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

****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "ec2:CreateNetworkInterface",
                "ec2:DescribeDhcpOptions",
                "ec2:DescribeNetworkInterfaces",
                "ec2:DeleteNetworkInterface",
                "ec2:DescribeSubnets",
                "ec2:DescribeSecurityGroups",
                "ec2:DescribeVpcs"
            ],
            "Resource": "*"
        },
        {
            "Effect": "Allow",
            "Action": [
                "ec2:CreateNetworkInterfacePermission"
            ],
            "Resource": "arn:aws:ec2:us-west-2:111122223333:network-interface/*",
            "Condition": {
                "StringEquals": {
                    "ec2:AuthorizedService": "codebuild.amazonaws.com"
                },
                "ArnEquals": {
                    "ec2:Subnet": [
                        "arn:aws:ec2:us-west-2:111122223333:subnet/subnet-id-1",
                        "arn:aws:ec2:us-west-2:111122223333:subnet/subnet-id-2"
                    ]
                }
            }
        }
    ]
}
```

------

### 使用拒絕陳述式來 AWS CodeBuild 防止 中斷與來源提供者的連線
<a name="customer-managed-policies-example-deny-disconnect"></a>

 以下範例政策陳述式會使用拒絕陳述式，以防止 AWS CodeBuild 中斷與來源提供者的連接。它使用 `codebuild:DeleteOAuthToken` (與 `codebuild:PersistOAuthToken` 和 `codebuild:ImportSourceCredentials` 相反) 與來源供應商連接。如需詳細資訊，請參閱[AWS CodeBuild 主控台連線至來源提供者所需的許可](#console-policies)。

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

****  

```
{
  "Version":"2012-10-17",		 	 	 
  "Statement": [
    {
      "Effect": "Deny",
      "Action": "codebuild:DeleteOAuthToken",
      "Resource": "*"
    }
  ]
}
```

------