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

如何在AWS Step Functions的Pass状态间传递参数?

在AWS Step Functions Pass状态间传递参数的问题

问题场景

在AWS Step Functions中尝试从一个Pass状态向另一个Pass状态传递参数时,aws_stepfunctions.JsonPath.string_at在TaskInput.from_object(调用Lambda时)可正常解析,但在Pass状态的Result.from_object中无法生效,直接输出JsonPath字符串而非实际参数值。

示例代码

initial_pass = aws_stepfunctions.Pass(
    self,
    "initial_pass",
    result=aws_stepfunctions.Result.from_object(
         {
            "iterator": {"count": 5, "index": 0, "step": 1, "continue": True},
            "globals": {
                "start_datetime": "2023-01-01",
                 "end_datetime": "",
                 "upload_start_datetime": "",
                 "upload_end_datetime": "",
                 "device_details": {},
            },
         }
   )
)

second_pass = aws_stepfunctions.Pass(
    self,
    "second_pass",
    result=aws_stepfunctions.Result.from_object(
       {
           "iterator": {"count": 5, "index": 0, "step": 1, "continue": True},
           "globals": aws_stepfunctions.JsonPath.string_at("$.globals"),
       }
    ),
)

错误输出

{
  "iterator": {
    "count": 5,
    "index": 0,
    "step": 1,
    "continue": true
  },
  "globals": "$.globals"
}

问题原因

Result.from_object的作用是传入静态对象作为Pass状态的结果,它不会解析对象中的JsonPath表达式,因此JsonPath.string_at("$.globals")被当作普通字符串直接返回。而TaskInput.from_object是专门为任务输入设计的,会自动识别并解析其中的JsonPath引用。

解决方法

要在Pass状态中构造包含输入参数的输出,需使用parameters属性(支持JsonPath解析),并通过result_path控制输出结构:

修改后的代码

second_pass = aws_stepfunctions.Pass(
    self,
    "second_pass",
    parameters={
        "iterator": {"count": 5, "index": 0, "step": 1, "continue": True},
        "globals": aws_stepfunctions.JsonPath.string_at("$.globals"),
    },
    result_path="$",  # 将parameters构造的内容替换为整个输出
)

其他场景说明

  • 如果只想将输入中某个路径的内容直接作为Pass状态的结果,可以使用Result.from_json_path:
    pass_globals = aws_stepfunctions.Pass(
        self,
        "PassGlobals",
        result=aws_stepfunctions.Result.from_json_path("$.globals"),
    )
    
  • 如果需要保留原始输入的同时添加新内容,可调整result_path,例如result_path="$.processed_data",此时输出会包含原始输入字段,加上processed_data字段存储构造的内容。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.03 02:50:42