Terraform を使用したクラスターの作成 - AWS ParallelCluster

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

Terraform を使用したクラスターの作成

を使用する場合 AWS ParallelCluster、 AWS ParallelCluster イメージとクラスターを作成または更新したときに作成された AWS リソースに対してのみ料金が発生します。詳細については、「AWS が使用する のサービス AWS ParallelCluster」を参照してください。

前提条件

Terraform プロジェクトを定義する

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

  1. というディレクトリを作成しますmy-clusters

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

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

    terraform { required_version = ">= 1.5.7" required_providers { aws-parallelcluster = { source = "aws-tf/aws-parallelcluster" version = "1.0.0" } } }
  3. ファイルを作成して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 }
  4. ファイルを作成してmain.tf、 モジュールを使用して ParallelClusterリソースを定義します。

    module "pcluster" { source = "aws-tf/parallelcluster/aws" version = "1.0.0" region = var.region api_stack_name = var.api_stack_name api_version = var.api_version deploy_pcluster_api = false template_vars = local.config_vars cluster_configs = local.cluster_configs config_path = "config/clusters.yaml" }
  5. ファイルを作成してclusters.tf、複数のクラスターを Terraform ローカル変数として定義します。

    注記

    cluster_config 要素内で複数のクラスターを定義できます。クラスターごとに、ローカル変数内のクラスタープロパティを明示的に定義することも (「」を参照DemoCluster01)、外部ファイルを参照することもできます (「」を参照)DemoCluster02

    設定要素内で設定できるクラスタープロパティを確認するには、「」を参照してくださいクラスター設定ファイル

    クラスター作成用に設定できるオプションを確認するには、「」を参照してくださいpcluster create-cluster

    locals { cluster_configs = { DemoCluster01 : { region : local.config_vars.region rollbackOnFailure : false validationFailureLevel : "WARNING" suppressValidators : [ "type:KeyPairValidator" ] configuration : { Region : local.config_vars.region Image : { Os : "alinux2" } HeadNode : { InstanceType : "t3.small" Networking : { SubnetId : local.config_vars.subnet } Iam : { AdditionalIamPolicies : [ { Policy : "arn:aws:iam::aws:policy/AmazonSSMManagedInstanceCore" } ] } } Scheduling : { Scheduler : "slurm" SlurmQueues : [{ Name : "queue1" CapacityType : "ONDEMAND" Networking : { SubnetIds : [local.config_vars.subnet] } Iam : { AdditionalIamPolicies : [ { Policy : "arn:aws:iam::aws:policy/AmazonSSMManagedInstanceCore" } ] } ComputeResources : [{ Name : "compute" InstanceType : "t3.small" MinCount : "1" MaxCount : "4" }] }] SlurmSettings : { QueueUpdateStrategy : "TERMINATE" } } } } DemoCluster02 : { configuration : "config/cluster_config.yaml" } } }
  6. ファイルを作成してconfig/clusters.yaml、複数のクラスターを YAML 設定として定義します。

    DemoCluster03: region: ${region} rollbackOnFailure: true validationFailureLevel: WARNING suppressValidators: - type:KeyPairValidator configuration: config/cluster_config.yaml DemoCluster04: region: ${region} rollbackOnFailure: false configuration: config/cluster_config.yaml
  7. ファイル を作成します。これはconfig/cluster_config.yaml、Terraform 変数を挿入できる標準 ParallelCluster 設定ファイルです。

    設定要素内で設定できるクラスタープロパティを確認するには、「」を参照してくださいクラスター設定ファイル

    Region: ${region} Image: Os: alinux2 HeadNode: InstanceType: t3.small Networking: SubnetId: ${subnet} Iam: AdditionalIamPolicies: - Policy: arn:aws:iam::aws:policy/AmazonSSMManagedInstanceCore Scheduling: Scheduler: slurm SlurmQueues: - Name: queue1 CapacityType: ONDEMAND Networking: SubnetIds: - ${subnet} Iam: AdditionalIamPolicies: - Policy: arn:aws:iam::aws:policy/AmazonSSMManagedInstanceCore ComputeResources: - Name: compute InstanceType: t3.small MinCount: 1 MaxCount: 5 SlurmSettings: QueueUpdateStrategy: TERMINATE
  8. ファイルを作成してclusters_vars.tf、クラスター設定に挿入できる変数を定義します。

    このファイルを使用すると、リージョンやサブネットなどのクラスター設定で使用できる動的な値を定義できます。

    この例では、プロジェクト変数から直接値を取得しますが、カスタムロジックを使用して値を決定する必要がある場合があります。

    locals { config_vars = { subnet = var.subnet_id region = var.cluster_region } }
  9. ファイルを作成してvariables.tf、このプロジェクトに挿入できる変数を定義します。

    variable "region" { description = "The region the ParallelCluster API is deployed in." type = string default = "us-east-1" } variable "cluster_region" { description = "The region the clusters will be deployed in." type = string default = "us-east-1" } variable "profile" { type = string description = "The AWS profile used to deploy the clusters." default = null } variable "subnet_id" { type = string description = "The id of the subnet to be used for the ParallelCluster instances." } 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." }
  10. ファイルを作成してterraform.tfvars、変数の任意の値を設定します。

    以下の ファイルはsubnet-123456789、スタック名 で に既にデプロイされている既存の ParallelCluster API 3.10.0 を使用して、サブネット eu-west-1内の us-east-1にクラスターをデプロイしますMyParallelClusterAPI-310

    region = "us-east-1" api_stack_name = "MyParallelClusterAPI-310" api_version = "3.10.0" cluster_region = "eu-west-1" subnet_id = "subnet-123456789"
  11. ファイルを作成してoutputs.tf、このプロジェクトによって返される出力を定義します。

    output "clusters" { value = module.pcluster.clusters }

    プロジェクトディレクトリは次のとおりです。

    my-clusters ├── config │ ├── cluster_config.yaml - Cluster configuration, where terraform variables can be injected.. │ └── clusters.yaml - File listing all the clusters to deploy. ├── clusters.tf - Clusters defined as Terraform local variables. ├── clusters_vars.tf - Variables that can be injected into cluster configurations. ├── 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.

クラスターをデプロイする

クラスターをデプロイするには、標準の Terraform コマンドを順番に実行します。

注記

この例では、 アカウントに ParallelCluster API を既にデプロイしていることを前提としています。

  1. プロジェクトを構築します。

    terraform init
  2. デプロイプランを定義します。

    terraform plan -out tfplan
  3. プランをデプロイします。

    terraform apply tfplan

クラスターを使用して ParallelCluster API をデプロイする

ParallelCluster API をデプロイしておらず、クラスターでデプロイする場合は、次のファイルを変更します。

  • main.tf

    module "pcluster" { source = "aws-tf/aws/parallelcluster" version = "1.0.0" region = var.region api_stack_name = var.api_stack_name api_version = var.api_version deploy_pcluster_api = true template_vars = local.config_vars cluster_configs = local.cluster_configs config_path = "config/clusters.yaml" }
  • providers.tf

    provider "aws-parallelcluster" { region = var.region profile = var.profile endpoint = module.pcluster.pcluster_api_stack_outputs.ParallelClusterApiInvokeUrl role_arn = module.pcluster.pcluster_api_stack_outputs.ParallelClusterApiUserRole }

必要なアクセス許可

Terraform でクラスターをデプロイするには、次のアクセス許可が必要です。

  • ParallelCluster API とのやり取りを担当する ParallelCluster API ロールを引き受ける

  • ParallelCluster API の AWS CloudFormation スタックを記述して、そのスタックが存在することを確認し、そのパラメータと出力を取得します。

{ "Version": "2012-10-17", "Statement": [ { "Action": "sts:AssumeRole", "Resource": "arn:PARTITION:iam::ACCOUNT:role/PCAPIUserRole-*", "Effect": "Allow", "Sid": "AssumePCAPIUserRole" }, { "Action": [ "cloudformation:DescribeStacks" ], "Resource": "arn:PARTITION:cloudformation:REGION:ACCOUNT:stack/*", "Effect": "Allow", "Sid": "CloudFormation" } ] }