

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

# 建置您自己的自訂項目
<a name="cfcn-byo-customizations"></a>

若要建置自己的自訂，您可以透過新增或更新服務控制政策 (SCPs)、資源控制政策 (RCPs) CloudFormation 和資源來修改 CfCT `manifest.yaml` 檔案。對於必須部署的資源，您可以新增或移除帳戶和 OUs。您可以在套件資料夾中新增或修改範本、建立自己的資料夾，以及參考 `manifest.yaml` 檔案中的範本或資料夾。

本節說明建置自訂項目的兩個主要部分：
+ 如何為服務控制政策設定自己的組態套件
+ 如何為 AWS CloudFormation 堆疊集設定自己的組態套件

# 設定 SCPs或 RCPs 的組態套件
<a name="cfcn-set-up-custom-scps"></a>

本節說明如何建立服務控制政策 SCPs) 或資源控制政策 (RCPs組態套件。此程序的兩個主要部分是 (1) 準備 CfCT 資訊清單檔案，以及 (2) 準備您的資料夾結構。

## 步驟 1：編輯 manifest.yaml 檔案
<a name="cfct-byo-scp-step-1"></a>

使用範例`manifest.yaml`檔案做為起點。輸入所有必要的組態。新增 `resource_file`和 `deployment_targets`詳細資訊。

下列程式碼片段顯示預設資訊清單檔案。

```
---
region: us-east-1
version: 2021-03-15

resources: []
```

部署期間會自動`region`新增 的值。它必須符合您部署 CfCT 的區域。此區域必須與 AWS Control Tower 區域相同。

若要在存放在 Amazon S3 儲存貯體之 zip 套件的 `example-configuration` 資料夾中新增自訂 SCP 或 RCP，請開啟 `example-manifest.yaml` 檔案並開始編輯。

```
---
region: your-home-region
version: 2021-03-15

resources:
  - name: test-preventive-controls
    description: To prevent from deleting or disabling resources in member accounts
    resource_file: policies/preventive-controls.json
    deploy_method: scp | rcp
    #Apply to the following OU(s)
    deployment_targets:
      organizational_units: #array of strings
        - OUName1
        - OUName2 

…truncated…
```

下列程式碼片段顯示自訂資訊清單檔案的範例。您可以在單一變更中新增多個政策。

```
---
region: us-east-1
version: 2021-03-15

resources:
  - name: block-s3-public-access
    description: To S3 buckets to have public access
    resource_file: policies/block-s3-public.json
    deploy_method: scp | rcp
    #Apply to the following OU(s)
    deployment_targets:
      organizational_units: #array of strings
        - OUName1
        - OUName2
```

## 步驟 2：建立資料夾結構
<a name="cfct-byo-scp-step-2"></a>

如果您使用資源檔案的 Amazon S3 URL，以及搭配金鑰/值對使用**參數**，則可以略過此步驟。

您必須包含 JSON 格式的 SCP 政策或 RCP 政策以支援資訊清單，因為資訊清單檔案參考 JSON 檔案。確定檔案路徑符合資訊清單檔案中提供的路徑資訊。
+ *政策* JSON 檔案包含要部署到 OUs SCPs 或 RCPs。

下列程式碼片段顯示範例資訊清單檔案的資料夾結構。

```
- manifest.yaml
- policies/
   - block-s3-public.json
```

下列程式碼片段是`block-s3-public.json`政策檔案的範例。

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

****  

```
{
   "Version":"2012-10-17",		 	 	 
   "Statement":[
      {
         "Sid":"GuardPutAccountPublicAccessBlock",
         "Effect":"Deny",
         "Action":"s3:PutAccountPublicAccessBlock",
         "Resource":"arn:aws:s3:::*"
      }
   ]
}
```

------

# 設定 CloudFormation StackSets 的組態套件
<a name="cfcn-byo-cfn-stacksets"></a>

本節說明如何設定 CloudFormation StackSets 的組態套件。此程序的兩個主要部分是：(1) 準備資訊清單檔案，以及 (2) 更新資料夾結構。

## 步驟 1：編輯現有的資訊清單檔案
<a name="cfcn-byo-cfn-stacksets-step-1"></a>

將新的 CloudFormation StackSets 資訊新增至您先前編輯的資訊清單檔案。

僅供檢閱，下列程式碼片段包含先前顯示的相同自訂資訊清單檔案，用於設定 SCPs或 RCPs 的組態套件。現在您可以進一步編輯此檔案，以包含 資源的詳細資訊。

```
---
region: us-east-1
version: 2021-03-15

resources:
  
  - name: block-s3-public-access
    description: To S3 buckets to have public access
    resource_file: policies/block-s3-public.json
    deploy_method: scp | rcp
    #Apply to the following OU(s)
    deployment_targets:
    organizational_units: #array of strings
    - OUName1
    - OUName2
```

下列程式碼片段顯示已編輯的範例資訊清單檔案，其中包含`resources`詳細資訊。的順序會`resources`決定建立`resources`相依性的執行順序。您可以根據您的業務需求編輯下列範例資訊清單檔案。

```
---
region: your-home-region
version: 2021-03-15

…truncated…

resources:
  - name: stackset-1
    resource_file: templates/create-ssm-parameter-keys-1.template
    parameters:
      - parameter_key: parameter-1
        parameter_value: value-1
    deploy_method: stack_set
    deployment_targets:
      accounts: # array of strings, [0-9]{12}
        - account number or account name
        - 123456789123
      organizational_units: #array of strings, ou ids, ou-xxxx
        - OuName1
        - OUName2 
    export_outputs:
      - name: /org/member/test-ssm/app-id
        value: $[output_ApplicationId]
    regions:
      - region-name

  - name: stackset-2
    resource_file: s3://bucket-name/key-name
    parameters:
      - parameter_key: parameter-1
        parameter_value: value-1
    deploy_method: stack_set
    deployment_targets:
      accounts: # array of strings, [0-9]{12}
        - account number or account name
        - 123456789123
      organizational_units: #array of strings
        - OuName1
        - OUName2 
regions:
  - region-name
```

下列範例顯示您可以在資訊清單檔案中新增多個 CloudFormation 資源。

```
---
region: us-east-1
version: 2021-03-15

resources:
  - name: block-s3-public-access
    description: To S3 buckets to have public access
    resource_file: policies/block-s3-public.json
    deploy_method: scp | rcp
    #Apply to the following OU(s)
    deployment_targets:
      organizational_units: #array of strings
        - Custom
        - Sandbox

  - name: transit-network
    resource_file: templates/transit-gateway.template
    parameter_file: parameters/transit-gateway.json
    deploy_method: stack_set
    deployment_targets:
      accounts: # array of strings, [0-9]{12}
        - Prod
        - 123456789123 #Network
      organizational_units: #array of strings
        - Custom
    export_outputs:
      - name: /org/network/transit-gateway-id
        value: $[output_TransitGatewayID]
    regions:
      - us-east-1
```

## 步驟 2：更新資料夾結構
<a name="cfct-byo-cfn-stacksets-step-2"></a>

當您更新資料夾結構時，您可以包含資訊清單檔案中的所有支援 CloudFormation 範本檔案和 SCP 或 RCP 政策檔案。確認檔案路徑符合資訊清單檔案中提供的路徑。
+ *範本*檔案包含要在 OUs 和帳戶中部署 AWS 的資源。
+ *政策*檔案包含範本檔案中使用的輸入參數。

下列範例顯示在[步驟 1 ](#cfcn-byo-cfn-stacksets-step-1)中建立的範例資訊清單檔案的資料夾結構。

```
- manifest.yaml
- policies/
   - block-s3-public.json
- templates/
   - transit-gateway.template
```

# 「fred」協助程式和 CloudFormation 參數檔案
<a name="alfred-helper"></a>

 CfCT 為您提供稱為*預製*協助程式的機制，以取得 CloudFormation 範本中定義的 [SSM 參數存放區](https://docs.aws.amazon.com//systems-manager/latest/userguide/systems-manager-parameter-store.html)金鑰值。使用*預製*協助程式，您可以使用存放在 SSM 參數存放區中的值，無需更新 CloudFormation 範本。如需詳細資訊，請參閱*CloudFormation 《 使用者指南*》中的[什麼是 CloudFormation 範本？](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/gettingstarted.templatebasics.html#gettingstarted.templatebasics.what)。

**重要**  
 *預製*協助程式有兩個限制。參數僅適用於 AWS Control Tower 管理帳戶的主區域。最佳實務是考慮使用不會從堆疊執行個體變更為堆疊執行個體的值。當 'alfred' 協助程式擷取參數時，會從匯出變數的堆疊集選擇隨機堆疊執行個體。

## 範例
<a name="w2aac28c41c15c13b7"></a>

 假設您有兩個 CloudFormation 堆疊集。*堆疊集 1* 有一個堆疊執行個體，並部署到一個區域中的一個帳戶。它會在可用區域中建立 Amazon VPC 和子網路，且 `VPC ID`和 `subnet ID` 必須傳遞至*堆疊集 2* 做為參數值。在將 `VPC ID`和 `subnet ID` 傳遞至*堆疊集 2* 之前，`subnet ID`必須使用 將 `VPC ID`和 存放在*堆疊集 1* 中`AWS:::SSM::Parameter`。如需詳細資訊，請參閱《CloudFormation 使用者指南》**中的 [https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-ssm-parameter.html](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-ssm-parameter.html)。

**CloudFormation 堆疊集 1：**

 在下列程式碼片段中，*原始*協助程式可以從參數存放區取得 `VPC ID`和 的值`subnet ID`，並將其做為輸入傳遞至 StackSet 狀態機器。

```
VpcIdParameter:
    Type: AWS::SSM::Parameter
    Properties:
      Name: '/stack_1/vpc/id'
      Description: Contains the VPC id
      Type: String
      Value: !Ref MyVpc

SubnetIdParameter:
    Type: AWS::SSM::Parameter
    Properties:
      Name: '/stack_1/subnet/id'
      Description: Contains the subnet id
      Type: String
      Value: !Ref MySubnet
```

**CloudFormation 堆疊集 2：**

 程式碼片段顯示 CloudFormation 堆疊 2 `manifest.yaml`檔案中指定的參數。

```
parameters:
      - parameter_key: VpcId
        parameter_value: $[alfred_ssm_/stack_1/vpc/id]
      - parameter_key: SubnetId
        parameter_value: $[alfred_ssm_/stack_1/subnet/id]
```

**CloudFormation 堆疊集 2.1：**

 程式碼片段顯示您可以列出`alfred_ssm`屬性，以支援 *CommaDelimitedList* 類型的參數。如需詳細資訊，請參閱《CloudFormation 使用者指南》**中的 [https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/parameters-section-structure.html#parameters-section-structure-properties-type](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/parameters-section-structure.html#parameters-section-structure-properties-type)。

```
parameters:
      - parameter_key: VpcId # Type: String
        parameter_value: $[alfred_ssm_/stack_1/vpc/id']
      - parameter_key: SubnetId # Type: String
        parameter_value: $[ alfred_ssm_/stack_1/subnet/id']
      - parameter_key: AvailablityZones # Type: CommaDelimitedList
        parameter_value:   - "$[alfred_ssm_/availability_zone_1]"  - "$[alfred_ssm_/availability_zone_2]"
```

**自訂套件的 JSON 結構描述**  
CfCT 自訂套件的 JSON 結構描述位於 [ GitHub 的原始程式碼儲存庫](https://github.com/aws-solutions/aws-control-tower-customizations)中。您可以搭配許多您最愛的開發工具使用結構描述，而且您可能會發現在建置自己的 CfCT `manifest.yaml` 檔案時減少錯誤很有幫助。