Submit a job to AWS Batch with Java (Eclipse)

I tried sending a job to AWS Batch in Java (Eclipse), so I will share it.

Premise

The following is set in AWS Batch. -Job definition name: sample-job-definition -Queue name: sample-job-queue -Computing environment name: sample-compute-environment

I'm doing the following in Eclipse: -Install AWS Toolkit for Eclipse. -Set the access key and secret access key of IAM user in AWS Toolkit for Eclipse. -Create a Gradle project.

Edit build.gradle, install SDK

Add ʻaws-java-sdk-batch to dependencies` and install it in your project. (Right-click the project and refresh the Gradle project to start the process)

Add to dependencies


dependencies {
… Abbreviation
    compile('com.amazonaws:aws-java-sdk-batch:1.11.683')
    // compile group: 'com.amazonaws', name: 'aws-java-sdk-batch', version: '1.11.683'    //This way of writing is also OK
… Abbreviation
}

Java sample code

I created the following trial code with reference to the document.

Sample code


package sample;

import java.util.HashMap;
import java.util.Map;

import com.amazonaws.regions.Regions;
import com.amazonaws.services.batch.AWSBatch;
import com.amazonaws.services.batch.AWSBatchClientBuilder;
import com.amazonaws.services.batch.model.SubmitJobRequest;
import com.amazonaws.services.batch.model.SubmitJobResult;

public class AwsBatchSample {

    public static void main(String[] args) {
        AwsBatchSample awsBatchSample = new AwsBatchSample();
        ab.awsBatchJobSend();
    }

    private void awsBatchJobSend() {
        AWSBatch client = null;
        try {
            client = AWSBatchClientBuilder.standard().withRegion(Regions.AP_NORTHEAST_1).build();
            Map<String,String> parameters = new HashMap<String, String>();
            parameters.put("param1", "JobParam1");
            //Command in job definition: echo sample-batch Ref::param1
            //Output result in log: sample-batch JobParam1
            SubmitJobRequest request = new SubmitJobRequest()
                                            .withJobName("sample-job-submit")
                                            .withJobQueue("sample-job-queue")
                                            .withJobDefinition("sample-job-definition")
                                            .withParameters(parameters)
                                            ;
            SubmitJobResult response = client.submitJob(request);
            System.out.println(response);
        } catch (Exception e) {
            System.out.println(e);
        } finally {
            if (client != null) {
                client.shutdown();
            }
        }
    }
}

If the job transmission is successful, the following will be returned.


{JobName: sample-job-submit,JobId: aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa}

Fitting point

It wasn't specified in the document example, If you do not specify .withRegion (Regions.AP_NORTHEAST_1), the following error is output.

Error log


20:31:11.636 [main] DEBUG com.amazonaws.AmazonWebServiceClient - Internal logging successfully configured to commons logger: true
20:31:11.998 [main] DEBUG com.amazonaws.monitoring.CsmConfigurationProviderChain - Unable to load configuration from com.amazonaws.monitoring.EnvironmentVariableCsmConfigurationProvider@17fc391b: Unable to load Client Side Monitoring configurations from environment variables!
20:31:11.998 [main] DEBUG com.amazonaws.monitoring.CsmConfigurationProviderChain - Unable to load configuration from com.amazonaws.monitoring.SystemPropertyCsmConfigurationProvider@2b30a42c: Unable to load Client Side Monitoring configurations from system properties variables!
20:31:11.998 [java-sdk-http-connection-reaper] DEBUG org.apache.http.impl.conn.PoolingHttpClientConnectionManager - Closing connections idle longer than 60000 MILLISECONDS
20:31:11.999 [main] DEBUG com.amazonaws.monitoring.CsmConfigurationProviderChain - Unable to load configuration from com.amazonaws.monitoring.ProfileCsmConfigurationProvider@43df23d3: Unable to load config file
20:31:12.025 [main] WARN com.amazonaws.internal.InstanceMetadataServiceResourceFetcher - Fail to retrieve token 
com.amazonaws.SdkClientException: Failed to connect to service endpoint: 
	at com.amazonaws.internal.EC2ResourceFetcher.doReadResource(EC2ResourceFetcher.java:100)
	at com.amazonaws.internal.InstanceMetadataServiceResourceFetcher.getToken(InstanceMetadataServiceResourceFetcher.java:91)
	at com.amazonaws.internal.InstanceMetadataServiceResourceFetcher.readResource(InstanceMetadataServiceResourceFetcher.java:69)
	at com.amazonaws.internal.EC2ResourceFetcher.readResource(EC2ResourceFetcher.java:62)
	at com.amazonaws.util.EC2MetadataUtils.getItems(EC2MetadataUtils.java:400)
	at com.amazonaws.util.EC2MetadataUtils.getData(EC2MetadataUtils.java:369)
	at com.amazonaws.util.EC2MetadataUtils.getData(EC2MetadataUtils.java:365)
	at com.amazonaws.util.EC2MetadataUtils.getEC2InstanceRegion(EC2MetadataUtils.java:280)
	at com.amazonaws.regions.InstanceMetadataRegionProvider.tryDetectRegion(InstanceMetadataRegionProvider.java:59)
	at com.amazonaws.regions.InstanceMetadataRegionProvider.getRegion(InstanceMetadataRegionProvider.java:50)
	at com.amazonaws.regions.AwsRegionProviderChain.getRegion(AwsRegionProviderChain.java:46)
	at com.amazonaws.client.builder.AwsClientBuilder.determineRegionFromRegionProvider(AwsClientBuilder.java:475)
	at com.amazonaws.client.builder.AwsClientBuilder.setRegion(AwsClientBuilder.java:458)
	at com.amazonaws.client.builder.AwsClientBuilder.configureMutableProperties(AwsClientBuilder.java:424)
	at com.amazonaws.client.builder.AwsSyncClientBuilder.build(AwsSyncClientBuilder.java:46)
	at sample.AwsBatchSample.awsBatchJobSend(AwsBatchSample.java:17)
	at sample.AwsBatchSample.main(AwsBatchSample.java:12)
Caused by: java.net.SocketException: Network is unreachable: connect
	at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method)
	at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:85)
	at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
	at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
	at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
	at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
	at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
	at java.net.Socket.connect(Socket.java:589)
	at sun.net.NetworkClient.doConnect(NetworkClient.java:175)
	at sun.net.www.http.HttpClient.openServer(HttpClient.java:463)
	at sun.net.www.http.HttpClient.openServer(HttpClient.java:558)
	at sun.net.www.http.HttpClient.<init>(HttpClient.java:242)
	at sun.net.www.http.HttpClient.New(HttpClient.java:339)
	at sun.net.www.http.HttpClient.New(HttpClient.java:357)
	at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(HttpURLConnection.java:1202)
	at sun.net.www.protocol.http.HttpURLConnection.plainConnect0(HttpURLConnection.java:1181)
	at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:1032)
	at sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:966)
	at com.amazonaws.internal.ConnectionUtils.connectToEndpoint(ConnectionUtils.java:52)
	at com.amazonaws.internal.EC2ResourceFetcher.doReadResource(EC2ResourceFetcher.java:80)
	... 16 common frames omitted
20:31:12.025 [main] WARN com.amazonaws.util.EC2MetadataUtils - Unable to retrieve the requested metadata (/latest/dynamic/instance-identity/document). Failed to connect to service endpoint: 
com.amazonaws.SdkClientException: Failed to connect to service endpoint: 
	at com.amazonaws.internal.EC2ResourceFetcher.doReadResource(EC2ResourceFetcher.java:100)
	at com.amazonaws.internal.InstanceMetadataServiceResourceFetcher.getToken(InstanceMetadataServiceResourceFetcher.java:91)
	at com.amazonaws.internal.InstanceMetadataServiceResourceFetcher.readResource(InstanceMetadataServiceResourceFetcher.java:69)
	at com.amazonaws.internal.EC2ResourceFetcher.readResource(EC2ResourceFetcher.java:62)
	at com.amazonaws.util.EC2MetadataUtils.getItems(EC2MetadataUtils.java:400)
	at com.amazonaws.util.EC2MetadataUtils.getData(EC2MetadataUtils.java:369)
	at com.amazonaws.util.EC2MetadataUtils.getData(EC2MetadataUtils.java:365)
	at com.amazonaws.util.EC2MetadataUtils.getEC2InstanceRegion(EC2MetadataUtils.java:280)
	at com.amazonaws.regions.InstanceMetadataRegionProvider.tryDetectRegion(InstanceMetadataRegionProvider.java:59)
	at com.amazonaws.regions.InstanceMetadataRegionProvider.getRegion(InstanceMetadataRegionProvider.java:50)
	at com.amazonaws.regions.AwsRegionProviderChain.getRegion(AwsRegionProviderChain.java:46)
	at com.amazonaws.client.builder.AwsClientBuilder.determineRegionFromRegionProvider(AwsClientBuilder.java:475)
	at com.amazonaws.client.builder.AwsClientBuilder.setRegion(AwsClientBuilder.java:458)
	at com.amazonaws.client.builder.AwsClientBuilder.configureMutableProperties(AwsClientBuilder.java:424)
	at com.amazonaws.client.builder.AwsSyncClientBuilder.build(AwsSyncClientBuilder.java:46)
	at sample.AwsBatchSample.awsBatchJobSend(AwsBatchSample.java:17)
	at sample.AwsBatchSample.main(AwsBatchSample.java:12)
Caused by: java.net.SocketException: Network is unreachable: connect
	at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method)
	at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:85)
	at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
	at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
	at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
	at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
	at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
	at java.net.Socket.connect(Socket.java:589)
	at sun.net.NetworkClient.doConnect(NetworkClient.java:175)
	at sun.net.www.http.HttpClient.openServer(HttpClient.java:463)
	at sun.net.www.http.HttpClient.openServer(HttpClient.java:558)
	at sun.net.www.http.HttpClient.<init>(HttpClient.java:242)
	at sun.net.www.http.HttpClient.New(HttpClient.java:339)
	at sun.net.www.http.HttpClient.New(HttpClient.java:357)
	at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(HttpURLConnection.java:1202)
	at sun.net.www.protocol.http.HttpURLConnection.plainConnect0(HttpURLConnection.java:1181)
	at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:1032)
	at sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:966)
	at com.amazonaws.internal.ConnectionUtils.connectToEndpoint(ConnectionUtils.java:52)
	at com.amazonaws.internal.EC2ResourceFetcher.doReadResource(EC2ResourceFetcher.java:80)
	... 16 common frames omitted
com.amazonaws.SdkClientException: Unable to find a region via the region provider chain. Must provide an explicit region in the builder or setup environment to supply a region.

Roughly speaking, that's all for this time.

reference

https://docs.amazonaws.cn/AWSJavaSDK/latest/javadoc/com/amazonaws/services/batch/AWSBatchClient.html#submitJob-com.amazonaws.services.batch.model.SubmitJobRequest- https://docs.amazonaws.cn/AWSJavaSDK/latest/javadoc/com/amazonaws/services/batch/model/SubmitJobRequest.html https://github.com/aws/aws-sdk-java/issues/2062 https://mvnrepository.com/artifact/com.amazonaws/aws-java-sdk-batch

Recommended Posts

Submit a job to AWS Batch with Java (Eclipse)
How to run a job with docker login in AWS batch
How to use Java framework with AWS Lambda! ??
I tried to break a block with java (1)
Until you run a Java program with the AWS SDK local to Windows
How to jump from Eclipse Java to a SQL file
How to deploy Java to AWS Lambda with Serverless Framework
[Java] How to start a new line with StringBuilder
Run batch with docker-compose with Java batch
Java to play with Function
Connect to DB with Java
Connect to MySQL 8 with Java
Connect to oracle with eclipse!
I tried to create a java8 development environment with Chocolatey
Java program to resize a photo into a square with margins
I tried to modernize a Java EE application with OpenShift.
[Beginner] Try to make a simple RPG game with Java ①
I want to make a list with kotlin and java!
I want to make a function with kotlin and java!
Create a SlackBot with AWS lambda & API Gateway in Java
How to deploy a kotlin (java) app on AWS fargate
Even in Java, I want to output true with a == 1 && a == 2 && a == 3
Try developing a containerized Java web application with Eclipse + Codewind
Try Spark Submit to EMR using AWS SDK for Java
A memo to start Java programming with VS Code (2020-04 version)
A story that I struggled to challenge a competition professional with Java
Build a Java project with Gradle
Java to learn with ramen [Part 1]
[Java] Points to note with Arrays.asList ()
How to make a Java container
How to implement a job that uses Java API in JobScheduler
Dare to challenge Kaggle with Java (1)
Run a batch file from Java
I tried to interact with Java
Connecting to a database with Java (Part 1) Maybe the basic method
Things to consider when running a specified job using Spring Batch
Create a Java project using Eclipse
[Java] How to create a folder
Replace with a value according to the match with a Java regular expression
Using Java with AWS Lambda-Eclipse Preparation
Java, arrays to start with beginners
java Eclipse How to debug javaScript
How to make a Java array
A standalone Java app that sends logs to CloudWatch Logs with slf4j / logback
Get a list of S3 files with ListObjectsV2Request (AWS SDK for Java)
[Personal memo] How to interact with a random number generator in Java
[Java] How to turn a two-dimensional array with an extended for statement
Even in Java, I want to output true with a == 1 && a == 2 && a == 3 (PowerMockito edition)
I want to create a dark web SNS with Jakarta EE 8 with Java 11
Hanashi stumbled a little on path trying to study Java with VScode
[Photo Organizer] [java] [windows] Move selected photos to a folder with 2 clicks
[Java] Deploy a web application created with Eclipse + Maven + Ontology on Heroku
I want to ForEach an array with a Lambda expression in Java
How to make an app with a plugin mechanism [C # and Java]
How to start a Docker container with a volume mounted in a batch file
How to make a Java calendar Summary
A memorandum on how to use Eclipse
Using Java with AWS Lambda-Implementation-Check CloudWatch Arguments
How to compile Java with VsCode & Ant
[Java] How to compare with equals method
Using Java with AWS Lambda-Implementation-Stop / Launch EC2