

# 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.aws.amazon.com/cloudformation/home?#iac-generator)를 엽니다.

1. 화면 상단의 탐색 모음에서 스캔한 리소스가 포함된 AWS 리전을 선택합니다.

1. **템플릿** 섹션에서 **템플릿 생성**을 선택합니다.

1. **새 템플릿에서 시작**을 선택합니다.

   1. **템플릿 이름**에 템플릿의 이름을 입력합니다.

   1. (선택 사항) **삭제 정책** 및 **업데이트 교체 정책**을 구성합니다.

   1. **다음**을 선택하여 스캔한 리소스를 템플릿에 추가합니다.

1. **스캔한 리소스 추가**에서 스캔한 리소스 목록을 탐색하고 템플릿에 추가할 리소스를 선택합니다. 리소스 식별자, 리소스 유형 또는 태그를 기준으로 리소스를 필터링할 수 있습니다. 필터는 상호 포괄적입니다.

1. 템플릿에 필요한 리소스를 모두 추가했으면 **다음**을 선택하여 **스캔한 리소스 추가** 페이지를 종료하고 **관련 리소스 추가** 페이지로 진행합니다.

1. 관련 리소스 권장 목록을 검토하세요. Amazon EC2 인스턴스와 보안 그룹 등의 관련 리소스는 상호 의존적이며 일반적으로 동일한 워크로드에 속합니다. 생성된 템플릿에 포함하려는 관련 리소스를 선택합니다.
**참고**  
이 템플릿에 모든 관련 리소스를 추가하는 것이 좋습니다.

1. 템플릿 세부 정보, 스캔한 리소스, 관련 리소스를 검토하세요.

1. **템플릿 생성**을 선택하여 **검토 및 생성** 페이지를 종료하고 템플릿을 생성합니다.

## 스캔한 리소스에서 템플릿 생성(AWS CLI)
<a name="create-template-from-scanned-resources-cli"></a>

**스캔한 리소스에서 새 스택 템플릿 생성**

1. 스캔 중 찾은 리소스를 나열하려면 [list-resource-scan-resources](https://docs.aws.amazon.com/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
           }
       ]
   }
   ```

   출력의 필드에 대한 설명은 **AWS CloudFormation API 참조의 [ScannedResource](https://docs.aws.amazon.com/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.aws.amazon.com/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
           }
       ]
   }
   ```

   출력의 필드에 대한 설명은 **AWS CloudFormation API 참조의 [ScannedResource](https://docs.aws.amazon.com/AWSCloudFormation/latest/APIReference/API_ScannedResource.html)를 참조하세요.
**참고**  
리소스 입력 목록의 길이는 100을 초과할 수 없습니다. 100개 이상의 리소스에 대한 관련 리소스를 나열하려면 100개씩 배치로 **list-resource-scan-related-resources** 명령을 실행하고 결과를 통합합니다.  
목록의 중복된 리소스가 출력에 포함될 수 있다는 점에 유의하세요.

1. 새 스택 템플릿을 생성하려면 [create-generated-template](https://docs.aws.amazon.com/cli/latest/reference/cloudformation/create-generated-template.html) 명령을 다음과 같이 수정하여 사용합니다.
   + 스캔한 리소스가 포함된 AWS 리전으로 `us-east-1`을 바꿉니다.
   + 생성할 템플릿의 이름으로 `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"
   }
   ```