

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

# 使用任务模板
<a name="job-templates"></a>

在启动任务运行时，任务模板中存储的值可以在 `StartJobRun` API 调用之间共享。它支持两种使用场景：
+ 防止重复出现周期性的 `StartJobRun` API 请求值。
+ 强制实施必须通过 `StartJobRun` API 请求提供某些值的规则。

借助任务模板，您可以定义可重复使用的任务运行模板，以应用额外的自定义，例如：
+ 配置执行程序和驱动程序的计算容量
+ 设置安全性和监管属性，例如 IAM 角色
+ 自定义 Docker 映像以跨多个应用程序和数据管道使用

以下主题提供了有关使用模板的详细信息，包括如何使用模板启动作业运行以及如何更改模板参数。

**Topics**
+ [创建并使用作业模板启动作业运行](create-job-template.md)
+ [定义任务模板参数](use-job-template-parameters.md)
+ [控制对任务模板的访问权限](iam-job-template.md)

# 创建并使用作业模板启动作业运行
<a name="create-job-template"></a>

本节介绍如何创建作业模板以及使用模板通过 AWS Command Line Interface (AWS CLI) 开始作业运行。

**创建任务模板**

1. 创建一个 `create-job-template-request.json` 文件并指定任务模板所需的参数，如下面的示例 JSON 文件所示。有关所有可用参数的信息，请参阅 [CreateJobTemplate](https://docs.aws.amazon.com/emr-on-eks/latest/APIReference/Welcome.html)API。

   `StartJobRun` API 所需的大多数值也是 `jobTemplateData` 所必需的。如果您想为任何参数使用占位符并在 StartJobRun 使用作业模板调用时提供值，请参阅下一节有关作业模板参数的内容。

   ```
   {
      "name": "mytemplate",
      "jobTemplateData": {
           "executionRoleArn": "iam_role_arn_for_job_execution", 
           "releaseLabel": "emr-6.7.0-latest",
           "jobDriver": {
               "sparkSubmitJobDriver": { 
                   "entryPoint": "entryPoint_location",
                   "entryPointArguments": [ "argument1","argument2",...],
                   "sparkSubmitParameters": "--class <main_class> --conf spark.executor.instances=2 --conf spark.executor.memory=2G --conf spark.executor.cores=2 --conf spark.driver.cores=1"
               }
           },
           "configurationOverrides": {
               "applicationConfiguration": [
                   {
                       "classification": "spark-defaults", 
                       "properties": {
                            "spark.driver.memory":"2G"
                       }
                   }
               ], 
               "monitoringConfiguration": {
                   "persistentAppUI": "ENABLED", 
                   "cloudWatchMonitoringConfiguration": {
                       "logGroupName": "my_log_group", 
                       "logStreamNamePrefix": "log_stream_prefix"
                   }, 
                   "s3MonitoringConfiguration": {
                       "logUri": "s3://my_s3_log_location/"
                   }
               }
           }
        }
   }
   ```

1. 使用 `create-job-template` 命令和存储在本地的 `create-job-template-request.json` 文件路径。

   ```
   aws emr-containers create-job-template \ 
   --cli-input-json file://./create-job-template-request.json
   ```

**使用任务模板启动任务运行**

在 `StartJobRun` 命令中提供虚拟集群 ID、任务模板 ID 和任务名称，如以下示例所示。

```
aws emr-containers start-job-run \
--virtual-cluster-id 123456 \
--name myjob \
--job-template-id 1234abcd
```

# 定义任务模板参数
<a name="use-job-template-parameters"></a>

借助任务模板参数，您可以在任务模板中指定变量。使用该任务模板启动任务运行时，您需要指定这些参数变量的值。任务模板参数以 `${parameterName}` 格式指定。您可以选择将 `jobTemplateData` 字段中的任何值指定为任务模板参数。指定每个任务模板参数变量的数据类型（`STRING` 或 `NUMBER`），此外还可以选择指定一个默认值。以下示例演示了如何为入口点位置、主类和 S3 日志位置值指定任务模板参数。

**将入口点位置、主类和 Amazon S3 日志位置指定为任务模板参数**

1. 创建一个 `create-job-template-request.json` 文件并指定任务模板所需的参数，如下面的示例 JSON 文件所示。有关参数的更多信息，请参阅 [CreateJobTemplate](https://docs.aws.amazon.com/emr-on-eks/latest/APIReference/Welcome.html)API。

   ```
   {
      "name": "mytemplate",
      "jobTemplateData": {
           "executionRoleArn": "iam_role_arn_for_job_execution", 
           "releaseLabel": "emr-6.7.0-latest",
           "jobDriver": {
               "sparkSubmitJobDriver": { 
                   "entryPoint": "${EntryPointLocation}",
                   "entryPointArguments": [ "argument1","argument2",...],
                   "sparkSubmitParameters": "--class ${MainClass} --conf spark.executor.instances=2 --conf spark.executor.memory=2G --conf spark.executor.cores=2 --conf spark.driver.cores=1"
               }
           },
           "configurationOverrides": {
               "applicationConfiguration": [
                   {
                       "classification": "spark-defaults", 
                       "properties": {
                            "spark.driver.memory":"2G"
                       }
                   }
               ], 
               "monitoringConfiguration": {
                   "persistentAppUI": "ENABLED", 
                   "cloudWatchMonitoringConfiguration": {
                       "logGroupName": "my_log_group", 
                       "logStreamNamePrefix": "log_stream_prefix"
                   }, 
                   "s3MonitoringConfiguration": {
                       "logUri": "${LogS3BucketUri}"
                   }
               }
           },
           "parameterConfiguration": {
               "EntryPointLocation": {
                   "type": "STRING"
               },
               "MainClass": {
                   "type": "STRING",
                   "defaultValue":"Main"
               },
               "LogS3BucketUri": {
                   "type": "STRING",
                   "defaultValue":"s3://my_s3_log_location/"
               }
           }
       }
   }
   ```

1. 使用 `create-job-template` 命令和存储在本地或 Amazon S3 上的 `create-job-template-request.json` 文件路径。

   ```
   aws emr-containers create-job-template \ 
   --cli-input-json file://./create-job-template-request.json
   ```

**使用具有任务模板参数的任务模板启动任务运行**

要使用包含任务模板参数的任务模板启动运行任务，请在 `StartJobRun` API 请求中指定任务模板 ID 以及任务模板参数的值，如下例所示。

```
aws emr-containers start-job-run \
--virtual-cluster-id 123456 \
--name myjob \
--job-template-id 1234abcd \
--job-template-parameters '{"EntryPointLocation": "entry_point_location","MainClass": "ExampleMainClass","LogS3BucketUri": "s3://example_s3_bucket/"}'
```

# 控制对任务模板的访问权限
<a name="iam-job-template"></a>

借助 `StartJobRun` 策略，您可以强制要求某个用户或角色只能使用您指定的任务模板运行任务，如果不使用指定的任务模板，则无法运行 `StartJobRun` 操作。要实现此目的，请首先确保向该用户或角色授予对指定任务模板的读取权限，如下例所示。

------
#### [ JSON ]

****  

```
{
  "Version":"2012-10-17",		 	 	 
  "Statement": [
    {
      "Effect": "Allow",
      "Action": [
        "emr-containers:DescribeJobRun"
      ],
      "Resource": [
        "arn:aws:emr-containers:*:*:jobtemplate/job_template_1_id",
        "arn:aws:emr-containers:*:*:jobtemplate/job_template_2_id"
      ],
      "Sid": "AllowEMRCONTAINERSDescribejobtemplate"
    }
  ]
}
```

------

要强制某个用户或角色只能在使用指定任务模板时调用 `StartJobRun` 操作，您可以将以下 `StartJobRun` 策略权限分配给该用户或角色。

------
#### [ JSON ]

****  

```
{
  "Version":"2012-10-17",		 	 	 
  "Statement": [
    {
      "Effect": "Allow",
      "Action": [
        "emr-containers:StartJobRun"
      ],
      "Resource": [
        "arn:aws:emr-containers:*:*:/virtualclusters/virtual_cluster_id"
      ],
      "Condition": {
        "ArnLike": {
          "emr-containers:JobTemplateArn": [
            "arn:aws:emr-containers:*:*:jobtemplate/job_template_1_id",
            "arn:aws:emr-containers:*:*:jobtemplate/job_template_2_id"
          ]
        }
      },
      "Sid": "AllowEMRCONTAINERSStartjobrun"
    }
  ]
}
```

------

如果任务模板在执行角色 ARN 字段中指定了任务模板参数，则用户将能够提供该参数的值，从而使用任意执行角色来调用 `StartJobRun`。要限制用户可以提供的执行角色，请参阅 [将任务执行角色与 Amazon EMR on EKS 结合使用](iam-execution-role.md) 中的**控制对执行角色的访问权限**。

如果在上述 `StartJobRun` 操作策略中没有为给定用户或角色指定任何条件，则表示允许该用户或角色使用其具有读取权限的任意任务模板或使用任意执行角色在指定的虚拟集群上调用 `StartJobRun` 操作。