

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

# 使用 Troposphere 產生包含 AWS Config 受管規則的 AWS CloudFormation 範本
<a name="generate-an-aws-cloudformation-template-containing-aws-config-managed-rules-using-troposphere"></a>

*Lucas Nation 和 Freddie Wilson，Amazon Web Services*

## 摘要
<a name="generate-an-aws-cloudformation-template-containing-aws-config-managed-rules-using-troposphere-summary"></a>

許多組織使用 [AWS Config 受](https://docs.aws.amazon.com/config/latest/developerguide/evaluate-config_use-managed-rules.html)管規則，根據常見最佳實務評估其 Amazon Web Services (AWS) 資源的合規性。不過，這些規則可能會耗時維護，此模式可協助您利用 Python 程式庫 [Troposphere](https://troposphere.readthedocs.io/en/latest/quick_start.html) 來產生和管理 AWS Config 受管規則。

模式可協助您使用 Python 指令碼，將包含 AWS Config受管規則的 Microsoft Excel 試算表轉換為 AWS CloudFormation 範本，以管理您的 AWS Config 受管規則。Troposphere 做為基礎設施做為程式碼 (IaC)，這表示您可以使用受管規則更新 Excel 試算表，而不是使用 JSON 或 YAML 格式的檔案。然後，您可以使用 範本來啟動 AWS CloudFormation 堆疊，以建立和更新 AWS 帳戶中的受管規則。

AWS CloudFormation 範本使用 Excel 試算表定義每個 AWS Config 受管規則，並協助您避免在 AWS 管理主控台中手動建立個別規則。指令碼會將每個受管規則的參數預設為空白字典，以及來自 的範圍的`ComplianceResourceTypes`預設值`THE_RULE_IDENTIFIER.template file`*。 *如需規則識別符的詳細資訊，請參閱 [ AWS Config 文件中的使用 AWS CloudFormation 範本建立 AWS Config 受管規則](https://docs.aws.amazon.com/config/latest/developerguide/aws-config-managed-rules-cloudformation-templates.html)。 AWS Config 

## 先決條件和限制
<a name="generate-an-aws-cloudformation-template-containing-aws-config-managed-rules-using-troposphere-prereqs"></a>

**先決條件**
+ 作用中的 AWS 帳戶
+ 熟悉使用 AWS CloudFormation 範本建立 AWS Config 受管規則。如需詳細資訊，請參閱 [ AWS Config 文件中的使用 AWS CloudFormation 範本建立 AWS Config 受管規則](https://docs.aws.amazon.com/config/latest/developerguide/aws-config-managed-rules-cloudformation-templates.html)。 AWS Config  
+ Python 3，已安裝和設定。如需詳細資訊，請參閱 [Python 文件](https://www.python.org/)。
+ 現有的整合開發環境 (IDE)。 
+ 在範例 `excel_config_rules.xlsx` Excel 試算表 (OUs)。

## 史詩
<a name="generate-an-aws-cloudformation-template-containing-aws-config-managed-rules-using-troposphere-epics"></a>

### 自訂和設定 AWS Config 受管規則
<a name="customize-and-configure-the-aws-config-managed-rules"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 更新範例 Excel 試算表。 | 下載範例 `excel_config_rules.xlsx` Excel 試算表 （已連接），並標記為您要使用的 `Implemented` AWS Config 受管規則。 <br />標記為 的規則`Implemented`將新增至 AWS CloudFormation 範本。 | 開發人員 | 
| （選用） 使用 AWS Config 規則參數更新 config\_rules\_params.json 檔案。 | 有些 AWS Config 受管規則需要參數，應該使用 `--param-file`選項以 JSON 檔案的形式傳遞至 Python 指令碼。例如，`access-keys-rotated`受管規則使用下列`maxAccessKeyAge`參數：<pre>{<br />         "access-keys-rotated": {<br />             "InputParameters": {<br />                 "maxAccessKeyAge": 90<br />             }<br />         }<br />     }</pre><br />在此範例參數中， `maxAccessKeyAge`設定為 90 天。指令碼會讀取參數檔案`InputParameters`，並新增它找到的任何 。 | 開發人員 | 
| （選用） 使用 AWS Config ComplianceResourceTypes 更新 config\_rules\_params.json 檔案。 ComplianceResourceTypes | 根據預設，Python 指令碼`ComplianceResourceTypes`會從 AWS 定義的範本擷取 。如果您想要覆寫特定 AWS Config 受管規則的範圍，則需要使用 `--param-file`選項將其做為 JSON 檔案傳遞至 Python 指令碼。<br />例如，下列範例程式碼顯示 `ComplianceResourceTypes`的 `ec2-volume-inuse-check` 如何設定為 `["AWS::EC2::Volume"]`清單：<pre>{<br />         "ec2-volume-inuse-check": {<br />             "Scope": {<br />                 "ComplianceResourceTypes": [<br />                     "AWS::EC2::Volume"<br />                 ]<br />             }<br />         }<br />     }</pre> | 開發人員 | 

### 執行 Python 指令碼
<a name="run-the-python-script"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 從 requirements.txt 檔案安裝 pip 套件。 | 下載 `requirements.txt` 檔案 （已連接），並在 IDE 中執行下列命令來安裝 Python 套件：<br />`pip3 install -r requirements.txt` | 開發人員 | 
| 執行 Python 指令碼。 | [See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/generate-an-aws-cloudformation-template-containing-aws-config-managed-rules-using-troposphere.html)您也可以新增下列選用參數：[See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/generate-an-aws-cloudformation-template-containing-aws-config-managed-rules-using-troposphere.html) | 開發人員 | 

### 部署 AWS Config 受管規則
<a name="deploy-the-aws-config-managed-rules"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 啟動 AWS CloudFormation 堆疊。 | [See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/generate-an-aws-cloudformation-template-containing-aws-config-managed-rules-using-troposphere.html) | 開發人員 | 

## 附件
<a name="attachments-07c1cfff-fc9e-4a1f-bd36-48f025808bd8"></a>

若要存取與本文件相關聯的其他內容，請解壓縮下列檔案： [attachment.zip](samples/p-attach/07c1cfff-fc9e-4a1f-bd36-48f025808bd8/attachments/attachment.zip)