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

如何在AWS Step Functions中用嵌套Map实现嵌套for循环?

在AWS Step Functions中实现嵌套Map传递内外层变量的方案

这个需求完全可以实现,核心是通过合理配置嵌套Map的输入传递逻辑,让内层任务同时获取外层Map的当前项和内层Map的当前项。以下是具体实现方案:

核心配置思路

  • 外层Map:遍历xArr,将当前遍历到的X值与完整的yArr一同传入内层Map的上下文
  • 内层Map:遍历传入的yArr,将外层传递的X值与当前遍历到的Y值组合后,作为任务的输入参数

完整状态机示例

{
  "Comment": "嵌套Map实现嵌套for循环逻辑",
  "StartAt": "OuterMap",
  "States": {
    "OuterMap": {
      "Type": "Map",
      "ItemsPath": "$.xArr",
      "ItemProcessor": {
        "ProcessorConfig": {
          "Mode": "INLINE"
        },
        "StartAt": "InnerMap",
        "States": {
          "InnerMap": {
            "Type": "Map",
            "ItemsPath": "$.yArr",
            "ItemProcessor": {
              "ProcessorConfig": {
                "Mode": "INLINE"
              },
              "StartAt": "ExecuteCalculation",
              "States": {
                "ExecuteCalculation": {
                  "Type": "Task",
                  "Resource": "arn:aws:lambda:REGION:ACCOUNT_ID:function:YourCalculationFunction",
                  "End": true,
                  "ItemSelector": {
                    "x": "$.outerX",
                    "y": "$$.Map.Item.Value"
                  }
                }
              }
            },
            "End": true,
            "ItemSelector": {
              "outerX": "$$.Map.Item.Value",
              "yArr": "$.yArr"
            }
          }
        }
      },
      "End": true
    }
  }
}

关键配置说明

  • 外层Map的ItemSelector:通过$$.Map.Item.Value获取当前遍历的X值并命名为outerX,同时将根节点的yArr原样传递,确保内层Map能拿到完整的Y数组
  • 内层Map的ItemSelector:将外层传入的outerX和当前遍历的Y值($$.Map.Item.Value)组合成任务需要的输入结构,让ExecuteCalculation任务同时拿到x和y参数
  • ItemsPath设置:外层Map的ItemsPath指向$.xArr,内层Map指向$.yArr,确保各自遍历对应的目标数组

输入数据示例

{
  "xArr": ["A", "B", "C"],
  "yArr": ["1", "2", "3"]
}

执行后,ExecuteCalculation任务会收到9组输入,分别是{"x":"A","y":"1"}、{"x":"A","y":"2"}、{"x":"A","y":"3"}、{"x":"B","y":"1"}等,完全复现Java嵌套for循环的遍历逻辑。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.12 13:50:39