文件範例儲存庫中有更多 AWS SDK可用的範例。 AWS SDK
本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
CreateComputeEnvironment
搭配 AWS SDK或 使用 CLI
下列程式碼範例示範如何使用 CreateComputeEnvironment
。
動作範例是大型程式的程式碼摘錄,必須在內容中執行。您可以在下列程式碼範例的內容中看到此動作:
- CLI
-
- 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" }
使用 Spot 執行個體建立受管運算環境
此範例會建立 M4 執行個體類型的受管運算環境,當 Spot 出價等於或低於執行個體類型的隨需價格的 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詳細資訊,請參閱 命令參考 CreateComputeEnvironment
中的 。 AWS CLI
-
- 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; }
-
如需API詳細資訊,請參閱 參考 CreateComputeEnvironment中的 。 AWS SDK for Java 2.x API
-
動作
CreateJobQueue