You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

AWS EventBridge Pipe:SQS转EventBus输入模板执行失败问题

问题:EventBridge Pipe配置inputTemplate执行失败,无法将SQS消息体作为EventBus事件detail

配置了一条SQS到EventBus的EventBridge Pipe,期望将SQS消息的JSON消息体直接作为EventBus事件的detail,但设置inputTemplate: <$.body>时管道执行失败;省略inputTemplate虽能运行,但会把整个SQS消息(包含元数据)放入detail,不符合需求;尝试{Payload: <$.body>}也无效。

CDK代码如下:

new pipes.CfnPipe(this, "pipe", {
  roleArn: pipeRole.roleArn,
  source: queue.queueArn,
  target: eventBus.eventBusArn,
  targetParameters: {
    eventBridgeEventBusParameters: {
      detailType: "FbaNotifcation",
      source: serviceName,
    },
    // 省略inputTemplate会将整个SQS消息(含元数据)放入detail中。
    inputTemplate: `<$.body>`,
  },
});

原因及解决方法

问题核心在于EventBridge Pipe的inputTemplate要求输出合法的JSON结构,直接使用<$.body>会导致输出不符合JSON规范(尤其是当body本身是JSON对象时),或者未正确解析字符串形式的JSON body。

方法1:用inputPathsMap映射后引用

通过inputPathsMap先提取body内容,再在inputTemplate中引用映射后的键,确保输出是合法的JSON:

new pipes.CfnPipe(this, "pipe", {
  roleArn: pipeRole.roleArn,
  source: queue.queueArn,
  target: eventBus.eventBusArn,
  targetParameters: {
    eventBridgeEventBusParameters: {
      detailType: "FbaNotifcation",
      source: serviceName,
    },
    inputPathsMap: {
      "sqsBody": "$.body"
    },
    inputTemplate: "<sqsBody>"
  },
});

方法2:解析字符串形式的JSON body

如果SQS消息的body是序列化后的JSON字符串(而非直接的JSON对象),需要用JSONPath的parse函数先解析成对象:

new pipes.CfnPipe(this, "pipe", {
  roleArn: pipeRole.roleArn,
  source: queue.queueArn,
  target: eventBus.eventBusArn,
  targetParameters: {
    eventBridgeEventBusParameters: {
      detailType: "FbaNotifcation",
      source: serviceName,
    },
    inputTemplate: "<parse($.body)>"
  },
});

关键说明

  • 若SQS消息body本身是JSON对象,直接用<$.body>会导致输出不是完整的JSON顶层结构,通过inputPathsMap映射可规避这个问题。
  • 若body是JSON字符串,必须用parse函数解析,否则EventBus会收到字符串类型的detail,无法作为JSON对象使用。

内容的提问来源于stack exchange,提问作者florian norbert bepunkt

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.21 05:35:14