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

Last updated: 2020-05-15

I want to pass parameters from a scheduled trigger in Amazon CloudWatch 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, 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 Amazon CloudWatch Events rule

1.    Open the CloudWatch console.

2.    In the navigation pane, choose Events.

3.    Choose Get started.

4.    In the Event Source section, select either Event Pattern or Schedule based on your needs.

5.    For Targets, choose Add target.

6.    From the list of targets, select Batch job queue.

7.    For Job queue, enter the name 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 CloudWatch 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 CloudWatch Events rules.

12.    Choose Configure details, and then complete the rest of the steps in the setup wizard to create your rule.


Did this article help you?

Anything we could improve?


Need more help?