12

Consider piece of AWS batch job definition:

    MyJobDefinition:
        Type: "AWS::Batch::JobDefinition"
        Properties:
            Type: container
            Parameters: {}
            JobDefinitionName: "my-job-name"
            ContainerProperties:
                Command:
                    - "java"
                    - "-jar"
                    - "my-application-SNAPSHOT.jar"
                    - "--param1"
                    - "Ref::param1"
                    - "--param2"
                    - "Ref::param2"

Which result to call:

java -jar my-application-SNAPSHOT.jar --param1 someValue1 --param2 someValue2

How do I change job definition to make it like this? (notice the = sign):

java -jar my-application-SNAPSHOT.jar --param1=someValue1 --param2=someValue2

Please note that Ref::param1 is not cloudformation template params, but aws batch job params.

Maria Ines Parnisari
  • 16,584
  • 9
  • 85
  • 130
Cherry
  • 31,309
  • 66
  • 224
  • 364
  • It is worth mentioning param1 and param2 in the Paremeters json field, so that it is clear, that those do not come from template params. – pubsy Feb 19 '19 at 22:15
  • Why don't you pass values as "java -jar my-application-SNAPSHOT.jar someValue1 someValue2"? – Fabio Manzano Feb 22 '19 at 18:44
  • Did you end up refactoring the code and passing in the arguments directly instead of using application.properties? – Urmzd Oct 11 '22 at 20:05

2 Answers2

0

In Java, you can pass environment variables using the -D option when running your application from the command line.Replace param1 with the name of your environment variable, and param1_value with the actual value you want to assign to it.

  MyJobDefinition:
            Type: "AWS::Batch::JobDefinition"
            Properties:
                Type: container
                Parameters: {}
       
    
         JobDefinitionName: "my-job-name"
            ContainerProperties:
                Command:
                    - "java"
                    - "-Dparam1=${param1_value}"
                    - "-Dparam2={param2_value}"
                    - "-Dparam3={param2_value}"
                    - "-jar"
                    - "my-application-SNAPSHOT.jar"

For example, if you have an environment variable named DATABASE_URL and you want to pass its value to your Java application, you can use:

java -DDATABASE_URL=jdbc:mysql://localhost:3306/dbname -jar YourApplication.jar

In your Java application, you can access the environment variable value using the System.getProperty() method:

String databaseUrl = System.getProperty("DATABASE_URL");

The databaseUrl variable will contain the value passed from the command line. Hope this helps.

Aastha Jain
  • 180
  • 1
  • 1
  • 13
-2

As I understand, AWS batch parameters are substituted by looking for the Ref:: prefix. I could find only one thread where they tried to use a parameter in a larger string and it works.

Given that, the following should work

MyJobDefinition:
    Type: "AWS::Batch::JobDefinition"
    Properties:
        Type: container
        Parameters: {}
        JobDefinitionName: "my-job-name"
        ContainerProperties:
            Command:
                - "java"
                - "-jar"
                - "my-application-SNAPSHOT.jar"
                - "--param1=Ref::param1"
                - "--param2=Ref::param2"
Manish Dash
  • 2,004
  • 10
  • 20
  • 4
    This is wrong. Batch does not support substitution inside strings. Only for complete arguments. – Gil May 28 '19 at 11:26