

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

# 将第三方资源添加到 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/the 系统可以找到的位置。有关更多信息，请参阅[设置用于开发扩展的环境](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:: 的配置。WordPress

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

从中确定架构版本 ID CloudFormation `DescribeType`。

在中 AWS Config，您可以查看此资源配置是否被接受。要评估合规性，您可以使用此资源编写 AWS Config 规则。

（可选）要自动记录配置，请实施定期配置收集器或基于更改的配置收集器。