如何在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
相关产品推荐
相关产品推荐

