

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 使用 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. 建立 檔案`providers.tf`以設定 ParallelCluster 和 AWS 提供者。

   ```
   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. 建立 檔案`main.tf`以使用 ParallelCluster 模組定義資源。

   若要檢閱您可以在 `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`以設定變數的任意值。

   使用下列檔案，使用已部署在`us-east-1`堆疊名稱為 的現有 ParallelCluster API 3.11.1，`us-east-1`以適用於 x86\$164 架構的 Amazon Linux 2 在 中部署自訂 AMI。 `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"
        }
    ]
}
```

------