

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

# Creazione di un'AMI personalizzata con Terraform
<a name="tutorial-create-ami-terraform"></a>

Quando si utilizza AWS ParallelCluster, si pagano solo le AWS risorse create quando si creano o si aggiornano AWS ParallelCluster immagini e cluster. Per ulteriori informazioni, consulta [AWS servizi usati da AWS ParallelCluster](aws-services-v3.md).

**Prerequisiti**
+  Terraform v1.5.7\$1 è installato. 
+ [AWS ParallelCluster API](api-reference-v3.md)v3.8.0\$1 è distribuito nel tuo account. Per informazioni, consulta [Creare un cluster con Terraform](tutorial-create-cluster-terraform.md). 
+ Ruolo IAM con le autorizzazioni per richiamare l'API. ParallelCluster Per informazioni, consulta [Autorizzazioni richieste](tutorial-create-ami-terraform-permissions.md).

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

In questo tutorial, definirai un semplice progetto Terraform per implementare un' ParallelCluster AMI personalizzata.

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

   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.

   Per esaminare le proprietà dell'immagine che è possibile impostare all'interno dell'`image_configuration`elemento, vedere[Crea file di configurazione delle immagini](image-builder-configuration-file-v3.md).

   Per esaminare le opzioni che è possibile impostare per la creazione di immagini, ad esempio `image_id` e`rollback_on_failure`, vedere[`pcluster build-image`](pcluster.build-image-v3.md). 

   ```
   data "aws-parallelcluster_list_official_images" "parent_image" {
     region = var.region
     os = var.os
     architecture = var.architecture
   }
   
   resource "aws-parallelcluster_image" "demo01" {
     image_id            = "demo01"
     image_configuration = yamlencode({
       "Build":{
         "InstanceType": "c5.2xlarge",
         "ParentImage": data.aws-parallelcluster_list_official_images.parent_image.official_images[0].amiId,
         "UpdateOsPackages": {"Enabled": false}
       }
     })
     rollback_on_failure = false
   }
   ```

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 "profile" {
     type        = string
     description = "The AWS profile used to deploy the clusters."
     default     = null
   }
   
   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."
   }
   
   variable "os" {
     type        = string
     description = "The OS of the ParallelCluster image."
   }
   
   variable "architecture" {
     type        = string
     description = "The architecture of the ParallelCluster image."
   }
   ```

1. Crea il file `terraform.tfvars` per impostare i tuoi valori arbitrari per le variabili. 

   Con il file seguente, distribuisci l'AMI personalizzata `us-east-1` basata sull'architettura Amazon Linux 2 per x86\$164, 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"
   
   os = "alinux2"
   architecture = "x86_64"
   ```

1. Crea il file per definire gli output restituiti `outputs.tf` da questo progetto.

   ```
   output "parent_image" {
     value = data.aws-parallelcluster_list_official_images.parent_image.official_images[0]
   }
   
   output "custom_image" {
     value = aws-parallelcluster_image.demo01
   }
   ```

   La cartella del progetto è:

   ```
   my-amis
   ├── 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.
   ```

# Implementa l'AMI
<a name="tutorial-create-ami-terraform-deploy"></a>

Per distribuire l'AMI, esegui i comandi Terraform standard nell'ordine.

1. Compila il progetto:

   ```
   terraform init
   ```

1. Definisci il piano di implementazione:

   ```
   terraform plan -out tfplan
   ```

1. Implementa il piano:

   ```
   terraform apply tfplan
   ```

# Autorizzazioni richieste
<a name="tutorial-create-ami-terraform-permissions"></a>

Sono necessarie le seguenti autorizzazioni per distribuire un'AMI personalizzata con Terraform:
+ assume il ruolo dell' ParallelCluster API, che si occupa dell'interazione con l'API ParallelCluster 
+ descrivi lo CloudFormation stack dell' ParallelCluster API, per verificarne l'esistenza e recuperarne i parametri e gli output

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

------