AWS Batch 使用示例 AWS CLI - AWS Command Line Interface

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

AWS Batch 使用示例 AWS CLI

以下代码示例向您展示了如何使用with来执行操作和实现常见场景 AWS Batch。 AWS Command Line Interface

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

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

主题

操作

以下代码示例显示了如何使用cancel-job

AWS CLI

取消任务

此示例取消具有指定作业 ID 的作业。

命令:

aws batch cancel-job --job-id bcf0b186-a532-4122-842e-2ccab8d54efb --reason "Cancelling job."

以下代码示例显示了如何使用create-compute-environment

AWS CLI

使用按需实例创建托管计算环境

此示例创建了一个托管计算环境,该环境具有按需启动的特定 C4 实例类型。计算环境称为 C4 OnDemand。

命令:

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" }

使用 Spot 实例创建托管计算环境

此示例使用 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" }

以下代码示例显示了如何使用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 的 C4 OnDemand 计算环境和阶数为 2 的 M4Spot 计算环境。调度器将首先尝试在 C4 OnDemand 计算环境中放置作业。

命令:

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" }

以下代码示例显示了如何使用delete-compute-environment

AWS CLI

删除计算环境

此示例删除了 P2 OnDemand 计算环境。

命令:

aws batch delete-compute-environment --compute-environment P2OnDemand

以下代码示例显示了如何使用delete-job-queue

AWS CLI

删除作业队列

此示例删除了GPGPU作业队列。

命令:

aws batch delete-job-queue --job-queue GPGPU

以下代码示例显示了如何使用deregister-job-definition

AWS CLI

取消注册作业定义

此示例取消注册名为 sleep10 的作业定义。

命令:

aws batch deregister-job-definition --job-definition sleep10

以下代码示例显示了如何使用describe-compute-environments

AWS CLI

描述计算环境

此示例描述了 P2 OnDemand 计算环境。

命令:

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" } ] }

以下代码示例显示了如何使用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 } ] }

以下代码示例显示了如何使用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" } ] }

以下代码示例显示了如何使用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中的。

以下代码示例显示了如何使用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中的。

以下代码示例显示了如何使用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 }

以下代码示例显示了如何使用submit-job

AWS CLI

提交作业

此示例向作业队列提交了一个名为 example 的简单容器 HighPriority 作业。

命令:

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中的。

以下代码示例显示了如何使用terminate-job

AWS CLI

终止作业

此示例终止具有指定作业 ID 的作业。

命令:

aws batch terminate-job --job-id 61e743ed-35e4-48da-b2de-5c8333821c84 --reason "Terminating job."
  • 有关API详细信息,请参阅AWS CLI 命令参考TerminateJob中的。

以下代码示例显示了如何使用update-compute-environment

AWS CLI

更新计算环境

此示例禁用 P2 OnDemand 计算环境,因此可以将其删除。

命令:

aws batch update-compute-environment --compute-environment P2OnDemand --state DISABLED

输出:

{ "computeEnvironmentName": "P2OnDemand", "computeEnvironmentArn": "arn:aws:batch:us-east-1:012345678910:compute-environment/P2OnDemand" }

以下代码示例显示了如何使用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中的。