

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

# AWS CodeBuild 條件索引鍵
<a name="action-context-keys"></a>

AWS CodeBuild 提供一組條件金鑰，可用於 IAM 政策，以在專案和機群等 CodeBuild 資源上強制執行組織政策。條件索引鍵涵蓋大部分 CodeBuild API 請求內容，包括網路設定、登入資料組態和運算限制。

**Topics**
+ [在專案和機群上強制執行 VPC 連線設定](#action-context-keys-example-overrideVPC)
+ [防止對專案 buildspec 進行未經授權的修改](#action-context-keys-example-overridebuildspec)
+ [限制您建置的運算類型](#action-context-keys-example-computechoice)
+ [控制環境變數設定](#action-context-keys-example-env-variables)
+ [在條件索引鍵名稱中使用變數](#action-context-keys-example-variables)
+ [檢查 API 請求中是否存在屬性](#action-context-keys-example-env-denyoverride)

## 在專案和機群上強制執行 VPC 連線設定
<a name="action-context-keys-example-overrideVPC"></a>

此政策允許發起人在建立 CodeBuild 專案和機群時，使用選取的 VPCs、子網路和安全群組。如需多值內容索引鍵的詳細資訊，請參閱[單一值與多值內容索引鍵。](https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_policies_condition-single-vs-multi-valued-context-keys.html)

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

****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [{
        "Effect": "Allow",
        "Action": [
            "codebuild:CreateProject",
            "codebuild:CreateFleet"
        ],
        "Resource": "*",
        "Condition": {
            "ForAnyValue:StringEquals": {
                "codebuild:vpcConfig.vpcId": [
                    "vpc-01234567890abcdef",
                    "vpc-abcdef01234567890"
                ],
                "codebuild:vpcConfig.subnets": [
                    "subnet-1234abcd",
                    "subnet-5678abcd"
                ],
                "codebuild:vpcConfig.securityGroupIds": [
                    "sg-12345678abcdefghij",
                    "sg-01234567abcdefghij"
                ]
            }
        }
    }]
}
```

------

## 防止對專案 buildspec 進行未經授權的修改
<a name="action-context-keys-example-overridebuildspec"></a>

此政策不允許發起人覆寫 `buildspecOverride` 欄位中的 buildspec。

**注意**  
`codebuild:source.buildspec` 條件索引鍵僅支援 Null 運算子來檢查 API 欄位是否存在。它不會評估 buildspec 的內容。

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

****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [{
        "Effect": "Allow",
        "Action": "codebuild:StartBuild",
        "Resource": "*"
    }, {
        "Effect": "Deny",
        "Action": "codebuild:StartBuild",
        "Resource": "*",
        "Condition": {
            "Null": {
                "codebuild:source.buildspec": "false"
            }
        }
    }]
}
```

------

## 限制您建置的運算類型
<a name="action-context-keys-example-computechoice"></a>

此政策允許建立只能使用 `c5.large`或 `m5.large` [運算執行個體類型](https://docs.aws.amazon.com/codebuild/latest/userguide/build-env-ref-compute-types.html#environment-reserved-capacity.instance-types)建置的機群。

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

****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [{
        "Effect": "Allow",
        "Action": "codebuild:CreateFleet",
        "Resource": "*",
        "Condition": {
            "ForAnyValue:StringEquals": {
                "codebuild:computeConfiguration.instanceType": ["c5.large", "m5.large"]
            }
        }
    }]
}
```

------

## 控制環境變數設定
<a name="action-context-keys-example-env-variables"></a>

此政策允許發起人覆寫`STAGE`環境變數為 `BETA`或 `GAMMA`。它也明確拒絕覆寫`STAGE`為 `PRODUCTION`，並拒絕覆寫`MY_APP_VERSION`環境變數。如需多個值內容索引鍵，請參閱[單一值與多值內容索引鍵。](https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_policies_condition-single-vs-multi-valued-context-keys.html)

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

****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "codebuild:StartBuild"
            ],
            "Resource": "*",
            "Condition": {
                "ForAnyValue:StringEquals": {
                    "codebuild:environment.environmentVariables/STAGE.value": [
                        "BETA",
                        "GAMMA"
                    ]
                }
            }
        },
        {
            "Effect": "Deny",
            "Action": [
                "codebuild:StartBuild"
            ],
            "Resource": "*",
            "Condition": {
                "StringEquals": {
                    "codebuild:environment.environmentVariables/STAGE.value": "PRODUCTION"
                },
                "ForAnyValue:StringEquals": {
                    "codebuild:environment.environmentVariables.name": [
                        "MY_APP_VERSION"
                    ]
                }
            }
        }
    ]
}
```

------

## 在條件索引鍵名稱中使用變數
<a name="action-context-keys-example-variables"></a>

您可以在條件索引鍵名稱中使用變數，例如 `secondarySources/${sourceIdentifier}.location`和 `secondaryArtifacts/${artifactIdentifier}.location`，您可以在 IAM 政策中指定次要[來源](https://docs.aws.amazon.com/codebuild/latest/APIReference/API_ProjectSource.html)或次要[成品](https://docs.aws.amazon.com/codebuild/latest/APIReference/API_ProjectArtifacts.html)識別符。以下政策允許發起人建立具有次要來源 之特定來源位置的專案`mySecondSource`。

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

****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [
        {
            "Effect": "Allow",
            "Action": "codebuild:CreateProject",
            "Resource": "*",
            "Condition": {
                "StringEquals": {
                    "codebuild:secondarySources/mySecondSource.location": "my-source-location"
                }
            }
        }
    ]
}
```

------

## 檢查 API 請求中是否存在屬性
<a name="action-context-keys-example-env-denyoverride"></a>

CodeBuild 支援條件索引鍵，以檢查 API 請求中是否存在某些欄位。政策會在建立或更新專案時強制執行 VPC 要求。

```
{
    "Version": "2012-10-17"		 	 	 		 	 	 ,
    "Statement": [{
        "Effect": "Allow",
        "Action": [
            "codebuild:CreateProject",
            "codebuild:UpdateProject"
        ],
        "Resource": "*",
        "Condition": {
            "Null": {
                "codebuild:vpcConfig.vpcId": "false"
            }
        }
    }]
}
```