Las traducciones son generadas a través de traducción automática. En caso de conflicto entre la traducción y la version original de inglés, prevalecerá la version en inglés.
Definición de un proyecto de Terraform
En este tutorial, definirá un proyecto sencillo de Terraform para implementar un clúster.
Cree un directorio denominado
my-clusters.Todos los archivos que cree estarán dentro de este directorio.
Cree el archivo
terraform.tfpara importar el ParallelCluster proveedor.terraform { required_version = ">= 1.5.7" required_providers { aws-parallelcluster = { source = "aws-tf/aws-parallelcluster" version = "~> 1.0" } } }Cree el archivo
providers.tfpara configurar los AWS proveedores ParallelCluster y.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 }Cree el archivo
main.tfpara definir los recursos mediante el ParallelCluster módulo.module "pcluster" { source = "aws-tf/parallelcluster/aws" version = "1.1.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" }Cree el archivo
clusters.tfpara definir varios clústeres como variables locales de Terraform.nota
Puede definir varios clústeres dentro del elemento
cluster_config. Para cada clúster, puede definir explícitamente las propiedades del clúster dentro de las variables locales (consulteDemoCluster01) o hacer referencia a un archivo externo (consulteDemoCluster02).Para revisar las propiedades del clúster que puede establecer en el elemento de configuración, consulte Configuración del clúster.
Para revisar las opciones que puede configurar para la creación de clústeres, consulte 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" } } }Cree el archivo
config/clusters.yamlpara definir varios clústeres como configuración de 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.yamlCree el archivo
config/cluster_config.yaml, que es un archivo de ParallelCluster configuración estándar en el que se pueden insertar variables de Terraform.Para revisar las propiedades del clúster que puede establecer en el elemento de configuración, consulte Configuración del clúster.
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: TERMINATECree el archivo
clusters_vars.tfpara definir las variables que se pueden inyectar en las configuraciones del clúster.Este archivo le permite definir valores dinámicos que se pueden usar en las configuraciones de clúster, como la región y la subred.
En este ejemplo, los valores se recuperan directamente de las variables del proyecto, pero es posible que necesite utilizar una lógica personalizada para determinarlos.
locals { config_vars = { subnet = var.subnet_id region = var.cluster_region } }Cree el archivo
variables.tfpara definir las variables que se pueden inyectar en este proyecto.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." }Cree el archivo
terraform.tfvarspara establecer valores arbitrarios para las variables.El siguiente archivo despliega los clústeres
eu-west-1dentro de la subredsubnet-123456789, utilizando la ParallelCluster API 3.11.1 existente, que ya está implementada con el nombre de la pila.us-east-1MyParallelClusterAPI-3111region = "us-east-1" api_stack_name = "MyParallelClusterAPI-3111" api_version = "3.11.1" cluster_region = "eu-west-1" subnet_id = "subnet-123456789"Cree el archivo
outputs.tfpara definir los resultados devueltos por este proyecto.output "clusters" { value = module.pcluster.clusters }El directorio del proyecto es el siguiente:
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.