

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

# 定义 Terraform 项目
<a name="tutorial-create-ami-terraform-define"></a>

在本教程中，您将定义一个简单的 Terraform 项目来部署自定义 ParallelCluster AMI。

1. 创建名为 `my-amis` 的目录。

   您创建的所有文件都将位于此目录中。

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`以定义资源。

   要查看可在 `image_configuration` 元素中设置的映像属性，请参阅[构建映像配置文件](image-builder-configuration-file-v3.md)。

   要查看创建映像时可设置的选项（如 `image_id` 和 `rollback_on_failure`），请参阅 [`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. 创建 `variables.tf` 文件来定义可以为此项目注入的变量。

   ```
   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. 创建文件`terraform.tfvars`以设置变量的任意值。

   在下面的文件中，使用已经使用堆栈名称部署的现有 ParallelCluster API 3.11.1，`us-east-1`基于适用于 x86\$164 架构的 Amazon Linux 2 部署自定义 AMI。`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. 创建 `outputs.tf` 文件来定义此项目返回的输出。

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

   项目目录为：

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