

기계 번역으로 제공되는 번역입니다. 제공된 번역과 원본 영어의 내용이 상충하는 경우에는 영어 버전이 우선합니다.

# Terraform을 사용하여 클러스터 생성
<a name="tutorial-create-cluster-terraform"></a>

를 사용하는 경우 AWS ParallelCluster 이미지 및 클러스터를 생성하거나 업데이트할 때 생성된 AWS 리소스 AWS ParallelCluster에 대해서만 비용을 지불합니다. 자세한 내용은 [AWS 에서 사용하는 서비스 AWS ParallelCluster](aws-services-v3.md) 단원을 참조하십시오.

**사전 조건**
+ Terraform v1.5.7\$1가 설치되었습니다.
+ [AWS ParallelCluster API](api-reference-v3.md) v3.8.0\$1가 계정에 배포됩니다. [Terraform을 사용하여 ParallelCluster API 배포](tutorial-deploy-terraform.md)을(를) 참조하세요.
+ ParallelCluster API를 간접적으로 호출할 수 있는 권한이 있는 IAM 역할입니다. [필수 권한] 참조

# Terraform 프로젝트 정의
<a name="tutorial-create-cluster-terraform-define"></a>

이 자습서에서는 클러스터를 배포하기 위한 간단한 Terraform 프로젝트를 정의합니다.

1. 이름이 `my-clusters`인 디렉터리를 생성합니다.

   생성하는 모든 파일은 이 디렉터리 내에 있습니다.

1. `terraform.tf` 파일을 생성하여 ParallelCluster 공급자를 가져옵니다.

   ```
   terraform {
     required_version = ">= 1.5.7"
     required_providers {
       aws-parallelcluster = {
         source  = "aws-tf/aws-parallelcluster"
         version = "~> 1.0"
       }
     }
   }
   ```

1. `providers.tf` 파일을 생성하여 ParallelCluster 및 AWS 공급자를 구성합니다.

   ```
   provider "aws" {
     region  = var.region
     profile = var.profile
   }
   
   provider "aws-parallelcluster" {
     region         = var.region
     profile        = var.profile
     api_stack_name = var.api_stack_name
     use_user_role  = true
   }
   ```

1. `main.tf` 파일을 생성하여 ParallelCluster 모듈을 사용하여 리소스를 정의합니다.

   ```
   module "pcluster" {
     source  = "aws-tf/parallelcluster/aws"
     version = "1.1.0"
   
     region                = var.region
     api_stack_name        = var.api_stack_name
     api_version           = var.api_version
     deploy_pcluster_api   = false
   
     template_vars         = local.config_vars
     cluster_configs       = local.cluster_configs
     config_path           = "config/clusters.yaml"
   }
   ```

1. `clusters.tf` 파일을 생성하여 여러 클러스터를 Terraform 로컬 변수로 정의합니다.
**참고**  
`cluster_config` 요소 내에 여러 클러스터를 정의할 수 있습니다. 모든 클러스터에 대해 로컬 변수 내에서 클러스터 속성을 명시적으로 정의하거나(`DemoCluster01` 참조) 외부 파일을 참조할 수 있습니다(`DemoCluster02` 참조).

   구성 요소 내에서 설정할 수 있는 클러스터 속성을 검토하려면 [클러스터 구성 파일](cluster-configuration-file-v3.md) 섹션을 참조하세요.

   클러스터 생성을 위해 설정할 수 있는 옵션을 검토하려면 [`pcluster create-cluster`](pcluster.create-cluster-v3.md) 섹션을 참조하세요.

   ```
   locals {
     cluster_configs = {
       DemoCluster01 : {
         region : local.config_vars.region
         rollbackOnFailure : false
         validationFailureLevel : "WARNING"
         suppressValidators : [
           "type:KeyPairValidator"
         ]
         configuration : {
           Region : local.config_vars.region
           Image : {
             Os : "alinux2"
           }
           HeadNode : {
             InstanceType : "t3.small"
             Networking : {
               SubnetId : local.config_vars.subnet
             }
             Iam : {
               AdditionalIamPolicies : [
                 { Policy : "arn:aws:iam::aws:policy/AmazonSSMManagedInstanceCore" }
               ]
             }
           }
           Scheduling : {
             Scheduler : "slurm"
             SlurmQueues : [{
               Name : "queue1"
               CapacityType : "ONDEMAND"
               Networking : {
                 SubnetIds : [local.config_vars.subnet]
               }
               Iam : {
                 AdditionalIamPolicies : [
                   { Policy : "arn:aws:iam::aws:policy/AmazonSSMManagedInstanceCore" }
                 ]
               }
               ComputeResources : [{
                 Name : "compute"
                 InstanceType : "t3.small"
                 MinCount : "1"
                 MaxCount : "4"
               }]
             }]
             SlurmSettings : {
               QueueUpdateStrategy : "TERMINATE"
             }
           }
         }
       }
       DemoCluster02 : {
         configuration : "config/cluster_config.yaml"
       }
     }
   }
   ```

1. `config/clusters.yaml` 파일을 생성하여 여러 클러스터를 YAML 구성으로 정의합니다.

   ```
   DemoCluster03:
     region: ${region}
     rollbackOnFailure: true
     validationFailureLevel: WARNING
     suppressValidators:
       - type:KeyPairValidator
     configuration: config/cluster_config.yaml
   DemoCluster04:
     region: ${region}
     rollbackOnFailure: false
     configuration: config/cluster_config.yaml
   ```

1. Terraform 변수를 삽입할 수 있는 표준 ParallelCluster 구성 파일인 `config/cluster_config.yaml` 파일을 생성합니다.

   구성 요소 내에서 설정할 수 있는 클러스터 속성을 검토하려면 [클러스터 구성 파일](cluster-configuration-file-v3.md) 섹션을 참조하세요.

   ```
   Region: ${region}
   Image:
    Os: alinux2
   HeadNode:
    InstanceType: t3.small
    Networking:
      SubnetId: ${subnet}
    Iam:
      AdditionalIamPolicies:
        - Policy: arn:aws:iam::aws:policy/AmazonSSMManagedInstanceCore
   Scheduling:
    Scheduler: slurm
    SlurmQueues:
      - Name: queue1
        CapacityType: ONDEMAND
        Networking:
          SubnetIds:
            - ${subnet}
        Iam:
          AdditionalIamPolicies:
            - Policy: arn:aws:iam::aws:policy/AmazonSSMManagedInstanceCore
        ComputeResources:
          - Name: compute
            InstanceType: t3.small
            MinCount: 1
            MaxCount: 5
    SlurmSettings:
      QueueUpdateStrategy: TERMINATE
   ```

1. `clusters_vars.tf` 파일을 생성하여 클러스터 구성에 삽입할 수 있는 변수를 정의합니다.

   이 파일을 사용하면 리전 및 서브넷과 같은 클러스터 구성에 사용할 수 있는 동적 값을 정의할 수 있습니다.

   이 예제는 프로젝트 변수에서 직접 값을 검색하지만 사용자 지정 로직을 사용하여 값을 결정해야 할 수 있습니다.

   ```
   locals {
     config_vars = {
       subnet = var.subnet_id
       region = var.cluster_region
     }
   }
   ```

1. `variables.tf` 파일을 생성하여 이 프로젝트에 주입할 수 있는 변수를 정의합니다.

   ```
   variable "region" {
     description = "The region the ParallelCluster API is deployed in."
     type        = string
     default     = "us-east-1"
   }
   
   variable "cluster_region" {
     description = "The region the clusters will be deployed in."
     type        = string
     default     = "us-east-1"
   }
   
   variable "profile" {
     type        = string
     description = "The AWS profile used to deploy the clusters."
     default     = null
   }
   
   variable "subnet_id" {
     type        = string
     description = "The id of the subnet to be used for the ParallelCluster instances."
   }
   
   variable "api_stack_name" {
     type        = string
     description = "The name of the CloudFormation stack used to deploy the ParallelCluster API."
     default     = "ParallelCluster"
   }
   
   variable "api_version" {
     type        = string
     description = "The version of the ParallelCluster API."
   }
   ```

1. `terraform.tfvars` 파일을 생성하여 변수에 대한 임의 값을 설정합니다.

   아래 파일은 스택 이름이 인에 이미 배포된 기존 ParallelCluster API 3.11.1을 `subnet-123456789`사용하여 서브넷 `eu-west-1` 내에서에 클러스터`us-east-1`를 배포합니다`MyParallelClusterAPI-3111`.

   ```
   region = "us-east-1"
   api_stack_name = "MyParallelClusterAPI-3111"
   api_version = "3.11.1"
   
   cluster_region = "eu-west-1"
   subnet_id = "subnet-123456789"
   ```

1. `outputs.tf` 파일을 생성하여 이 프로젝트에서 반환되는 출력을 정의합니다.

   ```
   output "clusters" {
     value = module.pcluster.clusters
   }
   ```

   프로젝트 디렉터리:

   ```
   my-clusters
   ├── config
   │   ├── cluster_config.yaml - Cluster configuration, where terraform variables can be injected..
   │   └── clusters.yaml - File listing all the clusters to deploy.
   ├── clusters.tf - Clusters defined as Terraform local variables.
   ├── clusters_vars.tf - Variables that can be injected into cluster configurations.
   ├── main.tf - Terraform entrypoint where the ParallelCluster module is configured.
   ├── outputs.tf - Defines the cluster as a Terraform output.
   ├── providers.tf - Configures the providers: ParallelCluster and AWS.
   ├── terraform.tf - Import the ParallelCluster provider.
   ├── terraform.tfvars - Defines values for variables, e.g. region, PCAPI stack name.
   └── variables.tf - Defines the variables, e.g. region, PCAPI stack name.
   ```

# 클러스터 배포
<a name="tutorial-create-cluster-terraform-deploy"></a>

클러스터를 배포하려면 표준 Terraform 명령을 순서대로 실행합니다.

**참고**  
이 예제에서는 계정에 ParallelCluster API를 이미 배포했다고 가정합니다.

1. 프로젝트 빌드:

   ```
   terraform init
   ```

1. 배포 계획 정의:

   ```
   terraform plan -out tfplan
   ```

1. 계획 배포:

   ```
   terraform apply tfplan
   ```

## 클러스터를 사용하여 ParallelCluster API 배포
<a name="tutorial-create-cluster-terraform-deploy-api"></a>

ParallelCluster API를 배포하지 않고 클러스터와 함께 배포하려는 경우 다음 파일을 변경합니다.
+ `main.tf`

  ```
  module "pcluster" {
    source  = "aws-tf/aws/parallelcluster"
    version = "1.0.0"
  
    region                = var.region
    api_stack_name        = var.api_stack_name
    api_version           = var.api_version
    deploy_pcluster_api   = true
    parameters = {
      EnableIamAdminAccess = "true"
    }
    
    template_vars         = local.config_vars
    cluster_configs       = local.cluster_configs
    config_path           = "config/clusters.yaml"
  }
  ```
+ `providers.tf`

  ```
  provider "aws-parallelcluster" {
    region   = var.region
    profile  = var.profile
    endpoint = module.pcluster.pcluster_api_stack_outputs.ParallelClusterApiInvokeUrl
    role_arn = module.pcluster.pcluster_api_stack_outputs.ParallelClusterApiUserRole
  }
  ```

# 필수 권한
<a name="tutorial-create-cluster-terraform-permissions"></a>

Terraform을 사용하여 클러스터를 배포하려면 다음 권한이 필요합니다.
+ ParallelCluster API와의 상호 작용을 담당하는 ParallelCluster API 역할을 수임합니다.
+ ParallelCluster API의 CloudFormation 스택을 설명하여 API가 존재하는지 확인하고 파라미터 및 출력을 검색합니다.

------
#### [ JSON ]

****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [
        {
            "Action": "sts:AssumeRole",
            "Resource": "arn:aws:sts::111122223333:role/PCAPIUserRole-*",
            "Effect": "Allow",
            "Sid": "AssumePCAPIUserRole"
        },
        {
            "Action": [
                "cloudformation:DescribeStacks"
            ],
            "Resource": "arn:aws:cloudformation:us-east-1:111122223333:stack/*",
            "Effect": "Allow",
            "Sid": "CloudFormation"
        }
    ]
}
```

------