如何在AWS Step Functions中通过动态变量从字典取值?
问题:AWS Step Functions能否无需Lambda,通过输入变量从字典中取值?
输入示例如下:
{ "workflows": {"A": "A_UIID", "B": "B_UUID", "C": "C_UUID" }, "name": "A" }
尝试在Pass状态中通过变量name从workflows字典中获取对应值,以下两种配置均执行失败:
写法1:
"GetId": { "Type": "Pass", "Parameters": { "id.$": "$['workflows'][$.name]" }, "ResultPath": "$.domain_workflow", "Next": "Next" }
写法2:
"GetId": { "Type": "Pass", "Parameters": { "id.$": "$['workflows'][$['name']]" }, "ResultPath": "$.domain_workflow", "Next": "Next" }
但使用固定键值时可以正常运行:
"GetId": { "Type": "Pass", "Parameters": { "id.$": "$['workflows']['A']" }, "ResultPath": "$.domain_workflow", "Next": "Next" }
解决方案
可以无需Lambda实现该需求。你之前的写法失败是因为Step Functions的JSONPath语法不支持直接用嵌套变量引用作为字典键,需要使用AWS Step Functions内置的States.GetItem函数来实现动态键取值。
修改后的Pass状态配置如下:
"GetId": { "Type": "Pass", "Parameters": { "id.$": "States.GetItem($.workflows, $.name)" }, "ResultPath": "$.domain_workflow", "Next": "Next" }
States.GetItem函数接收两个参数:第一个是目标字典(此处为$.workflows),第二个是要获取的键名(此处为$.name),执行后会返回字典中对应键的值,完全匹配你的需求。
内容的提问来源于stack exchange,提问作者yakutcv
相关产品推荐
相关产品推荐

