

# AWS Batch使用 的 示例AWS CLI
<a name="cli_batch_code_examples"></a>

以下代码示例演示如何通过将 AWS Command Line Interface与 AWS Batch 结合使用，来执行操作和实现常见场景。

*操作是大型程序的代码摘录*，必须在上下文中运行。您可以通过操作了解如何调用单个服务函数，还可以通过函数相关场景的上下文查看操作。

每个示例都包含一个指向完整源代码的链接，您可以从中找到有关如何在上下文中设置和运行代码的说明。

**Topics**
+ [操作](#actions)

## 操作
<a name="actions"></a>

### `cancel-job`
<a name="batch_CancelJob_cli_topic"></a>

以下代码示例演示了如何使用 `cancel-job`。

**AWS CLI**  
**取消作业**  
此示例取消具有指定作业 ID 的作业。  
命令:  

```
aws batch cancel-job --job-id bcf0b186-a532-4122-842e-2ccab8d54efb --reason "Cancelling job."
```
+  有关 API 详细信息，请参阅《AWS CLI 命令参考》**中的 [CancelJob](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/batch/cancel-job.html)。

### `create-compute-environment`
<a name="batch_CreateComputeEnvironment_cli_topic"></a>

以下代码示例演示了如何使用 `create-compute-environment`。

**AWS CLI**  
**创建具有按需型实例的托管计算环境**  
此示例创建一个托管计算环境，具有按需启动的特定 C4 实例类型。计算环境称为 C4OnDemand。  
命令:  

```
aws batch create-compute-environment --cli-input-json file://<path_to_json_file>/C4OnDemand.json
```
JSON 文件格式：  

```
{
  "computeEnvironmentName": "C4OnDemand",
  "type": "MANAGED",
  "state": "ENABLED",
  "computeResources": {
    "type": "EC2",
    "minvCpus": 0,
    "maxvCpus": 128,
    "desiredvCpus": 48,
    "instanceTypes": [
      "c4.large",
      "c4.xlarge",
      "c4.2xlarge",
      "c4.4xlarge",
      "c4.8xlarge"
    ],
    "subnets": [
      "subnet-220c0e0a",
      "subnet-1a95556d",
      "subnet-978f6dce"
    ],
    "securityGroupIds": [
      "sg-cf5093b2"
    ],
    "ec2KeyPair": "id_rsa",
    "instanceRole": "ecsInstanceRole",
    "tags": {
      "Name": "Batch Instance - C4OnDemand"
    }
  },
  "serviceRole": "arn:aws:iam::012345678910:role/AWSBatchServiceRole"
}
```
输出：  

```
{
    "computeEnvironmentName": "C4OnDemand",
    "computeEnvironmentArn": "arn:aws:batch:us-east-1:012345678910:compute-environment/C4OnDemand"
}
```
**创建具有竞价型实例托管计算环境**  
此示例创建一个具有 M4 实例类型的托管计算环境，在竞价出价等于或低于该实例类型按需价格的 20% 时启动。计算环境称为 M4Spot。  
命令:  

```
aws batch create-compute-environment --cli-input-json file://<path_to_json_file>/M4Spot.json
```
JSON 文件格式：  

```
{
  "computeEnvironmentName": "M4Spot",
  "type": "MANAGED",
  "state": "ENABLED",
  "computeResources": {
    "type": "SPOT",
    "spotIamFleetRole": "arn:aws:iam::012345678910:role/aws-ec2-spot-fleet-role",
    "minvCpus": 0,
    "maxvCpus": 128,
    "desiredvCpus": 4,
    "instanceTypes": [
      "m4"
    ],
    "bidPercentage": 20,
    "subnets": [
      "subnet-220c0e0a",
      "subnet-1a95556d",
      "subnet-978f6dce"
    ],
    "securityGroupIds": [
      "sg-cf5093b2"
    ],
    "ec2KeyPair": "id_rsa",
    "instanceRole": "ecsInstanceRole",
    "tags": {
      "Name": "Batch Instance - M4Spot"
    }
  },
  "serviceRole": "arn:aws:iam::012345678910:role/AWSBatchServiceRole"
}
```
输出：  

```
{
    "computeEnvironmentName": "M4Spot",
    "computeEnvironmentArn": "arn:aws:batch:us-east-1:012345678910:compute-environment/M4Spot"
}
```
+  有关 API 详细信息，请参阅《AWS CLI 命令参考》**中的 [CreateComputeEnvironment](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/batch/create-compute-environment.html)。

### `create-job-queue`
<a name="batch_CreateJobQueue_cli_topic"></a>

以下代码示例演示了如何使用 `create-job-queue`。

**AWS CLI**  
**创建包含单一计算环境的低优先级作业队列**  
此示例创建一个名为 LowPriority 且使用 M4Spot 计算环境的作业队列。  
命令:  

```
aws batch create-job-queue --cli-input-json file://<path_to_json_file>/LowPriority.json
```
JSON 文件格式：  

```
{
  "jobQueueName": "LowPriority",
  "state": "ENABLED",
  "priority": 10,
  "computeEnvironmentOrder": [
    {
      "order": 1,
      "computeEnvironment": "M4Spot"
    }
  ]
}
```
输出：  

```
{
    "jobQueueArn": "arn:aws:batch:us-east-1:012345678910:job-queue/LowPriority",
    "jobQueueName": "LowPriority"
}
```
**创建包含两个计算环境的高优先级作业队列**  
此示例创建一个名为 HighPriority 的作业队列，该队列使用顺序为 1 的 C4OnDemand 计算环境和顺序为 2 的 M4Spot 计算环境。调度器将首先尝试在 C4OnDemand 计算环境中放置作业。  
命令:  

```
aws batch create-job-queue --cli-input-json file://<path_to_json_file>/HighPriority.json
```
JSON 文件格式：  

```
{
  "jobQueueName": "HighPriority",
  "state": "ENABLED",
  "priority": 1,
  "computeEnvironmentOrder": [
    {
      "order": 1,
      "computeEnvironment": "C4OnDemand"
    },
    {
      "order": 2,
      "computeEnvironment": "M4Spot"
    }
  ]
}
```
输出：  

```
{
    "jobQueueArn": "arn:aws:batch:us-east-1:012345678910:job-queue/HighPriority",
    "jobQueueName": "HighPriority"
}
```
+  有关 API 详细信息，请参阅《AWS CLI 命令参考》**中的 [CreateJobQueue](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/batch/create-job-queue.html)。

### `delete-compute-environment`
<a name="batch_DeleteComputeEnvironment_cli_topic"></a>

以下代码示例演示了如何使用 `delete-compute-environment`。

**AWS CLI**  
**删除计算环境**  
此示例删除 P2OnDemand 计算环境。  
命令:  

```
aws batch delete-compute-environment --compute-environment P2OnDemand
```
+  有关 API 详细信息，请参阅《AWS CLI 命令参考》**中的 [DeleteComputeEnvironment](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/batch/delete-compute-environment.html)。

### `delete-job-queue`
<a name="batch_DeleteJobQueue_cli_topic"></a>

以下代码示例演示了如何使用 `delete-job-queue`。

**AWS CLI**  
**删除作业队列**  
此示例删除 GPGPU 作业队列。  
命令:  

```
aws batch delete-job-queue --job-queue GPGPU
```
+  有关 API 详细信息，请参阅《AWS CLI 命令参考》**中的 [DeleteJobQueue](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/batch/delete-job-queue.html)。

### `deregister-job-definition`
<a name="batch_DeregisterJobDefinition_cli_topic"></a>

以下代码示例演示了如何使用 `deregister-job-definition`。

**AWS CLI**  
**注销作业定义**  
此示例注销名为 sleep10 的作业定义。  
命令:  

```
aws batch deregister-job-definition --job-definition sleep10
```
+  有关 API 详细信息，请参阅《AWS CLI 命令参考》**中的 [DeregisterJobDefinition](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/batch/deregister-job-definition.html)。

### `describe-compute-environments`
<a name="batch_DescribeComputeEnvironments_cli_topic"></a>

以下代码示例演示了如何使用 `describe-compute-environments`。

**AWS CLI**  
**描述计算环境**  
此示例描述 P2OnDemand 计算环境。  
命令:  

```
aws batch describe-compute-environments --compute-environments P2OnDemand
```
输出：  

```
{
    "computeEnvironments": [
        {
            "status": "VALID",
            "serviceRole": "arn:aws:iam::012345678910:role/AWSBatchServiceRole",
            "computeEnvironmentArn": "arn:aws:batch:us-east-1:012345678910:compute-environment/P2OnDemand",
            "computeResources": {
                "subnets": [
                    "subnet-220c0e0a",
                    "subnet-1a95556d",
                    "subnet-978f6dce"
                ],
                "tags": {
                    "Name": "Batch Instance - P2OnDemand"
                },
                "desiredvCpus": 48,
                "minvCpus": 0,
                "instanceTypes": [
                    "p2"
                ],
                "securityGroupIds": [
                    "sg-cf5093b2"
                ],
                "instanceRole": "ecsInstanceRole",
                "maxvCpus": 128,
                "type": "EC2",
                "ec2KeyPair": "id_rsa"
            },
            "statusReason": "ComputeEnvironment Healthy",
            "ecsClusterArn": "arn:aws:ecs:us-east-1:012345678910:cluster/P2OnDemand_Batch_2c06f29d-d1fe-3a49-879d-42394c86effc",
            "state": "ENABLED",
            "computeEnvironmentName": "P2OnDemand",
            "type": "MANAGED"
        }
    ]
}
```
+  有关 API 详细信息，请参阅《AWS CLI 命令参考》**中的 [DescribeComputeEnvironments](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/batch/describe-compute-environments.html)。

### `describe-job-definitions`
<a name="batch_DescribeJobDefinitions_cli_topic"></a>

以下代码示例演示了如何使用 `describe-job-definitions`。

**AWS CLI**  
**描述活动作业定义**  
此示例描述您的所有活动作业定义。  
命令:  

```
aws batch describe-job-definitions --status ACTIVE
```
输出：  

```
{
    "jobDefinitions": [
        {
            "status": "ACTIVE",
            "jobDefinitionArn": "arn:aws:batch:us-east-1:012345678910:job-definition/sleep60:1",
            "containerProperties": {
                "mountPoints": [],
                "parameters": {},
                "image": "busybox",
                "environment": {},
                "vcpus": 1,
                "command": [
                    "sleep",
                    "60"
                ],
                "volumes": [],
                "memory": 128,
                "ulimits": []
            },
            "type": "container",
            "jobDefinitionName": "sleep60",
            "revision": 1
        }
    ]
}
```
+  有关 API 详细信息，请参阅《AWS CLI 命令参考》**中的 [DescribeJobDefinitions](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/batch/describe-job-definitions.html)。

### `describe-job-queues`
<a name="batch_DescribeJobQueues_cli_topic"></a>

以下代码示例演示了如何使用 `describe-job-queues`。

**AWS CLI**  
**描述作业队列**  
此示例描述 HighPriority 作业队列。  
命令:  

```
aws batch describe-job-queues --job-queues HighPriority
```
输出：  

```
{
    "jobQueues": [
        {
            "status": "VALID",
            "jobQueueArn": "arn:aws:batch:us-east-1:012345678910:job-queue/HighPriority",
            "computeEnvironmentOrder": [
                {
                    "computeEnvironment": "arn:aws:batch:us-east-1:012345678910:compute-environment/C4OnDemand",
                    "order": 1
                }
            ],
            "statusReason": "JobQueue Healthy",
            "priority": 1,
            "state": "ENABLED",
            "jobQueueName": "HighPriority"
        }
    ]
}
```
+  有关 API 详细信息，请参阅《AWS CLI 命令参考》**中的 [DescribeJobQueues](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/batch/describe-job-queues.html)。

### `describe-jobs`
<a name="batch_DescribeJobs_cli_topic"></a>

以下代码示例演示了如何使用 `describe-jobs`。

**AWS CLI**  
**描述任务**  
以下 `describe-jobs` 示例描述具有指定作业 ID 的作业。  

```
aws batch describe-jobs \
    --jobs bcf0b186-a532-4122-842e-2ccab8d54efb
```
输出：  

```
{
    "jobs": [
        {
            "status": "SUBMITTED",
            "container": {
                "mountPoints": [],
                "image": "busybox",
                "environment": [],
                "vcpus": 1,
                "command": [
                    "sleep",
                    "60"
                ],
                "volumes": [],
                "memory": 128,
                "ulimits": []
            },
            "parameters": {},
            "jobDefinition": "arn:aws:batch:us-east-1:012345678910:job-definition/sleep60:1",
            "jobQueue": "arn:aws:batch:us-east-1:012345678910:job-queue/HighPriority",
            "jobId": "bcf0b186-a532-4122-842e-2ccab8d54efb",
            "dependsOn": [],
            "jobName": "example",
            "createdAt": 1480483387803
        }
    ]
}
```
+  有关 API 详细信息，请参阅《AWS CLI 命令参考》**中的 [DescribeJobs](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/batch/describe-jobs.html)。

### `list-jobs`
<a name="batch_ListJobs_cli_topic"></a>

以下代码示例演示了如何使用 `list-jobs`。

**AWS CLI**  
**列出正在运行的作业**  
此示例列出 HighPriority 作业队列中正在进行的作业。  
命令:  

```
aws batch list-jobs --job-queue HighPriority
```
输出：  

```
{
    "jobSummaryList": [
        {
            "jobName": "example",
            "jobId": "e66ff5fd-a1ff-4640-b1a2-0b0a142f49bb"
        }
    ]
}
```
**列出已提交的作业**  
此示例列出 HighPriority 作业队列中处于 SUBMITTED 作业状态的作业。  
命令:  

```
aws batch list-jobs --job-queue HighPriority --job-status SUBMITTED
```
输出：  

```
{
    "jobSummaryList": [
        {
            "jobName": "example",
            "jobId": "68f0c163-fbd4-44e6-9fd1-25b14a434786"
        }
    ]
}
```
+  有关 API 的详细信息，请参阅《AWS CLI 命令参考》**中的 [ListJobs](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/batch/list-jobs.html)。

### `register-job-definition`
<a name="batch_RegisterJobDefinition_cli_topic"></a>

以下代码示例演示了如何使用 `register-job-definition`。

**AWS CLI**  
**注册作业定义**  
此示例为一个简单容器作业注册作业定义。  
命令:  

```
aws batch register-job-definition --job-definition-name sleep30 --type container --container-properties '{ "image": "busybox", "vcpus": 1, "memory": 128, "command": [ "sleep", "30"]}'
```
输出：  

```
{
    "jobDefinitionArn": "arn:aws:batch:us-east-1:012345678910:job-definition/sleep30:1",
    "jobDefinitionName": "sleep30",
    "revision": 1
}
```
+  有关 API 详细信息，请参阅《AWS CLI 命令参考》**中的 [RegisterJobDefinition](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/batch/register-job-definition.html)。

### `submit-job`
<a name="batch_SubmitJob_cli_topic"></a>

以下代码示例演示了如何使用 `submit-job`。

**AWS CLI**  
**提交作业**  
此示例向 HighPriority 作业队列提交一个名为 example 的简单容器作业。  
命令:  

```
aws batch submit-job --job-name example --job-queue HighPriority  --job-definition sleep60
```
输出：  

```
{
    "jobName": "example",
    "jobId": "876da822-4198-45f2-a252-6cea32512ea8"
}
```
+  有关 API 详细信息，请参阅《AWS CLI 命令参考》**中的 [SubmitJob](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/batch/submit-job.html)。

### `terminate-job`
<a name="batch_TerminateJob_cli_topic"></a>

以下代码示例演示了如何使用 `terminate-job`。

**AWS CLI**  
**终止作业**  
此示例中止具有指定作业 ID 的作业。  
命令:  

```
aws batch terminate-job --job-id 61e743ed-35e4-48da-b2de-5c8333821c84 --reason "Terminating job."
```
+  有关 API 详细信息，请参阅《AWS CLI 命令参考》**中的 [TerminateJob](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/batch/terminate-job.html)。

### `update-compute-environment`
<a name="batch_UpdateComputeEnvironment_cli_topic"></a>

以下代码示例演示了如何使用 `update-compute-environment`。

**AWS CLI**  
**更新计算环境**  
此示例禁用 P2OnDemand 计算环境，以便将其删除。  
命令:  

```
aws batch update-compute-environment --compute-environment P2OnDemand --state DISABLED
```
输出：  

```
{
    "computeEnvironmentName": "P2OnDemand",
    "computeEnvironmentArn": "arn:aws:batch:us-east-1:012345678910:compute-environment/P2OnDemand"
}
```
+  有关 API 详细信息，请参阅《AWS CLI 命令参考》**中的 [UpdateComputeEnvironment](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/batch/update-compute-environment.html)。

### `update-job-queue`
<a name="batch_UpdateJobQueue_cli_topic"></a>

以下代码示例演示了如何使用 `update-job-queue`。

**AWS CLI**  
**更新作业队列**  
此示例禁用作业队列，以便将其删除。  
命令:  

```
aws batch update-job-queue --job-queue GPGPU --state DISABLED
```
输出：  

```
{
    "jobQueueArn": "arn:aws:batch:us-east-1:012345678910:job-queue/GPGPU",
    "jobQueueName": "GPGPU"
}
```
+  有关 API 详细信息，请参阅《AWS CLI 命令参考》**中的 [UpdateJobQueue](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/batch/update-job-queue.html)。