

As traduções são geradas por tradução automática. Em caso de conflito entre o conteúdo da tradução e da versão original em inglês, a versão em inglês prevalecerá.

# Definir um projeto Terraform
<a name="tutorial-create-ami-terraform-define"></a>

Neste tutorial, você definirá um projeto simples do Terraform para implantar uma AMI ParallelCluster personalizada.

1. Crie um diretório denominado `my-amis`. 

   Todos os arquivos que você criar estarão dentro desse diretório.

1. Crie o arquivo `terraform.tf` para importar o ParallelCluster provedor.

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

1. Crie o arquivo `providers.tf` para configurar ParallelCluster os AWS provedores 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. Crie o arquivo `main.tf` para definir os recursos usando o ParallelCluster módulo.

   Para revisar as propriedades da imagem que você pode definir dentro no elemento `image_configuration`, consulte [Arquivos de configuração de imagem de compilação](image-builder-configuration-file-v3.md).

   Para revisar as opções que você pode definir para a criação de imagens, por exemplo `image_id` e `rollback_on_failure`, consulte [`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. Crie o arquivo `variables.tf` para definir as variáveis que podem ser injetadas neste projeto.

   ```
   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. Crie o arquivo `terraform.tfvars` para definir seus valores arbitrários para as variáveis. 

   Com o arquivo abaixo, implante a AMI personalizada `us-east-1` com base na arquitetura Amazon Linux 2 para x86\$164, usando a ParallelCluster API 3.11.1 existente, que já está implantada com o nome da pilha. `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. Crie o arquivo `outputs.tf` para definir os resultados gerados por esse projeto.

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

   O diretório do projeto é:

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