

# 使用 IaC 生成器根据扫描的资源创建 CloudFormation 模板
<a name="iac-generator-create-template-from-scanned-resources"></a>

本主题介绍了如何使用 IaC 生成器功能，根据扫描的资源创建模板。

## 根据扫描的资源创建模板（控制台）
<a name="create-template-from-scanned-resources-console"></a>

**根据扫描的资源创建堆栈模板**

1. 打开 CloudFormation 控制台的 [IaC 生成器页面](https://console.amazonaws.cn/cloudformation/home?#iac-generator)。

1. 在屏幕顶部的导航栏上，选择包含已扫描资源的 Amazon Web Services 区域。

1. 从**模板**部分中，选择**创建模板**。

1. 选择**从新模板开始**。

   1. 对于**模板名称**，请提供模板的名称。

   1. （可选）配置您的**删除策略**和**更新替换策略**。

   1. 选择**下一步**将扫描的资源添加到模板中。

1. 对于**添加扫描的资源**，浏览扫描的资源列表，然后选择要添加到模板中的资源。您可以按资源标识符、资源类型或标签筛选资源。筛选器是互相包容的。

1. 将所有需要的资源添加到模板后，选择**下一步**以退出**添加扫描的资源**页面，然后进入**添加相关资源**页面。

1. 查看推荐的相关资源列表。诸如 Amazon EC2 实例和安全组之类的相关资源是相互依赖的，通常属于同一个工作负载。选择要包含在生成的模板中的相关资源。
**注意**  
建议您将所有相关资源添加到此模板中。

1. 查看模板详细信息、扫描的资源和相关资源。

1. 选择**创建模板**退出**查看并创建**页面并创建模板。

## 根据扫描的资源创建模板（Amazon CLI）
<a name="create-template-from-scanned-resources-cli"></a>

**根据扫描的资源创建堆栈模板**

1. 使用 [list-resource-scan-resources](https://docs.amazonaws.cn/cli/latest/reference/cloudformation/list-resource-scan-resources.html) 命令来列出在扫描期间找到的资源，还可选择指定 `--resource-identifier` 选项来限制输出范围。对于 `--resource-scan-id` 选项，请将示例 ARN 替换为实际的 ARN。

   ```
   aws cloudformation list-resource-scan-resources \
     --resource-scan-id {{arn:aws:cloudformation:us-east-1:123456789012:resourceScan/0a699f15-489c-43ca-a3ef-3e6ecfa5da60}} \
     --resource-identifier {{MyApp}}
   ```

   示例响应如下，其中 `ManagedByStack` 指示 CloudFormation 是否已管理该资源。复制输出。您在下一个步骤中需要用到它。

   ```
   {
       "Resources": [
           {
               "ResourceType": "AWS::EKS::Cluster",
               "ResourceIdentifier": {
                   "ClusterName": "MyAppClusterName"
               },
               "ManagedByStack": false
           },
           {
               "ResourceType": "AWS::AutoScaling::AutoScalingGroup",
               "ResourceIdentifier": {
                   "AutoScalingGroupName": "MyAppASGName"
               },
               "ManagedByStack": false
           }
       ]
   }
   ```

   有关输出中字段的描述，请参阅《Amazon CloudFormation API 参考》中的 [ScannedResource](https://docs.amazonaws.cn/AWSCloudFormation/latest/APIReference/API_ScannedResource.html)**。

1. 使用 `cat` 命令将资源类型和标识符存储到您主目录中名为 `resources.json` 的 JSON 文件中。以下是基于上一步中示例输出的示例 JSON。

   ```
   $ cat > resources.json
   [
       {
           "ResourceType": "AWS::EKS::Cluster",
           "ResourceIdentifier": {
               "ClusterName": "MyAppClusterName"
           }
       },
       {
           "ResourceType": "AWS::AutoScaling::AutoScalingGroup",
           "ResourceIdentifier": {
               "AutoScalingGroupName": "MyAppASGName"
           }
       }
   ]
   ```

1. 使用 [list-resource-scan-related-resources](https://docs.amazonaws.cn/cli/latest/reference/cloudformation/list-resource-scan-related-resources.html) 命令以及您创建的 `resources.json` 文件来列出与您扫描的资源相关的资源。

   ```
   aws cloudformation list-resource-scan-related-resources \
     --resource-scan-id {{arn:aws:cloudformation:us-east-1:123456789012:resourceScan/0a699f15-489c-43ca-a3ef-3e6ecfa5da60}} \
     --resources {{file://resources.json}}
   ```

   示例响应如下，其中 `ManagedByStack` 指示 CloudFormation 是否已管理该资源。将这些资源添加到您在上一步中创建的 JSON 文件中。您需要这些输出来创建模板。

   ```
   {
       "RelatedResources": [
           {
               "ResourceType": "AWS::EKS::Nodegroup",
               "ResourceIdentifier": {
                   "NodegroupName": "MyAppNodegroupName"
               },
               "ManagedByStack": false
           },
           {
               "ResourceType": "AWS::IAM::Role",
               "ResourceIdentifier": {
                   "RoleId": "arn:aws::iam::{{account-id}}:role/MyAppIAMRole"
               },
               "ManagedByStack": false
           }
       ]
   }
   ```

   有关输出中字段的描述，请参阅《Amazon CloudFormation API 参考》中的 [ScannedResource](https://docs.amazonaws.cn/AWSCloudFormation/latest/APIReference/API_ScannedResource.html)**。
**注意**  
资源输入列表的长度不能超过 100。如果要列出的资源数超过了 100 个，请按照一个批次 100 个资源的方式，来运行 **list-resource-scan-related-resources** 命令并合并结果。  
请注意，输出列表中可能包含重复的资源。

1. 使用 [create-generated-template](https://docs.amazonaws.cn/cli/latest/reference/cloudformation/create-generated-template.html) 命令来创建新的堆栈模板，并进行下列修改：
   + 将 `{{us-east-1}}` 替换为包含已扫描资源的 Amazon Web Services 区域。
   + 将 `{{MyTemplate}}` 替换为要创建的模板的名称。

   ```
   aws cloudformation create-generated-template --region {{us-east-1}} \
    --generated-template-name {{MyTemplate}} \
     --resources {{file://resources.json}}
   ```

   下面是一个 `resources.json` 示例文件。

   ```
   [
       {
           "ResourceType": "AWS::EKS::Cluster",
           "LogicalResourceId":"MyCluster",
           "ResourceIdentifier": {
               "ClusterName": "MyAppClusterName"
           }
       },
       {
           "ResourceType": "AWS::AutoScaling::AutoScalingGroup",
           "LogicalResourceId":"MyASG",
           "ResourceIdentifier": {
               "AutoScalingGroupName": "MyAppASGName"
           }
       },
       {
           "ResourceType": "AWS::EKS::Nodegroup",
           "LogicalResourceId":"MyNodegroup",
           "ResourceIdentifier": {
               "NodegroupName": "MyAppNodegroupName"
           }
       },
       {
           "ResourceType": "AWS::IAM::Role",
           "LogicalResourceId":"MyRole",
           "ResourceIdentifier": {
               "RoleId": "arn:aws::iam::{{account-id}}:role/MyAppIAMRole"
           }
       }
   ]
   ```

   如果成功，该命令会返回以下响应。

   ```
   {
     "Arn":
       "arn:aws:cloudformation:{{region}}:{{account-id}}:generatedtemplate/{{7fc8512c-d8cb-4e02-b266-d39c48344e48}}",
     "Name": "{{MyTemplate}}"
   }
   ```