

翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。

# Terraform を使用したクラスターの作成
<a name="tutorial-create-cluster-terraform"></a>

を使用する場合 AWS ParallelCluster、 AWS ParallelCluster イメージとクラスターを作成または更新するときに作成された AWS リソースに対してのみ料金が発生します。詳細については、「[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 ロール。「必要な IAM 許可」を参照してください。

# Terraform プロジェクトを定義する
<a name="tutorial-create-cluster-terraform-define"></a>

このチュートリアルでは、クラスターをデプロイするためのシンプルな Terraform プロジェクトを定義します。

1. `my-clusters` という名前のディレクトリを作成します。

   作成するすべてのファイルは、このディレクトリに入ります。

1. ParallelCluster プロバイダーをインポートするファイル `terraform.tf` を作成します。

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

1. ParallelCluster と AWS プロバイダーを設定するファイル `providers.tf` を作成します。

   ```
   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. ParallelCluster モジュールを使用してリソースを定義するファイル `main.tf` を作成します。

   ```
   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. 複数のクラスターを Terraform ローカル変数として定義するファイル `clusters.tf` を作成します。
**注記**  
`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. 複数のクラスターを YAML 設定として定義するファイル `config/clusters.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` を作成します。

   以下のファイルは`subnet-123456789`、スタック名 `us-east-1`で に既にデプロイされている既存の ParallelCluster API 3.11.1 を使用して、サブネット `eu-west-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 スタックを記述して、そのスタックが存在することを確認し、そのパラメータと出力を取得します。

------
#### [ 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"
        }
    ]
}
```

------