与 AWS SDK或CreateComputeEnvironment一起使用 CLI - AWS SDK代码示例

AWS 文档 AWS SDK示例 GitHub 存储库中还有更多SDK示例

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

与 AWS SDK或CreateComputeEnvironment一起使用 CLI

以下代码示例演示如何使用 CreateComputeEnvironment

操作示例是大型程序的代码摘录,必须在上下文中运行。在以下代码示例中,您可以查看此操作的上下文:

CLI
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" }
Java
SDK适用于 Java 2.x
注意

还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库中进行设置和运行。

/** * Asynchronously creates a new compute environment in AWS Batch. * * @param computeEnvironmentName the name of the compute environment to create * @param batchIAMRole the IAM role to be used by the compute environment * @param subnet the subnet ID to be used for the compute environment * @param secGroup the security group ID to be used for the compute environment * @return a {@link CompletableFuture} representing the asynchronous operation, which will complete with the * {@link CreateComputeEnvironmentResponse} when the compute environment has been created * @throws BatchException if there is an error creating the compute environment * @throws RuntimeException if there is an unexpected error during the operation */ public CompletableFuture<CreateComputeEnvironmentResponse> createComputeEnvironmentAsync( String computeEnvironmentName, String batchIAMRole, String subnet, String secGroup) { CreateComputeEnvironmentRequest environmentRequest = CreateComputeEnvironmentRequest.builder() .computeEnvironmentName(computeEnvironmentName) .type(CEType.MANAGED) .state(CEState.ENABLED) .computeResources(ComputeResource.builder() .type(CRType.FARGATE) .maxvCpus(256) .subnets(Collections.singletonList(subnet)) .securityGroupIds(Collections.singletonList(secGroup)) .build()) .serviceRole(batchIAMRole) .build(); CompletableFuture<CreateComputeEnvironmentResponse> response = getAsyncClient().createComputeEnvironment(environmentRequest); response.whenComplete((resp, ex) -> { if (ex != null) { String errorMessage = "Unexpected error occurred: " + ex.getMessage(); throw new RuntimeException(errorMessage, ex); } }); return response; }