使用 Terraform 创建自定义 AMI - Amazon ParallelCluster
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

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

使用 Terraform 创建自定义 AMI

使用时 Amazon ParallelCluster,您只需为创建或更新 Amazon ParallelCluster 映像和集群时创建的 Amazon 资源付费。有关更多信息,请参阅 Amazon 使用的服务 Amazon ParallelCluster

先决条件

定义一个 Terraform 项目

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

  1. 创建一个名为的目录my-amis

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

  2. 创建文件terraform.tf以导入 ParallelCluster 提供程序。

    terraform { required_version = ">= 1.5.7" required_providers { aws-parallelcluster = { source = "aws-tf/aws-parallelcluster" version = "1.0.0" } } }
  3. 创建用于配置 ParallelCluster 和 Amazon 提供程序的文件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 }
  4. 使用 ParallelCluster模块创建文件main.tf以定义资源。

    要查看可在image_configuration元素中设置的图像属性,请参阅构建映像配置文件

    要查看可以为图像创建设置的选项,例如image_idrollback_on_failure,请参阅pcluster build-image

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

    在下面的文件中,us-east-1基于适用于 x86_64 架构的 Amazon Linux 2,使用现有 ParallelCluster API 3.10.0 部署自定义 AMI,该API 3.10.0 已经部署在堆栈名称中。us-east-1 MyParallelClusterAPI-310

    region = "us-east-1" api_stack_name = "MyParallelClusterAPI-310" api_version = "3.10.0" os = "alinux2" architecture = "x86_64"
  7. 创建文件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

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

  1. 构建项目:

    terraform init
  2. 定义部署计划:

    terraform plan -out tfplan
  3. 部署计划:

    terraform apply tfplan

所需的权限

您需要以下权限才能使用 Terraform 部署自定义 AMI:

  • 担任 ParallelCluster API 角色,该角色负责与 ParallelCluster API 交互

  • 描述 ParallelCluster API 的 Amazon 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" } ] }