Terraform-provider-aws: aws_cloudwatch_event_target input_transformer input_template should accept string values

Created on 23 Jan 2019  路  7Comments  路  Source: hashicorp/terraform-provider-aws

Community Note

  • Please vote on this issue by adding a 馃憤 reaction to the original issue to help the community and maintainers prioritize this request
  • Please do not leave "+1" or "me too" comments, they generate extra noise for issue followers and do not help prioritize the request
  • If you are interested in working on this issue or have submitted a pull request, please leave a comment

Terraform Version

Terraform v0.11.11
terraform-provider-aws: 1.56

Affected Resource(s)

  • aws_cloudwatch_event_target

Terraform Configuration Files

provider "aws" {
  region              = "us-east-1"
}

resource "aws_cloudwatch_event_target" "sns" {
  rule       = "${aws_cloudwatch_event_rule.patch_compliance.name}"
  target_id  = "cwtarget-patch-compliance-sns"
  arn        = "${aws_sns_topic.patch_compliance.arn}"
  input_transformer = {
    input_paths = { "instance_id"="$.detail.resource-id", "state"="$.detail.compliance-status", "severity"="$.detail.severity", "baseline"="$.detail.patch-baseline-id" }

    input_template = "EC2 Patch Notification: instance <instance_id> <state> for <severity> <baseline>"
  }
}

### Expected Behavior

It should create the resource. Input Template is designed for templated strings, not just JSON, as can be seen at https://docs.aws.amazon.com/AmazonCloudWatchEvents/latest/APIReference/API_InputTransformer.html

Actual Behavior

  • module.prod_global.module.patch_baselines.aws_cloudwatch_event_target.sns: 1 error(s) occurred:

  • aws_cloudwatch_event_target.sns: Updating CloudWatch Event Target failed: ValidationException: Invalid InputTemplate for target cwtarget-patch-compliance-sns : [Source: (String)"EC2 Patch Notification: instance"; line: 1, column: 4].

Steps to Reproduce

  • Change input_template in the HCL above to
    input_template = <<INPUT_TEMPLATE_EOF
    {
        "instance_id":<instance_id>, 
        "state": <state>, 
        "severity": <severity>,
        "baseline": <baseline>
    }
    INPUT_TEMPLATE_EOF
  • terraform apply
  • Change input_template in the HCL above to "EC2 Patch Notification: instance for "
  • terraform apply

References

https://docs.aws.amazon.com/AmazonCloudWatchEvents/latest/APIReference/API_InputTransformer.html
https://docs.aws.amazon.com/AmazonCloudWatch/latest/events/CloudWatch-Events-Input-Transformer-Tutorial.html

question serviccloudwatchevents serviceventbridge

Most helpful comment

Maybe I am missing something but most probably this is not a bug. To be honest I had issues adding the target through the AWS console too. Then I noticed that in the AWS console you have to include the quotes around your string too. So, the solution is to put your string in double-quotes, i.e.,
"\"This is message <placeholder1>\""
and it works.

In the case of the issue @shederman had the following works:

provider "aws" {
  region              = "us-east-1"
}

resource "aws_cloudwatch_event_target" "sns" {
  rule       = "${aws_cloudwatch_event_rule.patch_compliance.name}"
  target_id  = "cwtarget-patch-compliance-sns"
  arn        = "${aws_sns_topic.patch_compliance.arn}"
  input_transformer = {
    input_paths = { "instance_id"="$.detail.resource-id", "state"="$.detail.compliance-status", "severity"="$.detail.severity", "baseline"="$.detail.patch-baseline-id" }

    input_template = "\"EC2 Patch Notification: instance <instance_id> <state> for <severity> <baseline>\""
  }
}

All 7 comments

This isnt working for me,
im getting unsupported argument when using input_transfomer in terraform.
i followed the above steps but i couldn't find why it is giving unsupported arguments.
It will be helpful if i get an quick solution on this.
im using terraform 2.32.0

I'm running into the same, unable to create any sort of template string which will work.

I've tried:

  • Defining a input template as a string with variables to be interpolated
  • Defining the input template with jsonencode()
  • Referencing a input template from a file using file()

All of them break down at some point between encoding issues (see #7474) or seemingly not passing things through to the AWS API as expected. Quite frustrating.

I've also ran into this annoying issue, with these unexpected transforms into null.

I dug into the test cases and found that the test is using a output template that's a JSON string, and that somehow works: https://github.com/terraform-providers/terraform-provider-aws/blob/710dacf6d764e93023f440507182980c5ee09183/aws/resource_aws_cloudwatch_event_target_test.go#L1100-L1119

If I make any updates to the Template such that it's not a JSON string, all placeholders get converted back to null. I'm going to try to use this as an ugly workaround now (i.e., convert the ugly raw JSON into a slightly cleaner JSON output)

Maybe I am missing something but most probably this is not a bug. To be honest I had issues adding the target through the AWS console too. Then I noticed that in the AWS console you have to include the quotes around your string too. So, the solution is to put your string in double-quotes, i.e.,
"\"This is message <placeholder1>\""
and it works.

In the case of the issue @shederman had the following works:

provider "aws" {
  region              = "us-east-1"
}

resource "aws_cloudwatch_event_target" "sns" {
  rule       = "${aws_cloudwatch_event_rule.patch_compliance.name}"
  target_id  = "cwtarget-patch-compliance-sns"
  arn        = "${aws_sns_topic.patch_compliance.arn}"
  input_transformer = {
    input_paths = { "instance_id"="$.detail.resource-id", "state"="$.detail.compliance-status", "severity"="$.detail.severity", "baseline"="$.detail.patch-baseline-id" }

    input_template = "\"EC2 Patch Notification: instance <instance_id> <state> for <severity> <baseline>\""
  }
}

I think above comment resolves the purpose of this issue and it can be closed. Thanks @jorditpuig for telling the exact behaviour

I tried to use the solution above (adding quotes) with heredoc syntax and it results in the same error:

resource "aws_cloudwatch_event_target" "sns" {
  rule       = "${aws_cloudwatch_event_rule.patch_compliance.name}"
  target_id  = "cwtarget-patch-compliance-sns"
  arn        = "${aws_sns_topic.patch_compliance.arn}"
  input_transformer = {
    input_paths = { "instance_id"="$.detail.resource-id", "state"="$.detail.compliance-status", "severity"="$.detail.severity", "baseline"="$.detail.patch-baseline-id" }

    input_template = <<-EOT
    "EC2 Patch Notification: 
     instance <instance_id> <state> for <severity> <baseline>"
    EOT
  }
}

Hi everyone, I've taken a look at this issue. As the AWS documentation states, the values for input_template must be valid JSON. JSON does support a bare string, but to be valid JSON, it needs to include double quotes, as @jorditpuig posted.

Since the JSON string is embedded in a Terraform string, it needs escaping for both JSON and Terraform. So a simple string needs embedded, escaped quotes, e.g. "\"EC2 Patch Notification: instance <instance_id> <state> for <severity> <baseline>\""

JSON does not support multi-line strings, as used in @kenske's example, even though Terraform does. To create a multi-line string, you'll need to use the JSON newline escape \n. Since \ needs to be escaped in Terraform, you will have to use \\n, e.g. "\"EC2 Patch Notification:\\ninstance <instance_id> <state> for <severity> <baseline>\""

I've created the PR #15941 to improve documentation around this field.

Was this page helpful?
0 / 5 - 0 ratings