

Le traduzioni sono generate tramite traduzione automatica. In caso di conflitto tra il contenuto di una traduzione e la versione originale in Inglese, quest'ultima prevarrà.

# Definisci un progetto Terraform
<a name="tutorial-create-cluster-terraform-define"></a>

In questo tutorial, definirai un semplice progetto Terraform per distribuire un cluster.

1. Crea una directory chiamata. `my-clusters` 

   Tutti i file che creerai si troveranno all'interno di questa directory.

1. Crea il file `terraform.tf` per importare il ParallelCluster provider.

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

1. Crea il file `providers.tf` per configurare i AWS provider ParallelCluster e.

   ```
   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. Crea il file `main.tf` per definire le risorse utilizzando il ParallelCluster modulo.

   ```
   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. Crea il file `clusters.tf` per definire più cluster come variabili locali Terraform. 
**Nota**  
È possibile definire più cluster all'interno dell'elemento. `cluster_config` Per ogni cluster, è possibile definire in modo esplicito le proprietà del cluster all'interno delle variabili locali (vedere`DemoCluster01`) o fare riferimento a un file esterno (vedere`DemoCluster02`).

   Per esaminare le proprietà del cluster che è possibile impostare all'interno dell'elemento di configurazione, vedere[File di configurazione del cluster](cluster-configuration-file-v3.md).

   Per esaminare le opzioni che è possibile impostare per la creazione di cluster, vedere[`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. Crea il file `config/clusters.yaml` per definire più cluster come configurazione 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. Crea il file`config/cluster_config.yaml`, che è un file di ParallelCluster configurazione standard in cui è possibile inserire le variabili Terraform.

   Per esaminare le proprietà del cluster che è possibile impostare all'interno dell'elemento di configurazione, vedere. [File di configurazione del cluster](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. Crea il file `clusters_vars.tf` per definire le variabili che possono essere inserite nelle configurazioni del cluster.

   Questo file consente di definire valori dinamici che possono essere utilizzati nelle configurazioni del cluster, come regione e sottorete.

   Questo esempio recupera i valori direttamente dalle variabili di progetto, ma potrebbe essere necessario utilizzare una logica personalizzata per determinarli.

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

1. Create il file `variables.tf` per definire le variabili che possono essere inserite per questo progetto.

   ```
   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. Crea il file `terraform.tfvars` per impostare valori arbitrari per le variabili. 

   Il file seguente distribuisce i cluster all'`eu-west-1`interno della sottorete`subnet-123456789`, utilizzando l' ParallelCluster API 3.11.1 esistente, che è già distribuita con il nome dello stack. `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. Crea il file per definire gli output restituiti `outputs.tf` da questo progetto.

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

   La cartella del progetto è:

   ```
   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.
   ```