

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

# 为 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 用户可以使用其中一个模板为 Workspace 启动新集群。用户必须具有从模板创建新集群的权限。您可以在[ 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 的集群模板。如果您想了解详情，还可参阅博文 [Build a self-service environment for each line of business using Amazon EMR and 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/)（使用 Amazon EMR 和 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 版本的参数。有关更多信息，请参阅《CloudFormation 用户指南》**中的[参数](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/parameters-section-structure.html)。

以下示例 `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_cn/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. 打开 AWS Service Catalog 控制台，网址为[https://console.aws.amazon.com/servicecatalog/](https://console.aws.amazon.com/servicecatalog/)。

   1. 从左侧导航菜单中，选择 **Portfolios (产品组合)**。

   1. 在 **Create portfolio (创建产品组合) **页面输入所需信息。

   1. 选择 “**创建**”。 AWS Service Catalog 创建投资组合并显示投资组合的详细信息。

1. 使用以下步骤将集群模板添加为 AWS Service Catalog 产品。

   1. 在 AWS Service Catalog 管理控制台，导航到 **Administration (管理) **下面的 **Products（产品) **页面。

   1. 选择 **Upload new product (上传新产品)**。

   1. 输入 **Product name (产品名称) **和 **Owner (拥有者)**。

   1. 在 **Version details (详细版本) **指定模板文件。

   1. 选择 **Review (审核)**，查看您的产品设置，然后选择 **Create product (创建产品)**。

1. 完成以下步骤，将产品添加到产品组合。

   1. 导航到 AWS Service Catalog 管理控制台的 **Products (产品) **页面。

   1. 选择您的产品，选择 **Actions (操作)**，然后选择 **Add product to portfolio (添加产品至产品组合)**。

   1. 选择您的产品组合，然后选择 **Add product to portfolio (添加产品至产品组合)**。

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 管理控制台的 **Portfolios (产品组合) **页面选择产品组合。

   1. 选择**约束**选项卡并选择**创建约束**。

   1. 在 **Constraint type (约束类型) **下，选择产品，然后选择 **Launch (启动)**。选择**继续**。

   1. 选择 **Launch constraint (启动约束) **部分中的启动约束角色，然后选择**Create (创建)**。

1. 授予对产品组合的访问权限。

   1. 从 AWS Service Catalog 管理控制台的 **Portfolios (产品组合) **页面选择产品组合。

   1. 展开 **Groups, roles, and users (组、角色和用户) **选项卡然后选择** Add groups, roles, users (添加组、角色和用户)**。

   1. 在 **Roles (角色)**选 项卡搜索 EMR Studio IAM 角色，选择角色，然后选择 **Add access (添加访问)**。  
****    
[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/emr/latest/ManagementGuide/emr-studio-cluster-templates.html)