How do I pass parameters from a scheduled trigger in EventBridge to an AWS Batch job?

Last updated: 2022-01-20

I want to pass parameters from a scheduled trigger in Amazon EventBridge to an AWS Batch job.

Short description

In AWS Batch, your parameters are placeholders for the variables that you define in the command section of your AWS Batch job definition. These placeholders allow you to:

  • Use the same job definition for multiple jobs that use the same format.
  • Programmatically change values in the command at submission time.

It's a best practice to define your parameter as a key-value pair. For example:

"Parameters" : {"test" : "abc"}

If you register a job definition or submit a job, then use parameter substitution placeholders in the command field of the job's container properties. For example:

"Command" : [ "echo” "Ref::test" ]

When you submit the preceding job, the Ref::test argument in the container's command is replaced with the default value (abc).

You can define a different parameter value for the same parameter key when you submit a job. For example:

"Parameters" : {"test" : "hello"}

When you submit the preceding job, the Ref::test argument in the container’s command is replaced with the custom value (hello) that you defined during job submission.

Resolution

Set up your AWS Batch environment

1.    Create a compute environment.

2.    Create a job queue, and then associate your job queue with the compute environment that you created in step 1.

3.    Create a job definition with an image (For example: nginx).

Create an EventsBridge rule

1.    Open the EventBridge console.

2.    Select Create rule.

3.    Enter a Name for your rule. You can optionally enter a Description.

4.    In Define pattern, select Event pattern or Schedule, based on your use case.

5.    In Select event bus, select the default option of AWS default event bus.

6.    In Select targets, choose Batch job queue from the Target dropdown list.

7.    For Job queue, enter the ARN of the job queue that you created earlier.

8.    For Job definition, enter the name of the job definition that you created earlier.

9.    For Job name, enter a name for your job.

10.    Expand the Configure input section, and select Constant (JSON text).

11.    In the text box that appears, enter the following:

{"Parameters": {"name":"test"}, "ContainerOverrides": { "Command": ["echo","Ref::name"] } }

The rule submits an AWS Batch job when EventBridge invokes the rule. If the job is successful, your CloudWatch logs print "test" at the following locations:

Log Group: /aws/batch/job
Log Stream: yourJobDefinitionName/default/your-ecs-task-ID

Important: You must use camel case for JSON text in your EventBridge rules.

12.    Select Create.