

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

# 使用 Terraform 创建自定义 AMI
<a name="tutorial-create-ami-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 创建集群](tutorial-create-cluster-terraform.md)。
+ 具有调用 ParallelCluster API 权限的 IAM 角色。请参阅[所需的权限](tutorial-create-ami-terraform-permissions.md)。

# 定义 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.
   ```

# 部署 AMI
<a name="tutorial-create-ami-terraform-deploy"></a>

要部署 AMI，请按顺序运行标准的 Terraform 命令。

1. 构建项目：

   ```
   terraform init
   ```

1. 定义部署计划：

   ```
   terraform plan -out tfplan
   ```

1. 部署计划：

   ```
   terraform apply tfplan
   ```

# 所需的权限
<a name="tutorial-create-ami-terraform-permissions"></a>

使用 Terraform 部署自定义 AMI 需要以下权限：
+ 担任 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"
        }
    ]
}
```

------