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

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

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

与 AWS SDK或RegisterJobDefinition一起使用 CLI

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

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

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

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

/** * Registers a new job definition asynchronously in AWS Batch. * <p> * When using Fargate as the compute environment, it is crucial to set the * {@link NetworkConfiguration} with {@link AssignPublicIp#ENABLED} to * ensure proper networking configuration for the Fargate tasks. This * allows the tasks to communicate with external services, access the * internet, or communicate within a VPC. * * @param jobDefinitionName the name of the job definition to be registered * @param executionRoleARN the ARN (Amazon Resource Name) of the execution role * that provides permissions for the containers in the job * @param cpuArch a value of either X86_64 or ARM64 required for the service call * @return a CompletableFuture that completes with the ARN of the registered * job definition upon successful execution, or completes exceptionally with * an error if the registration fails */ public CompletableFuture<String> registerJobDefinitionAsync(String jobDefinitionName, String executionRoleARN, String image, String cpuArch) { NetworkConfiguration networkConfiguration = NetworkConfiguration.builder() .assignPublicIp(AssignPublicIp.ENABLED) .build(); ContainerProperties containerProperties = ContainerProperties.builder() .image(image) .executionRoleArn(executionRoleARN) .resourceRequirements( Arrays.asList( ResourceRequirement.builder() .type(ResourceType.VCPU) .value("1") .build(), ResourceRequirement.builder() .type(ResourceType.MEMORY) .value("2048") .build() ) ) .networkConfiguration(networkConfiguration) .runtimePlatform(b -> b .cpuArchitecture(cpuArch) .operatingSystemFamily("LINUX")) .build(); RegisterJobDefinitionRequest request = RegisterJobDefinitionRequest.builder() .jobDefinitionName(jobDefinitionName) .type(JobDefinitionType.CONTAINER) .containerProperties(containerProperties) .platformCapabilities(PlatformCapability.FARGATE) .build(); CompletableFuture<String> future = new CompletableFuture<>(); getAsyncClient().registerJobDefinition(request) .thenApply(RegisterJobDefinitionResponse::jobDefinitionArn) .whenComplete((result, ex) -> { if (ex != null) { future.completeExceptionally(ex); } else { future.complete(result); } }); return future; }