{"id":11204,"date":"2024-09-13T06:39:13","date_gmt":"2024-09-13T06:39:13","guid":{"rendered":"https:\/\/www.bacancytechnology.com\/qanda\/?p=11204"},"modified":"2024-09-13T06:44:01","modified_gmt":"2024-09-13T06:44:01","slug":"aws-batch-input-parameter-from-cloudwatch-through-terraform","status":"publish","type":"post","link":"https:\/\/www.bacancytechnology.com\/qanda\/cloud\/aws-batch-input-parameter-from-cloudwatch-through-terraform","title":{"rendered":"How To AWS Batch Input Parameter From Cloudwatch Through Terraform"},"content":{"rendered":"<p>The issue you&#8217;re facing arises because the <strong>`input`<\/strong> property of the <strong>`aws_cloudwatch_event_target`<\/strong> resource passes the data to the Batch job as a JSON string. However, your Batch job definition expects the <strong>`inputCommand`<\/strong> to be an environment variable or command argument rather than a direct JSON input.<\/p>\n<p>To solve this, you can use the <strong>`input_transformer`<\/strong> 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&#8217;s `command` field.<\/p>\n<p><strong>Here&#8217;s how you can modify your Terraform configuration:<\/strong><\/p>\n<h3>1. Update the Job Definition:<\/h3>\n<p>&#8211; 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`.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"groovy\">\r\n```hcl\r\n    container_properties = &lt;&lt;CONTAINER_PROPERTIES\r\n    {\r\n        \"command\": [\"echo\", \"Ref::inputCommand\"],\r\n        ...\r\n    }\r\n    CONTAINER_PROPERTIES\r\n    ```<\/pre>\n<h3>2. Use `input_transformer` in `aws_cloudwatch_event_target`:<\/h3>\n<p>    &#8211; Use the `input_transformer` property to map the `inputCommand` from the CloudWatch event to the Batch job.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"groovy\">\r\nhcl\r\n    resource \"aws_cloudwatch_event_target\" \"test_target\" {\r\n      rule     = aws_cloudwatch_event_rule.every_minute.name\r\n      role_arn = aws_iam_role.event_iam_role.arn\r\n      arn      = aws_batch_job_queue.test_queue.arn\r\n\r\n      batch_target {\r\n        job_definition = aws_batch_job_definition.test.arn\r\n        job_name       = \"job-test\"\r\n        job_attempts   = 2\r\n      }\r\n\r\n      input_transformer {\r\n        input_paths {\r\n          command = \"$.inputCommand\"\r\n        }\r\n\r\n        input_template = \"{\\\"inputCommand\\\": <command>}\"\r\n      }\r\n    }\r\n   <\/pre>\n<h3>3. Modify the Input JSON:<\/h3>\n<p>  &#8211; Modify the input to the CloudWatch event so that the `inputCommand` is passed correctly:<br \/>\n<code><br \/>\n    {<br \/>\n      \"inputCommand\": \"commandToRun\"<br \/>\n    }<br \/>\n<\/code><\/p>\n<h2> Example of How it Works:<\/h2>\n<p>&#8211; The <strong>`input_paths`<\/strong> block maps the CloudWatch event&#8217;s JSON key (`inputCommand`) to a variable (`command`).<br \/>\n&#8211; The <strong>`input_template`<\/strong> block then formats the input as a JSON string that the Batch job can parse.<br \/>\n&#8211; This formatted input will be passed to the job, where `Ref::inputCommand` will resolve to `&#8221;commandToRun&#8221;`.<\/p>\n<p><strong> Full Terraform Configuration<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"groovy\">\r\nhcl\r\nresource \"aws_cloudwatch_event_target\" \"test_target\" {\r\n  rule     = aws_cloudwatch_event_rule.every_minute.name\r\n  role_arn = aws_iam_role.event_iam_role.arn\r\n  arn      = aws_batch_job_queue.test_queue.arn\r\n\r\n  batch_target {\r\n    job_definition = aws_batch_job_definition.test.arn\r\n    job_name       = \"job-test\"\r\n    job_attempts   = 2\r\n  }\r\n\r\n  input_transformer {\r\n    input_paths {\r\n      command = \"$.inputCommand\"\r\n    }\r\n\r\n    input_template = \"{\\\"inputCommand\\\": <command>}\"\r\n  }\r\n}\r\n<\/pre>\n<h2>In this configuration:<\/h2>\n<p>&#8211; <strong>`$.inputCommand`<\/strong> refers to the path in the JSON object passed by the CloudWatch Event.<br \/>\n&#8211; `<command>` is used in the `input_template` to create a JSON string that your job definition expects.<\/p>\n<p>This setup should correctly pass the `inputCommand` to your AWS Batch job when triggered by the CloudWatch Event rule.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The issue you&#8217;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 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":11205,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[17],"tags":[],"class_list":["post-11204","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\/11204"}],"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=11204"}],"version-history":[{"count":5,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/11204\/revisions"}],"predecessor-version":[{"id":11210,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/11204\/revisions\/11210"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/media\/11205"}],"wp:attachment":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/media?parent=11204"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/categories?post=11204"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/tags?post=11204"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}