The issue you’re facing arises because the `input` property of the `aws_cloudwatch_event_target` resource passes the data to the Batch job as a JSON string. However, your Batch job definition expects the `inputCommand` to be an environment variable or command argument rather than a direct JSON input.

To solve this, you can use the `input_transformer` property of the `aws_cloudwatch_event_target` resource to transform the CloudWatch event input into a format that can be correctly interpreted by the Batch job’s `command` field.

Here’s how you can modify your Terraform configuration:

1. Update the Job Definition:

– Ensure your job definition is set up to take the `inputCommand` as part of the command in a format compatible with what will be passed through the `input_transformer`.

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

2. Use `input_transformer` in `aws_cloudwatch_event_target`:

– Use the `input_transformer` property to map the `inputCommand` from the CloudWatch event to the Batch job.

hcl
    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_transformer {
        input_paths {
          command = "$.inputCommand"
        }

        input_template = "{\"inputCommand\": }"
      }
    }
   

3. Modify the Input JSON:

– Modify the input to the CloudWatch event so that the `inputCommand` is passed correctly:

{
"inputCommand": "commandToRun"
}

Example of How it Works:

– The `input_paths` block maps the CloudWatch event’s JSON key (`inputCommand`) to a variable (`command`).
– The `input_template` block then formats the input as a JSON string that the Batch job can parse.
– This formatted input will be passed to the job, where `Ref::inputCommand` will resolve to `”commandToRun”`.

Full Terraform Configuration

hcl
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_transformer {
    input_paths {
      command = "$.inputCommand"
    }

    input_template = "{\"inputCommand\": }"
  }
}

In this configuration:

`$.inputCommand` refers to the path in the JSON object passed by the CloudWatch Event.
– `` is used in the `input_template` to create a JSON string that your job definition expects.

This setup should correctly pass the `inputCommand` to your AWS Batch job when triggered by the CloudWatch Event rule.

Support On Demand!

Cloud