

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 使用 建立叢集 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)。
+ 具有執行 CLI 所需[許可](iam-roles-in-parallelcluster-v3.md#iam-roles-in-parallelcluster-v3-example-user-policies)的 IAM [`pcluster`](pcluster-v3.md) 角色。

## 使用 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 堆疊，分別用於執行叢集前端節點和運算節點。
   + 巢狀 CloudFormation 堆疊，具有用於管理叢集的 AWS ParallelCluster 自訂資源。
   + 巢狀 CloudFormation 堆疊，具有管理叢集的預設政策。
   + 巢狀堆疊的根 CloudFormation 堆疊。
   + 具有Slurm排程器和已定義運算節點數量的 AWS ParallelCluster 叢集。  
![\[主控台 CloudFormation 快速建立使用者介面。\]](http://docs.aws.amazon.com/zh_tw/parallelcluster/latest/ug/images/cfn-quick-create.png)

1. 在**快速建立堆疊****參數**區段中，輸入下列參數的值：

   1. 針對 **KeyName**，輸入 Amazon EC2 金鑰對的名稱。

   1. 針對 **AvailabilityZone**，為您的叢集節點選擇 AZ，例如 `us-east-1a`。

1. 勾選方塊以確認頁面底部的每個存取功能。

1. 選擇**建立堆疊**。

1. 等待 CloudFormation 堆疊達到 `CREATE_COMPLETE` 狀態。

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

在本教學課程中，您會使用 CloudFormation 的 AWS 命令列界面 (CLI) 來部署建立叢集的 CloudFormation 範本。

**建立下列 AWS 資源：**
+ 使用 CloudFormation 快速建立堆疊建立的根 CloudFormation 堆疊。
+ 巢狀 CloudFormation 堆疊，其中包含預設政策、預設 VPC 設定和自訂資源提供者。
+ 您可以登入並執行任務的範例 AWS ParallelCluster 叢集堆疊和叢集。

以您自己的值取代以*紅色反白顯示的輸入*，例如*金鑰對*。

**使用 建立叢集 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_tw/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
   ```