{"id":11537,"date":"2024-10-22T06:48:46","date_gmt":"2024-10-22T06:48:46","guid":{"rendered":"https:\/\/www.bacancytechnology.com\/qanda\/?p=11537"},"modified":"2024-10-22T06:48:46","modified_gmt":"2024-10-22T06:48:46","slug":"aws-batch-input-parameter-from-cloudwatch-using-terraform","status":"publish","type":"post","link":"https:\/\/www.bacancytechnology.com\/qanda\/cloud\/aws-batch-input-parameter-from-cloudwatch-using-terraform","title":{"rendered":"AWS Batch Input Parameter From Cloudwatch Through Terraform"},"content":{"rendered":"<h3>Problem:<\/h3>\n<p>I have a terraform project where I&#8217;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&#8217;m having is passing a static parameter (ie, a variable representing a command to run) from the cloudwatch event to the batch_target.<br \/>\nIs 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?<\/p>\n<p>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&#8217;m having trouble doing it via the cloudwatch event in terraform.<\/p>\n<h3>Solution:<\/h3>\n<p>You can have below terraform code to provide input parameters for aws cloudwatch . Below I have provided the Template to use parameter<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"dockerfile\">\r\nresource \"aws_cloudwatch_event_rule\" \"cron_rule\" {\r\n  name        = \"my-cron-rule\"\r\n  description = \"Schedule a Batch job\"\r\n  schedule_expression = \"cron(0 8 ? * 1-5 *)\"  # Run daily at 8 AM on weekdays\r\n  targets = [{\r\n    id         = \"my-target\"\r\n    arn        = aws_batch_job_definition.my_job_definition.arn\r\n    input      = jsonencode({\r\n      parameters = {\r\n        command = \"your_desired_command\"\r\n      }\r\n    })\r\n    input_path = \"$.detail\"\r\n  }]\r\n}\r\nresource \"aws_batch_job_definition\" \"my_job_definition\" {\r\n  type          = \"container\"\r\n  compute_environment = \"my-compute-environment\"\r\n  container_properties = {\r\n    image              = \"your_container_image\"\r\n    command            = [\"\/bin\/bash\", \"-c\", \"your_command\"]\r\n    environment         = [{\r\n      name  = \"MY_PARAMETER\"\r\n      value = \"your_default_value\"\r\n    }]\r\n  }\r\n}\r\n<\/pre>\n<p>Also for the problem that is stated for that code you can add below code for using parameters. <\/p>\n<h3>Terraform Code: <\/h3>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"dockerfile\">\r\nresource \"aws_batch_job_definition\" \"test\" {\r\n  type          = \"container\"\r\n  compute_environment = \"my-compute-environment\"\r\n  container_properties = {\r\n    image              = \"your_container_image\"\r\n    command            = [\"\/bin\/bash\", \"-c\", \"env | grep MY_COMMAND\"]  # Access environment variable\r\n    environment         = [{\r\n      name  = \"MY_COMMAND\"\r\n      value = \"commandToRun\"  # Set command as environment variable\r\n    }]\r\n  }\r\n}\r\nresource \"aws_cloudwatch_event_target\" \"test_target\" {\r\n  # ... rest of the configuration remains the same ...\r\n  input = jsonencode({\r\n    Parameters = {\r\n      # Reference the environment variable name\r\n      resourceRequirements = {\"resourceRequirements\": [ ... ]}\r\n    }\r\n  })\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Problem: I have a terraform project where I&#8217;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&#8217;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 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":11538,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[17],"tags":[],"class_list":["post-11537","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-cloud"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/11537"}],"collection":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/comments?post=11537"}],"version-history":[{"count":1,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/11537\/revisions"}],"predecessor-version":[{"id":11539,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/11537\/revisions\/11539"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/media\/11538"}],"wp:attachment":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/media?parent=11537"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/categories?post=11537"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/tags?post=11537"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}