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

如何在API Gateway HTTP类型中修改AWS StepFunction的返回响应

问题

我正在搭建HTTP类型API Gateway → Step Function → Lambda的架构,整体功能正常,但API返回的响应里带了一堆多余字段,比如计费信息、执行ARN这些,完全没用。当前返回的响应长这样:

{
  "billingDetails": {
    "billedDurationInMilliseconds": 100,
    "billedMemoryUsedInMB": 64
  },
  "executionArn": "arn:aws:states:us-west-2:***:express:ExpressStateMachine:********",
  "input": "{}",
  "inputDetails": {
    "__type": "com.amazonaws.swf.base.model#CloudWatchEventsExecutionDataDetails",
    "included": true
  },
  "name": "*********",
  "output": "{\"billedDurationInMilliseconds\":100,\"billedMemoryUsedInMB\":64}",
  "outputDetails": {
    "__type": "com.amazonaws.swf.base.model#CloudWatchEventsExecutionDataDetails",
    "included": true
  },
  "startDate": 1.667486785389E9,
  "stateMachineArn": "arn:aws:states:us-west-2:***:stateMachine:ExpressStateMachinePartnerMock",
  "status": "SUCCEEDED",
  "stopDate": 1.667486785466E9
}

我只想要响应体里保留output对应的JSON内容,而且必须用HTTP类型的API Gateway,不能换成REST的。之前想过用Lambda中转Step Function,但Step Function有好几个阶段,这么做太麻烦;试了API Gateway的响应映射,但好像只能改响应头和状态码,想问下到底能不能通过配置API Gateway来替换掉Step Function返回的响应体?

解决方案

完全可以,用HTTP API Gateway的**响应模板(Response Templates)**就能实现,具体步骤如下:

  • 打开HTTP API的控制台,找到和Step Function关联的集成配置
  • 进入集成的「响应」设置,给200 OK状态码(对应Step Function执行成功的场景)添加或编辑响应模板
  • 模板类型选application/json,输入下面的VTL代码直接提取output字段:
#set($output = $input.path('$.output'))
$output

如果你的output是转义后的JSON字符串(就像示例里那样),想让API直接返回解析好的JSON对象,就用这段代码:

#set($outputJson = $util.parseJson($input.path('$.output')))
$util.toJson($outputJson)
  • 保存配置后重新测试API,响应体就只会保留你需要的output内容了

补充说明:HTTP API的响应模板并非只能修改头和状态码,之前没生效应该是模板内容配置不对,VTL可以直接操作响应数据结构。

内容的提问来源于stack exchange,提问作者J Mas

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.14 14:15:40