

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

# 使用 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 角色。参见 [所需权限］

# 定义 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. 创建用于配置 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. 创建 `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. 创建文件`config/cluster_config.yaml`，这是一个标准 ParallelCluster 配置文件，可以在其中注入 Terraform 变量。

   要查看可在配置元素中设置的集群属性，请参阅[集群配置文件](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 在子网`eu-west-1`中部署集群`subnet-123456789`，该API 3.11.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 堆栈以验证其存在并检索其参数和输出

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

------