

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

# 具有服務受管許可的 Create CloudFormation StackSets
<a name="stacksets-orgs-associate-stackset-with-org"></a>

透過*服務受管*許可，您可以將堆疊部署到 AWS Organizations 特定區域中由 管理的帳戶。使用此模型，您不需要在每個目標帳戶和 AWS 區域中建立 IAM 角色。CloudFormation 會代表您建立 IAM 角色。如需詳細資訊，請參閱[啟用受信任存取](stacksets-orgs-activate-trusted-access.md)。

**Topics**
+ [考量事項](#stacksets-orgs-considerations)
+ [建立具有服務受管許可的 StackSet (主控台)](#stacksets-orgs-associate-stackset-with-org-console)
+ [建立具有服務受管許可的 StackSet (AWS CLI)](#stacksets-orgs-associate-stackset-with-org-cli)

## 考量事項
<a name="stacksets-orgs-considerations"></a>

建立具有服務受管許可的 StackSet 之前，請考慮下列事項：
+ 具有服務受管許可的 StackSets 可由組織的管理帳戶或委派的管理員帳戶啟動，但所有操作均由管理帳戶執行。
+ CloudFormation 不會將堆疊部署到管理帳戶，即使該帳戶是您的組織的一部分或屬於組織單位。
+ StackSet 可以將整個組織 (包括所有帳戶) 或指定的 OU 設為目標。當 StackSet 以父 OU 為目標時，會自動包含任何子 OU。根據預設，當 StackSet 以特定 OU 為目標時，它會包含這些 OU 中的所有帳戶。不過，您可使用帳戶篩選選項將特定帳戶設為目標。
+ 多個 StackSets 可以將相同的組織或 OU 設為目標。
+ 您無法以組織外部的帳戶為目標。
+ 您部署 StackSet 的權限取決於您用來登入管理帳戶之 IAM 主體實體 (使用者、角色或群組) 的許可。關於授與許可部署至組織的 IAM 政策範例，請參閱[根據區域和資源類型限制堆疊集操作](security_iam_id-based-policy-examples.md#resource-level-permissions-service-managed-stack-set)。
+ 委派管理員具有部署至組織中任何帳戶的完整許可。管理帳戶無法限制部署至特定 OU 或 StackSet 操作的委派管理員許可。
+ 自動部署設定是在 StackSet 層級套用。您無法特別針對 OU、帳戶或區域來調整自動部署。
+ 使用服務受管許可的 StackSets 不支援包含了巨集或轉換的巢狀堆疊或範本。

## 建立具有服務受管許可的 StackSet (主控台)
<a name="stacksets-orgs-associate-stackset-with-org-console"></a>

**建立 StackSet**

1. 登入 AWS 管理主控台 並在 https：//[https://console.aws.amazon.com/cloudformation](https://console.aws.amazon.com/cloudformation/) 開啟 CloudFormation 主控台。

1. 在畫面頂端的導覽列上，選擇您要從中管理 StackSet 的 AWS 區域 。

1. 從導覽窗格選擇 **StackSets**。

1. 從 **StackSets** 頁面上方選擇**建立 StackSet**。

1. 在 **Permissions (許可)** 下，選擇 **Service-managed permissions (服務管理許可)**。
**注意**  
如果 AWS Organizations 停用 的受信任存取，則會顯示橫幅。需要信任存取，才能建立或更新具有服務受管許可的 StackSet。只有組織管理帳戶中的管理員，才具有 [使用 啟用 StackSets 的受信任存取 AWS Organizations](stacksets-orgs-activate-trusted-access.md) 的許可。

1. 在**準備範本**下，請選擇**範本已準備就緒**。

1. 在 **Specify template (指定範本)** 下，選擇指定堆疊範本所在 S3 儲存貯體的 URL，或上傳堆疊範本檔案。然後選擇**下一步**。

1. 在**指定 StackSet 詳細資訊**頁面上，提供 StackSet 的名稱、指定任何參數，然後選擇**下一步**。

1. 在 **Configure StackSet options (設定 StackSet 選項)** 頁面的 **Tags (標籤)** 下，指定任何標籤以套用至堆疊中的資源。如需如何在 中使用標籤的詳細資訊 AWS，請參閱*AWS 帳單與成本管理 《 使用者指南*》中的[使用成本分配標籤組織和追蹤 AWS 成本](https://docs.aws.amazon.com/awsaccountbilling/latest/aboutv2/cost-alloc-tags.html)。

1. 對於**執行組態**，選擇**作用中**，以啟用 CloudFormation 的最佳化操作處理：
   + 非衝突操作會同時執行，以便加快部署時間。
   + 衝突操作會根據請求的順序，自動排入佇列和處理。

   在有執行中或排入佇列的操作時，CloudFormation 會將所有傳入操作排入佇列，即使這些操作沒有衝突也一樣。在此期間，您無法變更執行設定。

1. 如果範本包含 IAM 資源，在**功能**中選擇**我知道此範本可建立 IAM 資源**，以指定您要使用此範本中的 IAM 資源。如需詳細資訊，請參閱[認可 CloudFormation 範本中的 IAM 資源](control-access-with-iam.md#using-iam-capabilities)。

1. 選擇**下一步**以繼續，並啟用受信任存取 (如果尚未啟用)。

1. 在**設定部署選項**頁面，**部署目標**下，執行下列其中一項操作：
   + 選擇**部署至組織**，以部署至組織中的所有帳戶。
   + 選擇**部署至組織單位**，以部署至特定 OU 中的所有帳戶。選擇 **Add an OU (新增 OU)**，然後將目標 OU ID 貼到文字方塊中。針對每個新的目標 OU 重複上述步驟。

   若您選擇**部署到組織單位**，對於**帳戶篩選類型**，您可以選擇下列其中一個選項並提供帳戶號碼，將部署目標設定為特定個別帳戶。
   + **無** (預設) – 將堆疊部署到指定 OU 中的所有帳戶。
   + **交集** – 將堆疊部署到所選 OU 中的特定個別帳戶。
   + **差集** – 將堆疊部署到所選 OU 中的所有帳戶，但特定帳戶除外。
   + **聯集** – 將堆疊部署到指定的 OU 和其他個別帳戶。

1. 在**自動部署**下，選擇是否自動部署到未來新增至目標組織或 OU 的帳戶。如需詳細資訊，請參閱[在 中啟用或停用 StackSets 的自動部署 AWS Organizations](stacksets-orgs-manage-auto-deployment.md)。

1. 如果您已啟用自動部署，請在 **Account removal behavior (帳戶移除行為)** 下，選擇在從目標組織或 OU 移除帳戶時，是否保留或刪除堆疊資源。
**注意**  
如果選取**保留堆疊**，則會從 StackSet 移除堆疊執行個體，但會保留堆疊及其相關資源。資源會保持在當前狀態，但不再是 StackSet 的一部分。

1. 在**指定區域**下，選擇要部署堆疊的區域。

1. 對於**部署選項**，執行下列動作：
   + 針對**並行帳戶數目上限**，指定並行處理的帳戶數量。
   + 針對**容錯能力**，指定每個區域允許的帳戶失敗數目上限。達到此限制後，操作將停止，且不會繼續前往其他區域。
   + 對於**區域並行**，選擇如何處理區域：**循序** (一次處理一個區域) 或**平行** (同時處理多個區域)。
   + 針對**並行模式**，選擇並行在操作執行期間的行為方式。
     + **嚴格容錯能力** – 降低發生故障時的帳戶並行層級，保持在**容錯能力** \$11 的範圍內。
     + **軟性容錯能力** – 無論失敗為何，維持您指定的並行層級 (**最大並行帳戶**的值)。
   + 對於 StackSet **相依性**，新增相依的 StackSet ARNs，最多保持在 10 個相依性內。如需詳細資訊，請參閱[在 中啟用或停用 StackSets 的自動部署 AWS Organizations](stacksets-orgs-manage-auto-deployment.md)。

1. 選擇 **Next** (下一步) 繼續。

1. 在**檢閱**頁面上，確認 StackSet 將部署到正確區域中的正確帳戶，然後選擇**建立 StackSet**。

   **StackSet details (StackSet 詳細資訊)** 頁面隨即開啟。您可以檢視 StackSet 的堆疊建立進度和狀態。

## 建立具有服務受管許可的 StackSet (AWS CLI)
<a name="stacksets-orgs-associate-stackset-with-org-cli"></a>

請依照本節中的步驟使用 AWS CLI 來：
+ 建立 StackSet 容器。
+ 部署堆疊執行個體。

**注意**  
做為委派管理員時，您必須在命令中包含 `--call-as DELEGATED_ADMIN`。

------
#### [ Deploy to your organization ]

**建立 StackSet**

1. 使用 [https://docs.aws.amazon.com/cli/latest/reference/cloudformation/create-stack-set.html](https://docs.aws.amazon.com/cli/latest/reference/cloudformation/create-stack-set.html) 命令來建立新的 StackSet，並命名為 `my-stackset`。下列範例使用儲存在 S3 儲存貯體中的範本，啟用自動部署，並在移除帳戶時保留堆疊。如需詳細資訊，請參閱[在 中啟用或停用 StackSets 的自動部署 AWS Organizations](stacksets-orgs-manage-auto-deployment.md)。

   ```
   aws cloudformation create-stack-set \
     --stack-set-name my-stackset \
     --template-url https://s3.region-code.amazonaws.com/amzn-s3-demo-bucket/MyApp.template \
     --permission-model SERVICE_MANAGED \
     --auto-deployment Enabled=true,RetainStacksOnAccountRemoval=true,DependsOn=ARN1,ARN2
   ```

1. 使用 [https://docs.aws.amazon.com/cli/latest/reference/cloudformation/list-stack-sets.html](https://docs.aws.amazon.com/cli/latest/reference/cloudformation/list-stack-sets.html) 命令確認您的 StackSet 已建立。結果中會列出新的 StackSet。

   ```
   aws cloudformation list-stack-sets
   ```
   + 如果您在登入會員帳戶時，將 `--call-as` 選項設定為 `DELEGATED_ADMIN`，則 **list-stack-sets** 會傳回組織管理帳戶中具有服務管理許可的所有 StackSets。
   + 如果您在登入 AWS 帳戶時，將 `--call-as` 選項設定為 `SELF`，則 **list-stack-sets** 會傳回 AWS 帳戶中的所有自我管理 StackSets。
   + 如果您在登入組織的管理帳戶時，將 `--call-as` 選項設定為 `SELF`，則 **list-stack-sets** 會傳回組織 管理帳戶 中的所有 StackSets。

1. 使用 [https://docs.aws.amazon.com/cli/latest/reference/cloudformation/create-stack-instances.html](https://docs.aws.amazon.com/cli/latest/reference/cloudformation/create-stack-instances.html) 命令在 StackSet 中新增堆疊。針對 `--deployment-targets` 選項，指定要部署到組織中所有帳戶的組織根 ID。

   使用 `--operation-preferences` 選項，設定並行帳戶處理和其他部署偏好設定。此範例使用基於數量的設定。請注意，`MaxConcurrentCount` 不得超過 `FailureToleranceCount` \$1 1。對於以百分比為基礎的設定，請使用 `FailureTolerancePercentage` 或 `MaxConcurrentPercentage`。

   ```
   aws cloudformation create-stack-instances --stack-set-name my-stackset \
     --deployment-targets OrganizationalUnitIds=r-a1b2c3d4e5 \
     --regions us-west-2 us-east-1 \
     --operation-preferences MaxConcurrentCount=1,FailureToleranceCount=0
   ```

   如需詳細資訊，請參閱《*AWS CloudFormation API 參考*》中的 [CreateStackInstances](https://docs.aws.amazon.com/AWSCloudFormation/latest/APIReference/API_CreateStackInstances.html)。

1. 使用 **create-stack-instances** 輸出中傳回的 `operation-id`，使用下列 [https://docs.aws.amazon.com/cli/latest/reference/cloudformation/describe-stack-set-operation.html](https://docs.aws.amazon.com/cli/latest/reference/cloudformation/describe-stack-set-operation.html) 命令，以驗證是否已成功建立堆疊。

   ```
   aws cloudformation describe-stack-set-operation \
     --stack-set-name my-stackset \
     --operation-id operation_ID
   ```

------
#### [ Deploy to organizational units (OUs) ]

**建立 StackSet**

1. 使用 [https://docs.aws.amazon.com/cli/latest/reference/cloudformation/create-stack-set.html](https://docs.aws.amazon.com/cli/latest/reference/cloudformation/create-stack-set.html) 命令來建立新的 StackSet，並命名為 `my-stackset`。下列範例使用儲存於 S3 儲存貯體中的範本，並包含一個參數，該參數會將 `KeyPairName` 設定為數值 `TestKey`

   ```
   aws cloudformation create-stack-set \
     --stack-set-name my-stackset \
     --template-url https://s3.region-code.amazonaws.com/amzn-s3-demo-bucket/MyApp.template \
     --permission-model SERVICE_MANAGED \
     --parameters ParameterKey=KeyPairName,ParameterValue=TestKey
   ```

1. 使用 [https://docs.aws.amazon.com/cli/latest/reference/cloudformation/list-stack-sets.html](https://docs.aws.amazon.com/cli/latest/reference/cloudformation/list-stack-sets.html) 命令確認您的 StackSet 已建立。結果中會列出新的 StackSet。

   ```
   aws cloudformation list-stack-sets
   ```
   + 如果您在登入會員帳戶時，將 `--call-as` 選項設定為 `DELEGATED_ADMIN`，則 **list-stack-sets** 會傳回組織管理帳戶中具有服務管理許可的所有 StackSets。
   + 如果您在登入 AWS 帳戶時，將 `--call-as` 選項設定為 `SELF`，則 **list-stack-sets** 會傳回 AWS 帳戶中的所有自我管理 StackSets。
   + 如果您在登入組織的管理帳戶時，將 `--call-as` 選項設定為 `SELF`，則 **list-stack-sets** 會傳回組織 管理帳戶 中的所有 StackSets。

1. 使用 [https://docs.aws.amazon.com/cli/latest/reference/cloudformation/create-stack-instances.html](https://docs.aws.amazon.com/cli/latest/reference/cloudformation/create-stack-instances.html) 命令在 StackSet 中新增堆疊。針對 `--deployment-targets` 選項，指定要部署的 OU ID。

   使用 `--operation-preferences` 選項，設定並行帳戶處理和其他部署偏好設定。此範例使用基於數量的設定。請注意，`MaxConcurrentCount` 不得超過 `FailureToleranceCount` \$1 1。對於以百分比為基礎的設定，請使用 `FailureTolerancePercentage` 或 `MaxConcurrentPercentage`。

   ```
   aws cloudformation create-stack-instances --stack-set-name my-stackset \
     --deployment-targets OrganizationalUnitIds=ou-rcuk-1x5j1lwo,ou-rcuk-slr5lh0a \
     --regions us-west-2 us-east-1 \
     --operation-preferences MaxConcurrentCount=1,FailureToleranceCount=0
   ```

   如需詳細資訊，請參閱《*AWS CloudFormation API 參考*》中的 [CreateStackInstances](https://docs.aws.amazon.com/AWSCloudFormation/latest/APIReference/API_CreateStackInstances.html)。

1. 使用 **create-stack-instances** 輸出中傳回的 `operation-id`，使用下列 [https://docs.aws.amazon.com/cli/latest/reference/cloudformation/describe-stack-set-operation.html](https://docs.aws.amazon.com/cli/latest/reference/cloudformation/describe-stack-set-operation.html) 命令，以驗證是否已成功建立堆疊。

   ```
   aws cloudformation describe-stack-set-operation \
     --stack-set-name my-stackset \
     --operation-id operation_ID
   ```

------
#### [ Deploy to specific accounts in OUs ]

您可以指定特定組織單位並使用帳戶篩選，精確控制哪些帳戶會接收堆疊部署。根據預設，如果未指定帳戶篩選，堆疊會部署到指定 OU 中的所有帳戶。

在 中 AWS CLI，您可以使用 `--deployment-targets`選項指定帳戶篩選。關於詳細資訊，請參閱 [DeploymentTargets](https://docs.aws.amazon.com/AWSCloudFormation/latest/APIReference/API_DeploymentTargets.html)。

使用 **create-stack-set** 命令建立 StackSet 容器之後，請使用下列其中一個範例，將堆疊部署至特定帳戶。

**以 OU 中的特定帳戶為目標**  
以下範例只會將堆疊部署到 OU1 中的帳戶 A1 和 A2。

```
aws cloudformation create-stack-instances --stack-set-name my-stackset \
  --deployment-targets OrganizationalUnitIds=OU1,Accounts=A1,A2,AccountFilterType=INTERSECTION \
  --regions us-west-2 us-east-1
```

**從 OU 排除帳戶**  
下列範例會將堆疊部署到 OU1 中的所有帳戶，帳戶 A1 和 A2 除外。

```
aws cloudformation create-stack-instances --stack-set-name my-stackset \
  --deployment-targets OrganizationalUnitIds=OU1,Accounts=A1,A2,AccountFilterType=DIFFERENCE \
  --regions us-west-2 us-east-1
```

------