

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

# 使用创建集群 CloudFormation
<a name="tutorials_09_cfn-custom-resource-v3"></a>

学习如何使用 AWS ParallelCluster CloudFormation 自定义资源创建集群。有关更多信息，请参阅 [AWS CloudFormation 自定义资源](cloudformation-v3.md)。

使用时 AWS ParallelCluster，您只需为创建或更新 AWS ParallelCluster 映像和集群时创建的 AWS 资源付费。有关更多信息，请参阅 [AWS 使用的服务 AWS ParallelCluster](aws-services-v3.md)。

**先决条件：**
+  AWS CLI [已安装并配置。](https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html)
+ [Amazon EC2 密钥对](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-key-pairs.html)。
+ 具有运行 [`pcluster`](pcluster-v3.md) CLI 所需的[权限](iam-roles-in-parallelcluster-v3.md#iam-roles-in-parallelcluster-v3-example-user-policies)的 IAM 角色。

## 使用 CloudFormation 快速创建堆栈创建集群
<a name="cfn-custom-resource-quick-v3"></a>

在本教程中，您将使用快速创建堆栈来部署用于创建集群的 CloudFormation 模板和以下 AWS 资源：
+ 使用 CloudFormation 快速创建 CloudFormation 堆栈创建的根堆栈。
+ 嵌套 CloudFormation 堆栈，包括默认策略、默认 VPC 设置和自定义资源提供程序。
+ 示例 AWS ParallelCluster 集群堆栈和您可以登录并运行作业的集群。

**使用创建集群 AWS CloudFormation**

1. 登录到 AWS 管理控制台。

1. 打开 CloudFormation [快速创建链接](https://us-east-1.console.aws.amazon.com/cloudformation/home?region=us-east-1#/stacks/create/review?stackName=mycluster&templateURL=https://us-east-1-aws-parallelcluster.s3.amazonaws.com/parallelcluster/3.15.0/templates/1-click/cluster-example.yaml)，在 CloudFormation 控制台中创建以下资源：
   + 带有 VPC 的嵌套 CloudFormation 堆栈，其中包含公有子网和私有子网，分别用于运行集群头节点和计算节点。
   + 带有用于管理群集的 AWS ParallelCluster 自定义资源的嵌套 CloudFormation 堆栈。
   + 带有用于管理集群的默认策略的嵌套 CloudFormation 堆栈。
   + 嵌套 CloudFormation 堆栈的根堆栈。
   + 具有Slurm调度程序和已定义数量的计算节点的 AWS ParallelCluster 集群。  
![\[控制台 CloudFormation 快速创建用户界面。\]](http://docs.aws.amazon.com/zh_cn/parallelcluster/latest/ug/images/cfn-quick-create.png)

1. 在**快速创建堆栈****参数**部分，输入以下参数的值：

   1. 对于 **KeyName**，请输入您的 Amazon EC2 密钥对的名称。

   1. 对于 **AvailabilityZone**，为集群节点选择可用区，例如`us-east-1a`。

1. 在页面底部选中各个框以确认各项访问功能。

1. 选择**创建堆栈**。

1. 等待 CloudFormation 堆栈达到`CREATE_COMPLETE`状态。

## 使用 CloudFormation 命令行界面 (CLI) 创建集群
<a name="cfn-custom-resource-cli-v3"></a>

在本教程中，您将使用用于的 AWS 命令行界面 (CLI) CloudFormation 来部署用于创建集群的 CloudFormation 模板。

**创建以下 AWS 资源：**
+ 使用 CloudFormation 快速创建 CloudFormation 堆栈创建的根堆栈。
+ 嵌套 CloudFormation 堆栈，包括默认策略、默认 VPC 设置和自定义资源提供程序。
+ 示例 AWS ParallelCluster 集群堆栈和您可以登录并运行作业的集群。

用您自己的值替换*inputs highlighted in red*（例如*keypair*）。

**使用创建集群 AWS CloudFormation**

1. 使用以下内容创建`cluster_template.yaml`名为的 CloudFormation 模板：

   ```
   AWSTemplateFormatVersion: '2010-09-09'
   Description: > AWS ParallelCluster CloudFormation Template
   
   Parameters:
     KeyName:
       Description: KeyPair to login to the head node
       Type: AWS::EC2::KeyPair::KeyName
   
     AvailabilityZone:
       Description: Availability zone where instances will be launched
       Type: AWS::EC2::AvailabilityZone::Name
       Default: us-east-2a
   
   Mappings:
     ParallelCluster:
       Constants:
         Version: 3.15.0
   
   Resources:
     PclusterClusterProvider:
       Type: AWS::CloudFormation::Stack
       Properties:
         TemplateURL: !Sub
           - https://${AWS::Region}-aws-parallelcluster.s3.${AWS::Region}.${AWS::URLSuffix}/parallelcluster/${Version}/templates/custom_resource/cluster.yaml
           - { Version: !FindInMap [ParallelCluster, Constants, Version] }
   
     PclusterVpc:
       Type: AWS::CloudFormation::Stack
       Properties:
         Parameters:
           PublicCIDR: 10.0.0.0/24
           PrivateCIDR: 10.0.16.0/20
           AvailabilityZone: !Ref AvailabilityZone
         TemplateURL: !Sub
           - https://${AWS::Region}-aws-parallelcluster.s3.${AWS::Region}.${AWS::URLSuffix}/parallelcluster/${Version}/templates/networking/public-private-${Version}.cfn.json
           - { Version: !FindInMap [ParallelCluster, Constants, Version ] }
   
     PclusterCluster:
       Type: Custom::PclusterCluster
       Properties:
         ServiceToken: !GetAtt [ PclusterClusterProvider , Outputs.ServiceToken ]
         ClusterName: !Sub 'c-${AWS::StackName}'
         ClusterConfiguration:
           Image:
             Os: alinux2
           HeadNode:
             InstanceType: t2.medium
             Networking:
               SubnetId: !GetAtt [ PclusterVpc , Outputs.PublicSubnetId ]
             Ssh:
               KeyName: !Ref KeyName
           Scheduling:
             Scheduler: slurm
             SlurmQueues:
             - Name: queue0
               ComputeResources:
               - Name: queue0-cr0
                 InstanceType: t2.micro
               Networking:
                 SubnetIds:
                 -  !GetAtt [ PclusterVpc , Outputs.PrivateSubnetId ]
   Outputs:
     HeadNodeIp:
       Description: The Public IP address of the HeadNode
       Value: !GetAtt [ PclusterCluster, headNode.publicIpAddress ]
   ```

1. 运行以下 AWS CLI 命令部署 CloudFormation 堆栈以进行集群创建和管理。

   ```
   $ aws cloudformation deploy --template-file ./cluster_template.yaml \
     --stack-name mycluster \
     --parameter-overrides KeyName=keypair \
                           AvailabilityZone=us-east-2b \
     --capabilities CAPABILITY_NAMED_IAM CAPABILITY_AUTO_EXPAND
   ```

## 查看 CloudFormation 集群输出
<a name="cfn-custom-resource-view-v3"></a>

查看集 CloudFormation 群输出以获取有用的集群详细信息。添加的 `ValidationMessages` 属性允许访问集群创建和更新操作中的验证消息。

1. 导航到[CloudFormation 控制台](https://console.aws.amazon.com/cloudformation/home)并选择包含您的 AWS ParallelCluster 自定义资源的堆栈。

1. 选择**堆栈详细信息**，然后选择**输出**选项卡。  
![\[控制台 CloudFormation 输出表显示了 HeadNodeIp 和的值 ValidationMessages。\]](http://docs.aws.amazon.com/zh_cn/parallelcluster/latest/ug/images/cfn-outputs.png)

   验证消息可能会被截断。有关如何检索日志的更多信息，请参阅[AWS ParallelCluster 故障排除](troubleshooting-v3.md)。

## 访问您的集群
<a name="cfn-custom-resource-access-v3"></a>

访问集群。

**`ssh` 登录到集群头节点**

1.  CloudFormation 堆栈部署完成后，使用以下命令获取头节点的 IP 地址：

   ```
   $ HEAD_NODE_IP=$(aws cloudformation describe-stacks --stack-name=mycluster --query "Stacks|[0].Outputs[?OutputKey=='HeadNodeIp']|[0].OutputValue" --output=text)
   ```

   您也可以从 CloudFormation 控制台集群堆栈**输出**选项卡中的**HeadNodeIp**参数中检索头节点 IP 地址。

   您可以在此处找到头节点 IP 地址，因为它是在集群 CloudFormation 模板的`Outputs`部分中添加的，专门针对此示例集群。

1. 通过运行以下命令，连接集群头节点：

   ```
   $ ssh -i keyname.pem ec2-user@$HEAD_NODE_IP
   ```

## 清理
<a name="cfn-custom-resource-cleanup-v3"></a>

请删除集群。

1. 运行以下 AWS CLI 命令删除 CloudFormation 堆栈和集群。

   ```
   $ aws cloudformation delete-stack --stack-name=mycluster
   ```

1. 通过运行以下命令，检查堆栈删除状态。

   ```
   $ aws cloudformation describe-stacks --stack-name=mycluster
   ```