

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

# 使用 AWS CodePipeline 和 Amazon Bedrock 以程式碼形式管理 AWS Organizations 政策
<a name="manage-organizations-policies-as-code"></a>

*Andre Cavalcante 和 Mariana Pessoa de Queiroz，Amazon Web Services*

## 總結
<a name="manage-organizations-policies-as-code-summary"></a>

您可以在 中使用*授權政策* AWS Organizations 來集中設定和管理成員帳戶中主體和資源的存取權。[服務控制政策 SCPs)](https://docs.aws.amazon.com/organizations/latest/userguide/orgs_manage_policies_scps.html) 定義組織中 AWS Identity and Access Management (IAM) 角色和使用者的最大可用許可。[資源控制政策 RCPs)](https://docs.aws.amazon.com/organizations/latest/userguide/orgs_manage_policies_rcps.html) 會定義組織中資源可用的最大許可。

此模式可協助您將 SCPs 和 RCPs 管理為透過持續整合和持續部署 (CI/CD) 管道部署的基礎設施即程式碼 (IaC)。透過使用 AWS CloudFormation 或 Hashicorp Terraform 來管理這些政策，您可以減輕與建立和維護多個授權政策相關的負擔。

此模式包含下列功能：
+ 您可以使用*資訊清單檔案* (`scp-management.json` 和 ) 建立、刪除和更新授權政策`rcp-management.json`。
+ 您使用護欄而非政策。您可以在資訊清單檔案中定義護欄及其目標。
+ 使用 AWS CodePipeline AWS CodeBuild 和 的管道會合併和最佳化資訊清單檔案中的護欄。對於資訊清單檔案中的每個陳述式，管道會將護欄合併為單一 SCP 或 RCP，然後將其套用至定義的目標。
+ AWS Organizations 會將政策套用至您的目標。*目標*可以是 AWS 帳戶、組織單位 (OU)、環境 （這是您在 `environments.json` 檔案中定義的一組帳戶或 OUs)，或共用[AWS 標籤](https://docs.aws.amazon.com/whitepapers/latest/tagging-best-practices/what-are-tags.html)的一組帳戶。
+ Amazon Bedrock 會讀取管道日誌並摘要所有政策變更。
+ 管道需要手動核准。核准者可以檢閱 Amazon Bedrock 準備的執行摘要，以協助他們了解變更。

## 先決條件和限制
<a name="manage-organizations-policies-as-code-prereqs"></a>

**先決條件**
+ 以組織形式管理 AWS 帳戶 的多個 AWS Organizations。如需詳細資訊，請參閱[建立組織](https://docs.aws.amazon.com/organizations/latest/userguide/orgs_manage_org_create.html)。
+ SCP 和 RCP 功能已在 中啟用 AWS Organizations。如需詳細資訊，請參閱[啟用政策類型](https://docs.aws.amazon.com/organizations/latest/userguide/enable-policy-type.html)。
+ [已安裝](https://developer.hashicorp.com/terraform/tutorials/aws-get-started/install-cli) Terraform 1.9.8 版或更新版本。
+ 如果您不是透過 Terraform 管道部署此解決方案，則 Terraform 狀態檔案必須[存放在](https://developer.hashicorp.com/terraform/language/backend/s3)部署政策管理管道 AWS 帳戶 的 中的 Amazon Simple Storage Service (Amazon S3) 儲存貯體中。
+ [已安裝](https://www.python.org/downloads/) Python 3.13.3 版或更新版本。

**限制**
+ 您無法使用此模式來管理在此 CI/CD 管道之外建立的 SCPs 或 RCPs。不過，您可以透過管道重新建立現有的政策。如需詳細資訊，請參閱此模式[額外資訊](#manage-organizations-policies-as-code-additional)區段中的*將現有政策遷移至管道*。
+ 每個帳戶中的帳戶、OUs 和政策數量受 [的配額和服務限制](https://docs.aws.amazon.com/organizations/latest/userguide/orgs_reference_limits.html)約束 AWS Organizations。
+ 此模式無法用於在 中設定[管理政策](https://docs.aws.amazon.com/organizations/latest/userguide/orgs_manage_policies_management_policies.html) AWS Organizations，例如備份政策、標籤政策、聊天應用程式政策或宣告政策。

## Architecture
<a name="manage-organizations-policies-as-code-architecture"></a>

下圖顯示政策管理管道及其相關聯資源的工作流程。

![\[透過政策管理管道釋出 SCPs RCPs。\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/images/pattern-img/372a1ace-5b2e-4f93-9f88-b5b0519ded48/images/a2cceb99-2b93-48e0-b072-bc61a572201f.png)


該圖顯示以下工作流程：

1. 使用者將變更遞交至遠端儲存庫主分支中的 `scp-management.json`或 `rcp-management.json` 資訊清單檔案。

1. `main` 分支的變更會在其中啟動管道 AWS CodePipeline。

1. CodePipeline 會啟動 `Validate-Plan` CodeBuild 專案。此專案使用遠端儲存庫中的 Python 指令碼來驗證政策和政策資訊清單檔案。此 CodeBuild 專案會執行下列動作：

   1. 檢查 SCP 和 RCP 資訊清單檔案是否包含唯一的陳述式 IDs(`Sid`)。

   1. 使用 `scp-policy-processor/main.py`和 `rcp-policy-processor/main.py` Python 指令碼，將護欄資料夾中的護欄串連為單一 RCP 或 SCP 政策。它結合了具有相同 `Resource`、 `Action`和 的護欄`Condition`。

   1. 使用 AWS Identity and Access Management Access Analyzer 驗證最終的最佳化政策。如果有任何問題清單，管道會停止。

   1. 建立 Terraform 用來建立資源的 `scps.json`和 `rcps.json` 檔案。

   1. 執行 `terraform plan`命令，這會建立 Terraform 執行計畫。

1. （選用） `Validate-Plan` CodeBuild 專案使用`bedrock-prompt/prompt.py`指令碼將提示傳送至 Amazon Bedrock。您可以在 `bedrock-prompt/prompt.txt` 檔案中定義提示。Amazon Bedrock 使用 Anthropic Claude Sonnet 3.5，透過分析 Terraform 和 Python 日誌來產生提議變更的摘要。

1. CodePipeline 使用 Amazon Simple Notification Service (Amazon SNS) 主題，以通知核准者必須檢閱變更。如果 Amazon Bedrock 產生變更摘要，通知會包含此摘要。

1. 政策核准者核准 CodePipeline 中的動作。如果 Amazon Bedrock 產生變更摘要，核准者可以在核准之前檢閱 CodePipeline 中的摘要。

1. CodePipeline 會啟動 `Apply` CodeBuild 專案。此專案使用 Terraform 來套用 RCP 和 SCP 變更 AWS Organizations。

與此架構相關聯的 IaC 範本也會部署支援政策管理管道的下列資源：
+ 用於存放 CodePipeline 成品和指令碼的 Amazon S3 儲存貯體，例如 `scp-policy-processor/main.py`和 `bedrock-prompt/prompt.py`
+ 加密此解決方案所建立資源的 AWS Key Management Service (AWS KMS) 金鑰

## 工具
<a name="manage-organizations-policies-as-code-tools"></a>

**AWS 服務**
+ [Amazon Bedrock](https://docs.aws.amazon.com/bedrock/latest/userguide/what-is-bedrock.html) 是一項全受管 AI 服務，可透過統一 API 使用許多高效能的基礎模型。
+ [AWS CodeBuild](https://docs.aws.amazon.com/codebuild/latest/userguide/welcome.html) 是一種全受管建置服務，可協助您編譯原始程式碼、執行單元測試，並產生準備好部署的成品。 
+ [AWS CodePipeline](https://docs.aws.amazon.com/codepipeline/latest/userguide/welcome.html) 可協助您快速建模和設定軟體版本的不同階段，並自動化持續發行軟體變更所需的步驟。
+ [AWS Organizations](https://docs.aws.amazon.com/organizations/latest/userguide/orgs_introduction.html) 是一種帳戶管理服務，可協助您將多個 合併 AWS 帳戶 到您建立並集中管理的組織。
+ [適用於 Python (Boto3) 的 AWS SDK](https://boto3.amazonaws.com/v1/documentation/api/latest/guide/quickstart.html) 是一種軟體開發套件，可協助您整合 Python 應用程式、程式庫或指令碼 AWS 服務。
+ [Amazon Simple Storage Service (Amazon S3)](https://docs.aws.amazon.com/AmazonS3/latest/userguide/Welcome.html) 是一種雲端型物件儲存服務，可協助您儲存、保護和擷取任何數量的資料。

**其他工具**
+ [HashiCorp Terraform](https://www.terraform.io/docs) 是一種 IaC 工具，可協助您使用程式碼來佈建和管理雲端基礎設施和資源。

**程式碼儲存庫**

此模式的程式碼可在 [organizations-policy-pipeline](https://github.com/aws-samples/organizations-policy-pipeline) GitHub 儲存庫中使用。以下是 `sample-repository` 資料夾中包含的金鑰檔案：
+ 在 `environments` 資料夾中， `environments.json`包含環境清單。*環境*是一組目標，它們可以包含 AWS 帳戶 IDs或組織單位 OUs)。
+ 在 `rcp-management`資料夾中：
  + `guardrails` 資料夾包含 RCPs 的個別護欄
  + `policies` 資料夾包含個別 RCPs
  + `rcp-management.json` 資訊清單檔案可協助您管理 RCP 護欄、完整 RCPs 及其相關聯的目標。
+ 在 `scp-management`資料夾中：
  + `guardrails` 資料夾包含 SCPs 的個別護欄
  + `policies` 資料夾包含個別 SCPs
  + `scp-management.json` 資訊清單檔案可協助您管理 SCP 護欄、完整 SCPs 及其相關聯的目標。
+ `utils` 資料夾包含的指令碼可協助您遷移目前的 SCPs 和 RCPs，以便您可以透過管道管理它們。如需詳細資訊，請參閱此模式[的其他資訊](#manage-organizations-policies-as-code-additional)一節。

## 最佳實務
<a name="manage-organizations-policies-as-code-best-practices"></a>
+ 在您設定管道之前，建議您確認尚未達到 AWS Organizations [配額](https://docs.aws.amazon.com/organizations/latest/userguide/orgs_reference_limits.html)的限制。
+ 我們建議您僅將 AWS Organizations 管理帳戶用於必須在該帳戶中執行的任務。如需詳細資訊，請參閱 [管理帳戶的最佳實務](https://docs.aws.amazon.com/organizations/latest/userguide/orgs_best-practices_mgmt-acct.html#bp_mgmt-acct_use-mgmt)。

## 史詩
<a name="manage-organizations-policies-as-code-epics"></a>

### 設定目標帳戶
<a name="set-up-the-target-account"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 建立 儲存庫。 | 建立安全操作團隊將從中管理政策的儲存庫。使用其中一個 AWS CodeConnections [支援](https://docs.aws.amazon.com/dtconsole/latest/userguide/supported-versions-connections.html)的第三方儲存庫提供者。 | DevOps 工程師 | 
| 委派政策管理。 | 將 AWS Organizations 政策的管理委派給您要部署管道的成員帳戶。如需說明，請參閱[使用 建立資源型委派政策 AWS Organizations](https://docs.aws.amazon.com/organizations/latest/userguide/orgs-policy-delegate.html)。如需範例政策，請參閱此模式[額外資訊](#manage-organizations-policies-as-code-additional)區段中的以*資源為基礎的委派政策範例*。 | AWS 管理員 | 
| （選用） 啟用基礎模型。 | 如果您想要產生政策變更的摘要，請在 AWS 帳戶 您要部署管道的 中，啟用 Amazon Bedrock 中 Anthropic Claude 3.5 Sonnet 基礎模型的存取權。如需說明，請參閱[新增或移除對 Amazon Bedrock 基礎模型的存取權](https://docs.aws.amazon.com/bedrock/latest/userguide/model-access-modify.html)。 | 一般 AWS | 

### 部署管道的資源
<a name="deploy-the-resources-for-the-pipeline"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 複製儲存庫。 | 輸入下列命令，從 GitHub 複製 [organizations-policy-pipeline ](https://github.com/aws-samples/organizations-policy-pipeline)儲存庫：`git clone https://github.com/aws-samples/organizations-policy-pipeline.git` | DevOps 工程師 | 
| 定義您的部署方法。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/manage-organizations-policies-as-code.html) | DevOps 工程師 | 
| 部署管道。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/manage-organizations-policies-as-code.html) | DevOps 工程師，Terraform | 
| 連接遠端儲存庫。 | 在上一個步驟中，Terraform 建立了與第三方儲存庫的 CodeConnections 連線。在[AWS 開發人員工具主控台](https://console.aws.amazon.com/codesuite/settings/connections)中，將連線的狀態從 變更為 `PENDING` `AVAILABLE`。如需說明，請參閱[更新待定連線](https://docs.aws.amazon.com/dtconsole/latest/userguide/connections-update.html)。 | AWS DevOps | 
| 訂閱 Amazon SNS 主題。 | Terraform 已建立 Amazon SNS 主題。訂閱 主題的端點並確認訂閱，讓核准者收到管道中待核准動作的通知。如需說明，請參閱[建立 Amazon SNS 主題的訂閱](https://docs.aws.amazon.com/sns/latest/dg/sns-create-subscribe-endpoint-to-topic.html)。 | 一般 AWS | 

### 定義您的護欄和政策
<a name="define-your-guardrails-and-policies"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 填入遠端儲存庫。 | 從複製的儲存庫，將`sample-repository`資料夾的內容複製到遠端儲存庫。這包括 `environments`、`scp-management`、 `rcp-management`和 `utils` 資料夾。 | DevOps 工程師 | 
| 定義您的環境。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/manage-organizations-policies-as-code.html) | DevOps 工程師 | 
| 定義您的護欄。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/manage-organizations-policies-as-code.html) | DevOps 工程師 | 
| 定義您的政策。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/manage-organizations-policies-as-code.html) | DevOps 工程師 | 

### 使用資訊清單檔案來管理政策
<a name="use-the-manifest-file-to-manage-the-policies"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 設定資訊清單檔案。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/manage-organizations-policies-as-code.html) | DevOps 工程師 | 
| 啟動管道。 | 遞交變更並推送至您在 `variables.tf` 檔案中定義的遠端儲存庫分支。一般而言，這是`main`分支。CI/CD 管道會自動啟動。如果有任何管道錯誤，請參閱此模式的[故障診斷](#manage-organizations-policies-as-code-troubleshooting)一節。 | DevOps 工程師 | 
| 核准變更。 | `Validate-Plan` CodeBuild 專案完成後，政策核准者會透過您先前設定的 Amazon SNS 主題收到通知。請執行下列操作：[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/manage-organizations-policies-as-code.html) | 一般 AWS、政策核准者 | 
| 驗證部署。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/manage-organizations-policies-as-code.html) | 一般 AWS | 

## 疑難排解
<a name="manage-organizations-policies-as-code-troubleshooting"></a>


| 問題 | 解決方案 | 
| --- | --- | 
| 管道`Validate-Plan`階段中的資訊清單檔案錯誤 | 如果 `scp-management.json`或 檔案有任何錯誤，「資訊清單檔案驗證與計劃階段的管道錯誤」訊息會顯示在管道輸出中`rcp-management.json`。可能的錯誤包括不正確的環境名稱、重複SIDs 或無效的欄位或值。請執行下列操作：[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/manage-organizations-policies-as-code.html) | 
| 管道`Validate-Plan`階段中的 IAM Access Analyzer 調查結果 | 如果護欄或政策定義中有任何錯誤，「在驗證與計劃階段期間 IAM Access Analyzer 中尋找」訊息會顯示在管道輸出中。此模式使用 IAM Access Analyzer 來驗證最終政策。請執行下列操作：[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/manage-organizations-policies-as-code.html) | 

## 相關資源
<a name="manage-organizations-policies-as-code-resources"></a>
+ [JSON 政策元素參考](https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_policies_elements.html) (IAM 文件）
+ [資源控制政策](https://docs.aws.amazon.com/organizations/latest/userguide/orgs_manage_policies_rcps.html) (AWS Organizations 文件）
+ [服務控制政策](https://docs.aws.amazon.com/organizations/latest/userguide/orgs_manage_policies_scps.html) (AWS Organizations 文件）
+ [新增或移除對 Amazon Bedrock 基礎模型的存取權](https://docs.aws.amazon.com/bedrock/latest/userguide/model-access-modify.html) (Amazon Bedrock 文件）
+ 在[ CodePipeline (CodePipeline 文件） 中核准或拒絕核准動作](https://docs.aws.amazon.com/codepipeline/latest/userguide/approvals-approve-or-reject.html) CodePipeline 

## 其他資訊
<a name="manage-organizations-policies-as-code-additional"></a>

**以資源為基礎的委派政策範例**

以下是 的範例資源型委派政策 AWS Organizations。它允許委派的管理帳戶管理組織的 SCPs RCPs。在下列範例政策中，將 `<MEMBER_ACCOUNT_ID>`取代為您部署政策管理管道的帳戶 ID。

```
{
  "Version": "2012-10-17",		 	 	 
  "Statement": [
    {
      "Sid": "DelegationToAudit",
      "Effect": "Allow",
      "Principal": {
        "AWS": "arn:aws:iam::<MEMBER_ACCOUNT_ID>:root"
      },
      "Action": [
        "organizations:ListTargetsForPolicy",
        "organizations:CreatePolicy",
        "organizations:DeletePolicy",
        "organizations:AttachPolicy",
        "organizations:DetachPolicy",
        "organizations:DisablePolicyType",
        "organizations:EnablePolicyType",
        "organizations:UpdatePolicy",
        "organizations:DescribeEffectivePolicy",
        "organizations:DescribePolicy",
        "organizations:DescribeResourcePolicy"
      ],
      "Resource": "*"
    }
  ]
}
```

**將現有政策遷移至管道**

如果您有要透過此管道遷移和管理的現有 SCPs 或 RCPs，您可以使用程式碼儲存庫 `sample-repository/utils` 資料夾中的 Python 指令碼。這些指令碼包括：
+ `check-if-scp-exists-in-env.py` – 此指令碼會檢查指定的政策是否適用於您在 `environments.json` 檔案中定義的特定環境中的任何目標。輸入下列命令來執行此指令碼：

  ```
  python3 check-if-scp-exists-in-env.py \
     --policy-type <POLICY_TYPE> \
     --policy-name <POLICY_NAME> \
     --env-id <ENV_ID>
  ```

  在此命令中取代以下內容：
  + `<POLICY_TYPE>` 是 `scp` 或 `rcp`
  + `<POLICY_NAME>` 是 SCP 或 RCP 的名稱
  + `<ENV_ID>` 是您在 `environments.json` 檔案中定義的環境 ID
+ `create-environments.py` – 此指令碼會根據您環境中目前的 SCPs和 RCPs 建立 environment.json 檔案。它不包括透過 部署的政策 AWS Control Tower。輸入下列命令來執行此指令碼，其中 `<POLICY_TYPE>`為 `scp`或 `rcp`：

  ```
  python create-environments.py --policy-type <POLICY_TYPE>
  ```
+ `verify-policies-capacity.py` – 此指令碼會檢查您定義的每個環境，以判斷每個 AWS Organizations 政策相關配額的剩餘容量。您可以定義要在 `environments.json` 檔案中檢查的環境。輸入下列命令來執行此指令碼，其中 `<POLICY_TYPE>`為 `scp`或 `rcp`：

  ```
  python verify-policies-capacity.py --policy-type <POLICY_TYPE>
  ```