I have a terraform project where I’m trying to set up 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.
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.
You can have below terraform code to provide input parameters for aws cloudwatch . Below I have provided the Template to use parameter
resource "aws_cloudwatch_event_rule" "cron_rule" { name = "my-cron-rule" description = "Schedule a Batch job" schedule_expression = "cron(0 8 ? * 1-5 *)" # Run daily at 8 AM on weekdays targets = [{ id = "my-target" arn = aws_batch_job_definition.my_job_definition.arn input = jsonencode({ parameters = { command = "your_desired_command" } }) input_path = "$.detail" }] } resource "aws_batch_job_definition" "my_job_definition" { type = "container" compute_environment = "my-compute-environment" container_properties = { image = "your_container_image" command = ["/bin/bash", "-c", "your_command"] environment = [{ name = "MY_PARAMETER" value = "your_default_value" }] } }
Also for the problem that is stated for that code you can add below code for using parameters.
resource "aws_batch_job_definition" "test" { type = "container" compute_environment = "my-compute-environment" container_properties = { image = "your_container_image" command = ["/bin/bash", "-c", "env | grep MY_COMMAND"] # Access environment variable environment = [{ name = "MY_COMMAND" value = "commandToRun" # Set command as environment variable }] } } resource "aws_cloudwatch_event_target" "test_target" { # ... rest of the configuration remains the same ... input = jsonencode({ Parameters = { # Reference the environment variable name resourceRequirements = {"resourceRequirements": [ ... ]} } }) }