

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

# 使用 AWS Config 為第三方資源使用 記錄組態 AWS CLI
<a name="customresources"></a>

記錄第三方資源或自訂資源類型的組態，例如內部部署伺服器、SAAS 監控工具和版本控制系統 (如 GitHub)。

您可以使用 AWS Config 主控台 AWS Config 和 APIs，將第三方資源的組態資料發佈至 ，並檢視和監控資源庫存和組態歷史記錄。您可以使用 AWS Config 來管理所有資源，並使用 AWS Config 規則評估資源組態是否符合最佳實務。您也可以建立 AWS Config 規則或一致性套件，根據最佳實務、內部政策和法規政策來評估這些第三方資源。

**注意**  
如果您已設定 AWS Config 記錄所有資源類型，則透過 管理 （建立、更新或刪除） 的第三方資源 CloudFormation 會在 中自動追蹤 AWS Config 為組態項目。

**先決條件：**必須使用 註冊第三方資源或自訂資源類型 CloudFormation。

**Topics**
+ [新增資源](customresources-adding.md)
+ [錄製組態項目](add-custom-resource-type-cli.md)
+ [讀取組態項目](view-custom-resource-type-cli.md)
+ [刪除資源](delete-custom-resource-type.md)

# 將第三方資源新增至 AWS Config
<a name="customresources-adding"></a>

請依照下列步驟將第三方資源新增至其中 AWS Config。

**Topics**
+ [步驟 1：設定您的開發環境](#customresources-setupdevenvironment)
+ [步驟 2：建立資源模型](#customresources-modelresource)
+ [步驟 3：產生成品](#generateartifactsforcfnsubmit)
+ [步驟 4：註冊您的資源](#customresources-registerresource)
+ [步驟 5：發佈資源組態](#customresources-publishresourceconfiguration)

## 步驟 1：設定您的開發環境
<a name="customresources-setupdevenvironment"></a>

安裝及設定 CloudFormation AWS CLI。 AWS CLI 可讓您建立模型並註冊自訂資源。如需詳細資訊，請參閱《[自訂資源](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/template-custom-resources.html)》和《[什麼是 CloudFormation 命令列介面？](https://docs.aws.amazon.com/cloudformation-cli/latest/userguide/what-is-cloudformation-cli.html)》。

## 步驟 2：建立資源模型
<a name="customresources-modelresource"></a>

建立符合並驗證資源類型組態的資源提供者結構描述。

1. 使用 `init` 命令建立您的資源提供者專案，並產生所需的檔案。

   ```
   $ cfn init
   Initializing new project
   ```

1. `init` 命令會啟動精靈，引導您完成專案的設定，包括指定資源名稱。在此演練中，指定 `MyCustomNamespace::Testing::WordPress`。

   ```
   Enter resource type identifier (Organization::Service::Resource): MyCustomNamespace::Testing::WordPress
   ```

1. 輸入資源的套件名稱。

   ```
   Enter a package name (empty for default 'com.custom.testing.wordpress'): com.custom.testing.wordpress 
   Initialized a new project in /workplace/user/custom-testing-wordpress
   ```
**注意**  
為了保證任何專案相依項都能正確解析，您可以使用 Maven 支援將產生的專案匯入 IDE。  
例如，如果您正在使用 IntelliJ IDEA，您將需要執行下列動作：  
從 **檔案** 選單內選擇 **新建**，再選擇 **現存來源的專案**。
導覽至專案目錄
在 **匯入專案** 對話方塊中，選擇 **從外部模型匯入專案**，再選擇 **Maven**。
選擇 **下一步** 並接受任何預設值，以完成專案匯入。

1. 開啟包含資源結構描述的 `mycustomnamespace-testing-wordpress.json` 檔案。將下列結構描述複製並貼到 `mycustomnamespace-testing-wordpress.json` 中。

   ```
   {
     "typeName": "MyCustomNamespace::Testing::WordPress",
     "description": "An example resource that creates a website based on WordPress 5.2.2.",
     "properties": {
       "Name": {
         "description": "A name associated with the website.",
         "type": "string",
         "pattern": "^[a-zA-Z0-9]{1,219}\\Z",
         "minLength": 1, "maxLength": 219
       },
       "SubnetId": {
         "description": "A subnet in which to host the website.",
         "pattern": "^(subnet-[a-f0-9]{13})|(subnet-[a-f0-9]{8})\\Z",
         "type": "string"
       },
       "InstanceId": {
         "description": "The ID of the instance that backs the WordPress site.",
         "type": "string"
       },
       "PublicIp": {
         "description": "The public IP for the WordPress site.",
         "type": "string"
       }
     },
     "required": [ "Name", "SubnetId" ],
     "primaryIdentifier": [ "/properties/PublicIp", "/properties/InstanceId" ],
     "readOnlyProperties": [ "/properties/PublicIp", "/properties/InstanceId" ],
     "additionalProperties": false
   }
   ```

1. 驗證結構描述。

   ```
   $ cfn validate
   ```

1. 更新資源提供者套件中自動產生的檔案，以檢視資源提供者結構描述更新。啟動資源提供者專案時， AWS CLI 會產生資源提供者的支援檔案和程式碼。重新產生程式碼以查看更新的結構描述。

   ```
   $ cfn generate
   ```
**注意**  
使用 Maven 時，作為建置流程的一部分，`generate` 命令將在代碼編譯之前自動執行。因此，您的變更永遠不會與產生的程式碼同步。  
請注意，CloudFormation CLI 必須位於 Maven/系統可以找到的位置。如需詳細資訊，請參閱《[為開發延伸模組設定環境](https://docs.aws.amazon.com/cloudformation-cli/latest/userguide/what-is-cloudformation-cli.html#resource-type-setup)》。

如需完整流程的詳細資訊，請參閱《[CloudFormation中的模型資源提供者](https://docs.aws.amazon.com/cloudformation-cli/latest/userguide/resource-type-model.html)》。

## 步驟 3：產生成品
<a name="generateartifactsforcfnsubmit"></a>

請執行下列命令來產生 `cfn submit` 的成品。

```
$ mvn package
```

## 步驟 4：註冊您的資源
<a name="customresources-registerresource"></a>

AWS Config 不需要資源提供者處理常式，即可為您的資源執行組態追蹤。執行以下命令來註冊您的資源。

```
$ cfn submit
```

如需詳細資訊，請參閱[註冊資源提供者以在 CloudFormation 範本中使用](https://docs.aws.amazon.com/cloudformation-cli/latest/userguide/resource-type-register.html)。

## 步驟 5：發佈資源組態
<a name="customresources-publishresourceconfiguration"></a>

確定 MyCustomNamespace::Testing::WordPress 的組態。

```
{
  "Name": "MyWordPressSite",
  "SubnetId": "subnet-abcd0123",
  "InstanceId": "i-01234567",
  "PublicIp": "my-wordpress-site.com"
}
```

從中判斷結構描述版本 ID CloudFormation `DescribeType`。

在 中 AWS Config，您可以查看是否接受此資源組態。若要評估合規性，您可以使用此資源撰寫 AWS Config 規則。

（選用） 若要自動記錄組態，請實作定期或以變更為基礎的組態收集器。

# 使用 記錄第三方資源 AWS Config 的組態項目 AWS CLI
<a name="add-custom-resource-type-cli"></a>

使用下列程序，記錄第三方資源或自訂資源類型的組態項目：

請確定使用相符的結構描述來註冊資源類型 `MyCustomNamespace::Testing::WordPress`。

1. 開啟命令提示或終端機視窗。

1. 輸入以下命令：

   ```
   aws configservice put-resource-config --resource-type MyCustomNamespace::Testing::WordPress --resource-id resource-001 --schema-version-id 00000001 --configuration  '{
     "Id": "resource-001",
     "Name": "My example custom resource.",
     "PublicAccess": false
   }'
   ```

**注意**  
如類型結構描述中所定義， `writeOnlyProperties`將在記錄之前從組態中移除 AWS Config。這表示從讀取 APIs 取得組態時，不會出現這些值。如需 `writeOnlyProperties` 的詳細資訊，請參閱《[資源類型結構描述](https://docs.aws.amazon.com/cloudformation-cli/latest/userguide/resource-type-schema.html)》。

# 使用 AWS Config 搭配第三方資源的 讀取組態項目 AWS CLI
<a name="view-custom-resource-type-cli"></a>

使用下列程序讀取第三方資源或自訂資源類型的組態項目：

1. 開啟命令提示或終端機視窗。

1. 輸入以下命令：

   ```
   aws configservice list-discovered-resources --resource-type MyCustomNamespace::Testing::WordPress
   ```

1. 按 Enter。

   您應該會看到類似下列的輸出：

   ```
   {
       "resourceIdentifiers": [
           {
               "resourceType": "MyCustomNamespace::Testing::WordPress",
               "resourceId": "resource-001"
           }
       ]
   }
   ```

1. 輸入以下命令：

   ```
   aws configservice batch-get-resource-config --resource-keys '[ { "resourceType": "MyCustomNamespace::Testing::WordPress", "resourceId": "resource-001" } ]'
   ```

1. 按 Enter。

   您應該會看到類似下列的輸出：

   ```
   {
       "unprocessedResourceKeys": [],
       "baseConfigurationItems": [
           {
               "configurationItemCaptureTime": 1569605832.673,
               "resourceType": "MyCustomNamespace::Testing::WordPress",
               "resourceId": "resource-001",
               "configurationStateId": "1569605832673",
               "awsRegion": "us-west-2",
               "version": "1.3",
               "supplementaryConfiguration": {},
               "configuration": "{\"Id\":\"resource-001\",\"Name\":\"My example custom resource.\",\"PublicAccess\":false}",
               "configurationItemStatus": "ResourceDiscovered",
               "accountId": "AccountId"
           }
       ]
   }
   ```

# AWS Config 使用 從 刪除第三方資源 AWS CLI
<a name="delete-custom-resource-type"></a>

輸入下列命令來刪除第三方資源：

```
aws configservice delete-resource-config --resource-type MyCustomNamespace::Testing::WordPress --resource-id resource-002
```

若成功的話，命令會在沒有其他輸出的情況下執行。