

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

# 建立 Amazon EMR Studio 的 AWS CloudFormation 範本
<a name="emr-studio-cluster-templates"></a>

## 關於 EMR Studio 叢集範本
<a name="emr-studio-about-cluster-templates"></a>

您可以建立 AWS CloudFormation 範本，以協助 EMR Studio 使用者在工作區中啟動新的 Amazon EMR 叢集。CloudFormation 範本是採用 JSON 或 YAML 格式的文本檔案。在範本中，您會描述 AWS 資源堆疊，並告知 CloudFormation 如何為您佈建這些資源。對於 EMR Studio，可以建立一個或多個描述 Amazon EMR 叢集的範本。

您可以在 中組織範本 AWS Service Catalog。 AWS Service Catalog 可讓您建立和管理通常部署的 IT 服務，稱為 *產品* AWS。可以在與 EMR Studio 使用者共用的*產品組合*中將範本當作產品進行收集。建立叢集範本之後，Studio 使用者可以使用其中一個範本為工作區啟動新叢集。使用者必須具有從範本建立新叢集的許可。可以在 [EMR Studio 許可政策](emr-studio-user-permissions.md)中設定使用者許可。

若要進一步了解 CloudFormation 範本，請參閱《AWS CloudFormation 使用者指南》**中的[範本](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/cfn-whatis-concepts.html#w2ab1b5c15b7)。如需 的詳細資訊 AWS Service Catalog，請參閱[什麼是 AWS Service Catalog](https://docs.aws.amazon.com/servicecatalog/latest/adminguide/introduction.html)。

以下影片示範如何在適用於 EMR Studio 的 AWS Service Catalog 中設定叢集範本。也可以在[使用 Amazon EMR 和 Service Catalog 為每個業務單位建立自助服務環境](https://aws.amazon.com/blogs/big-data/build-a-self-service-environment-for-each-line-of-business-using-amazon-emr-and-aws-service-catalog/)部落格文章中深入了解。

[![AWS Videos](http://img.youtube.com/vi/https://www.youtube.com/embed/9w_TXTdFLpo/0.jpg)](http://www.youtube.com/watch?v=https://www.youtube.com/embed/9w_TXTdFLpo)


### 選用範本參數
<a name="emr-studio-cluster-template-parameters"></a>

可以在範本的 [https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/parameters-section-structure.html](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/parameters-section-structure.html) 區段中包含其他選項。*參數*可讓 Studio 使用者輸入或選取叢集的自訂值。例如，可以新增能讓使用者選取特定 Amazon EMR 版本的參數。如需更多詳細資訊，請參閱 [https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/parameters-section-structure.html](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/parameters-section-structure.html) 使用者指南*中的CloudFormation 參數*部分。

下列範例 `Parameters` 區段會定義其他輸入參數，例如 `ClusterName`、`EmrRelease` 版本以及 `ClusterInstanceType`。

```
Parameters:
  ClusterName:
    Type: "String"
    Default: "Cluster_Name_Placeholder"
  EmrRelease:
    Type: "String"
    Default: "emr-6.2.0"
    AllowedValues:
    - "emr-6.2.0"
    - "emr-5.32.0"
  ClusterInstanceType:
    Type: "String"
    Default: "m5.xlarge"
    AllowedValues:
    - "m5.xlarge"
    - "m5.2xlarge"
```

當您新增參數時，Studio 使用者會在選取叢集範本後看到其他表單選項。下圖顯示 **EmrRelease** 版本、**ClusterName** 以及 **InstanceType** 的其他表單選項。

![\[當使用者選取具有參數的叢集範本時，Studio 使用者介面中其他輸入的螢幕擷取畫面。\]](http://docs.aws.amazon.com/zh_tw/emr/latest/ManagementGuide/images/cluster-template-parameters-studio-ui.jpg)


## 先決條件
<a name="emr-studio-cluster-template-prereqs"></a>

建立叢集範本之前，請確定您具有存取 Service Catalog 管理員主控台檢視的 IAM 許可。您也需要必要的 IAM 許可，才能執行 Service Catalog 管理任務。如需詳細資訊，請參閱[將許可授予給 Service Catalog 管理員](https://docs.aws.amazon.com/servicecatalog/latest/adminguide/getstarted-iamadmin.html)。

## 建立 EMR 叢集範本
<a name="emr-studio-cluster-template-instructions"></a>

**使用 Service Catalog 建立 EMR 叢集範本**

1. 建立一個或多個 CloudFormation 範本。儲存範本的位置由您決定。由於範本是格式化的文字檔案，因此可以將它們上傳到 Amazon S3 或保存在本機檔案系統中。若要進一步了解 CloudFormation 範本，請參閱《AWS CloudFormation 使用者指南》**中的[範本](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/cfn-whatis-concepts.html#w2ab1b5c15b7)。

   使用下列規則來命名範本，或根據模式 `[a-zA-Z0-9][a-zA-Z0-9._-]*` 檢查名稱。
   + 範本名稱開頭必須是字母或數字。
   + 範本名稱只能由字母、數字、句點 (.)、底線 (\$1) 和連字號 (-) 組成。

   您建立的每個叢集範本都必須包含下列選項：

   **輸入參數**
   + ClusterName - 叢集的名稱，可協助使用者在佈建後加以識別。

   **輸出**
   + `ClusterId` - 新佈建之 EMR 叢集的 ID。

   以下是具有兩個節點之叢集的 YAML 格式範例 CloudFormation 範本。範例範本包含必要的範本選項，並定義 `EmrRelease` 和 `ClusterInstanceType` 的其他輸入參數。

   ```
   awsTemplateFormatVersion: 2010-09-09
   
   Parameters:
     ClusterName:
       Type: "String"
       Default: "Example_Two_Node_Cluster"
     EmrRelease:
       Type: "String"
       Default: "emr-6.2.0"
       AllowedValues:
       - "emr-6.2.0"
       - "emr-5.32.0"
     ClusterInstanceType:
       Type: "String"
       Default: "m5.xlarge"
       AllowedValues:
       - "m5.xlarge"
       - "m5.2xlarge"
   
   Resources:
     EmrCluster:
       Type: AWS::EMR::Cluster
       Properties:
         Applications:
         - Name: Spark
         - Name: Livy
         - Name: JupyterEnterpriseGateway
         - Name: Hive
         EbsRootVolumeSize: '10'
         Name: !Ref ClusterName
         JobFlowRole: EMR_EC2_DefaultRole
         ServiceRole: EMR_DefaultRole_V2
         ReleaseLabel: !Ref EmrRelease
         VisibleToAllUsers: true
         LogUri: 
           Fn::Sub: 's3://aws-logs-${AWS::AccountId}-${AWS::Region}/elasticmapreduce/'
         Instances:
           TerminationProtected: false
           Ec2SubnetId: 'subnet-ab12345c'
           MasterInstanceGroup:
             InstanceCount: 1
             InstanceType: !Ref ClusterInstanceType
           CoreInstanceGroup:
             InstanceCount: 1
             InstanceType: !Ref ClusterInstanceType
             Market: ON_DEMAND
             Name: Core
   
   Outputs:
     ClusterId:
       Value:
         Ref: EmrCluster
       Description: The ID of the  EMR cluster
   ```

1. 在與 Studio 相同的 AWS 帳戶中為您的叢集範本建立產品組合。

   1. 在 https：//[https://console.aws.amazon.com/servicecatalog/](https://console.aws.amazon.com/servicecatalog/) 開啟 AWS Service Catalog 主控台。

   1. 在左側導覽功能表中選擇**產品組合**。

   1. 在**建立產品組合**頁面中輸入要求的資訊。

   1. 選擇 **Create**. AWS Service Catalog create 產品組合並顯示產品組合詳細資訊。

1. 使用以下步驟，將叢集範本新增為 AWS Service Catalog 產品。

   1. 導覽至 AWS Service Catalog 管理主控台中**管理**下的**產品**頁面。

   1. 選擇**上傳新產品**。

   1. 輸入**產品名稱**和**擁有者**。

   1. 在**版本詳細資訊**下指定範本檔案。

   1. 選擇**檢閱**來檢閱您的產品設定，然後選擇**建立產品**。

1. 請完成以下步驟，將您的產品新增到產品組合。

   1. 導覽至 AWS Service Catalog 管理主控台中的**產品**頁面。

   1. 選擇產品，選擇**動作**，然後選擇**將產品新增至產品組合**。

   1. 選擇您的產品組合，然後選擇**將產品新增至產品組合**。

1. 為您的產品建立啟動限制。啟動限制是一個 IAM 角色，它可指定用於啟動產品的使用者許可。可以量身定製啟動限制，但必須允許使用 CloudFormation、Amazon EMR 和 AWS Service Catalog。如需詳細資訊和指示，請參閱 [Service Catalog 啟動限制](https://docs.aws.amazon.com/servicecatalog/latest/adminguide/constraints-launch.html)。

1. 將啟動限制套用至產品組合中的每個產品。必須將啟動限制單獨套用至每個產品。

   1. 從 AWS Service Catalog 管理主控台的**產品組合**頁面中選取您的產品組合。

   1. 選擇 **Constraints (限制)** 索引標籤，並選擇 **Create constraint (建立限制)**。

   1. 選擇您的產品並在**限制類型**下選擇**啟動**。選擇**繼續**。

   1. 在**啟動限制**區段中選取您的啟動限制角色，然後選擇**建立**。

1. 授予對產品組合的存取權。

   1. 從 AWS Service Catalog 管理主控台的**產品組合**頁面中選取您的產品組合。

   1. 展開**群組、角色和使用者**索引標籤，然後選擇**新增群組、角色和使用者**。

   1. 在**角色**索引標籤中搜尋您的 EMR Studio IAM 角色，選取您的角色，然後選擇**新增存取權限**。  
****    
[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/emr/latest/ManagementGuide/emr-studio-cluster-templates.html)