Terraform配置AWS EventBridge Input Transformer不生效问题求助
问题分析与解决
你的问题出在input_transformer的input_template配置上——你用了jsonencode()包裹模板对象,导致EventBridge无法识别占位符<detailType>,直接把转义后的字符串发送到SQS,而非执行转换逻辑。
修正后的配置
移除input_template中的jsonencode(),直接提供JSON格式的模板字符串即可:
resource "aws_cloudwatch_event_target" "developer-notification" { rule = aws_cloudwatch_event_rule.notification.name target_id = "SendToSQS" arn = aws_sqs_queue.test-queue.arn input_transformer { input_paths = { "detailType" = "$.detail-type" } # 直接编写模板字符串,无需jsonencode处理 input_template = <<EOF { "detailType": "<detailType>" } EOF } }
也可以使用单行字符串形式:
input_template = "{\"detailType\": \"<detailType>\"}"
原因说明
当你用jsonencode()处理模板对象时,Terraform会将整个对象转换成转义后的JSON字符串(比如"{\"detailType\": \"<detailType>\"}")。EventBridge会把这个转义字符串当作原始内容发送,无法识别其中的<detailType>占位符进行字段替换。
正确的做法是直接提供未转义的JSON模板,让EventBridge能够识别占位符,并替换为input_paths中指定的对应字段值。
额外验证点
- 确认EventBridge规则的事件模式确实匹配你测试的事件,避免因事件不匹配导致其他规则的原始消息干扰测试结果
- 你的SQS队列策略已正确授权EventBridge发送消息,这部分配置无需调整
内容的提问来源于stack exchange,提问作者Mcbeer
相关产品推荐
相关产品推荐

