

翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。

# 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>

このチュートリアルでは、ParallelCluster のカスタム AMI をデプロイするためのシンプルな Terraform プロジェクトを定義します。

1. `my-amis` という名前のディレクトリを作成します。

   作成するすべてのファイルは、このディレクトリに入ります。

1. ParallelCluster プロバイダーをインポートするファイル `terraform.tf` を作成します。

   ```
   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`、変数の任意の値を設定します。

   以下の ファイルでは、スタック名 `us-east-1`で既にデプロイされている既存の ParallelCluster API 3.11.1 を使用して、x86\$164 アーキテクチャ用の Amazon Linux 2 `us-east-1`に基づいて にカスタム 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"
        }
    ]
}
```

------