

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

# 將第三方資源新增至 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 規則。

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