How do I run an AWS Batch job using Java?

4 minute read

I want to run AWS Batch jobs using Java. How do I set that up?


Note: If you receive errors when running AWS Command Line Interface (AWS CLI) commands, make sure that you're using the most recent AWS CLI version.

Prepare your environment

1.    Install Java by following the download instructions on the Oracle website.

2.    Install Eclipse by following the download instructions on the Eclipse Foundation website.

3.    Create an AWS Batch compute environment, job definition, and job queue.

4.    Verify that the job queue is assigned to a valid compute environment by running the following describe-compute-environments AWS CLI command:

Important: Replace your-compute-env-name with your compute environment's name.

$ aws batch describe-compute-environments --compute-environments your-compute-env-name

5.    Verify that the "status" value in the command output is "VALID". If your compute environment isn't valid, make the compute environment valid before continuing.

Note: You can use Java code to create an AWS Batch compute environment, job definition, and job queue. However, you must complete the steps in the Convert the Java project to a Maven project section before creating the resources. For more information, see the sample code from the AWS SDK for Java API Reference.

Install the AWS Toolkit for Eclipse

1.    Open the Eclipse Integrated Development Environment (IDE).

2.    From the Eclipse IDE menu bar, choose Help.

3.    Under Help, choose Eclipse Marketplace.

4.    In Eclipse Marketplace, choose the Search tab.

5.    In the Find search box, enter AWS.

6.    From the search results, choose Install for AWS Toolkit for Eclipse.

7.    From the Eclipse menu bar, choose Navigate.

8.    Choose Preferences. Then, choose Add Access Key ID and Secret Access key.

Create a new Java project

1.    From the Eclipse IDE menu bar, choose File.

2.    Choose New. Then, choose Project.

Convert the Java project to a Maven project

1.    In the Eclipse IDE, right-click the Java project that you created.

2.    Choose Configure. Then, choose Convert to Maven Project. Maven creates a POM.xml file that contains information about the project and the configuration details used to build the project.

3.    Add the required dependencies to the POM.xml file by adding the following code after the closing build tag in the file:

<!-- -->

Important: The code for step 3 works only if you include the code after the closing build tag in the POM.xml file.

Create a Java program to submit AWS Batch jobs

1.    In the Eclipse IDE, choose the project that you created.

2.    Inside the project, right-click the src folder.

3.    Choose New. Then, choose File.

4.    Name the new file

Important: The .java extension name must match with the public class name in the Java program.

5.    Add your AWS Batch environment details to the file by entering the following code into the file:

Important: Replace new-queue with your queue name. Replace us-east-1 with the AWS Region that your environment is in. Replace sleep30:4 with your job definition.

public class BatchClient {
public static void main(String[] args) {
        AWSBatch client = AWSBatchClientBuilder.standard().withRegion("us-east-1").build();
SubmitJobRequest request = new SubmitJobRequest().withJobName("example").withJobQueue("new-queue").withJobDefinition("sleep30:4");
SubmitJobResult response = client.submitJob(request);

6.    To submit the AWS Batch job and run the Java program, choose Run from the Run menu.

Note: If you receive a "SubmitJobResult can not be resolved" error, then you must import the package required for the SubmitJobResult API action. To import the package in the Eclipse IDE, do the following:
In the Java code, select the SubmitJobResult.
Right-click Choose.
Choose Source.
Choose Add import.

AWS OFFICIALUpdated 2 years ago