1
votes

I have a terraform project where I'm trying to setup a cloudwatch event rule and target to trigger a new aws batch job submission on a schedule. The issue I'm having is passing a static parameter (ie, a variable representing a command to run) from the cloudwatch event to the batch_target.

In my aws_batch_job_definition I have the following as part of the container_properties:

    container_properties = <<CONTAINER_PROPERTIES
{
    "command": ["echo", "command", "Ref::inputCommand"],
...etc
}

And my cloudwatch event target tied to the schedule rule looks like this:

resource "aws_cloudwatch_event_target" "test_target" {
  rule = aws_cloudwatch_event_rule.every_minute.name
  role_arn = aws_iam_role.event_iam_role.arn
  arn = aws_batch_job_queue.test_queue.arn
  batch_target {
      job_definition = aws_batch_job_definition.test.arn
      job_name = "job-test"
      job_attempts = 2
  }
  input = "{\"inputCommand\": \"commandToRun\"}" #this line does not work as intended
}

Is there a simple way to use the input or input_transformer properties for the event_target to pass through the variable inputCommand to the batch job?

The setup works when I submit a job with that parameter and value set through the console, or set a default parameter in the job definition, but I'm having trouble doing it via the cloudwatch event in terraform.

1

1 Answers

2
votes

I had a similar issue, but with CloudFormation template.

This docs helped me a lot: https://docs.aws.amazon.com/batch/latest/userguide/batch-cwe-target.html

In your case, I think the solution might be:

  input = "{\"Parameters\" : "{\"inputCommand\": \"commandToRun\"}}"

My working CloudFormation template looks something like this:

JobDefinition:
  Type: AWS::Batch::JobDefinition
  Properties:
    ...
    ContainerProperties:
      ...
      Image:...
      Command:
        - 'Ref::MyParameter'

ScheduledRule:
  Type: AWS::Events::Rule
  Properties:
    ...
    Targets:
      - ...
        BatchParameters:
          ...
          Input: "{\"Parameters\" : {\"MyParameter\": \"SomeValue\"}}"

Hope this helps!