Utilizzare CreateComputeEnvironment con un AWS SDK o CLI - Esempi di codice dell'AWS SDK

Ci sono altri AWS SDK esempi disponibili nel repository AWS Doc SDK Examples GitHub .

Le traduzioni sono generate tramite traduzione automatica. In caso di conflitto tra il contenuto di una traduzione e la versione originale in Inglese, quest'ultima prevarrà.

Utilizzare CreateComputeEnvironment con un AWS SDK o CLI

I seguenti esempi di codice mostrano come utilizzareCreateComputeEnvironment.

Gli esempi di operazioni sono estratti di codice da programmi più grandi e devono essere eseguiti nel contesto. È possibile visualizzare questa operazione nel contesto nel seguente esempio di codice:

CLI
AWS CLI

Per creare un ambiente di elaborazione gestito con istanze On-Demand

Questo esempio crea un ambiente di elaborazione gestito con tipi di istanze C4 specifici che vengono lanciati su richiesta. L'ambiente di calcolo si chiama C4. OnDemand

Comando:

aws batch create-compute-environment --cli-input-json file://<path_to_json_file>/C4OnDemand.json

JSONformato di file:

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

Output:

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

Per creare un ambiente di elaborazione gestito con istanze Spot

Questo esempio crea un ambiente di elaborazione gestito con il tipo di istanza M4 che viene lanciato quando il prezzo dell'offerta Spot è pari o inferiore al 20% del prezzo On-Demand per il tipo di istanza. L'ambiente di calcolo si chiama M4Spot.

Comando:

aws batch create-compute-environment --cli-input-json file://<path_to_json_file>/M4Spot.json

JSONformato di file:

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

Output:

{ "computeEnvironmentName": "M4Spot", "computeEnvironmentArn": "arn:aws:batch:us-east-1:012345678910:compute-environment/M4Spot" }
Java
SDKper Java 2.x
Nota

C'è altro su. GitHub Trova l'esempio completo e scopri di più sulla configurazione e l'esecuzione nel Repository di esempi di codice 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; }