AWS Batch input parameter from Cloudwatch through Terraform

Viewed 1240

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.

2 Answers

I had a similar issue, but with CloudFormation template.

This docs helped me a lot.

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\"}}"        

You can specify the command through the input section of your event_target. Your terraform could look like this ( and I included another parameter, resourceRequirements, just as an example ):

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 = "{\"Parameters\" : {\"command\": \"commandToRun\", \"resourceRequirements\": {\"resourceRequirements\": [ {\"type\": \"MEMORY\",\"value\": \"500\" }, {\"type\": \"VCPU\",\"value\": \"3\" }]}}}"
}

More info on the options that can be passed can be found here, https://docs.aws.amazon.com/batch/latest/userguide/batch-cwe-target.html about halfway down the page under Passing Event Information to an AWS Batch Target using the EventBridge Input Transformer

Related