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

在AWS Step Functions的Map状态中如何为Lambda传递前序参数?

解决方案

问题根源

当Map状态使用ItemReader(如s3:listObjectsV2)时,每个子执行的输入默认仅包含ItemReader返回的单个S3对象元数据(即你看到的那5个键值对),前序Lambda返回的变量处于Map状态的顶层输入中,子执行无法直接访问;另外你提到Map输入中存在bucket键,但代码中引用的是$.s3_bucket_name,这也可能是变量名不匹配导致的错误。

修复步骤

1. 修正变量名(若需要)

如果前序Lambda返回的字段是bucket而非s3_bucket_name,先将Map内Lambda的参数引用改为匹配的字段名:

"Payload": {
  "bucket.$": "$.bucket",
  "Key.$": "$.Key"
}

2. 将顶层变量注入Map子执行输入

若前序变量确实存在于Map的顶层输入中,需通过Map状态的Parameters字段,将顶层变量与ItemReader返回的S3对象元数据合并,传递给每个子执行:

修改后的Map状态定义如下:

"Map": {
  "Type": "Map",
  "Parameters": {
    // 将顶层输入的s3_bucket_name(或bucket)传递到子执行
    "parent_bucket.$": "$.s3_bucket_name",
    // 保留ItemReader返回的S3对象元数据
    "s3_object.$": "$"
  },
  "ItemProcessor": {
    "ProcessorConfig": {
      "Mode": "DISTRIBUTED",
      "ExecutionType": "STANDARD"
    },
    "StartAt": "Lambda change content",
    "States": {
      "Lambda change content": {
        "Type": "Task",
        "Resource": "arn:aws:states:::lambda:invoke",
        "OutputPath": "$.Payload",
        "Parameters": {
          "FunctionName": "AnotherLambdaFunctionInsideMapState-toTakeInputFromPreviousStates-HereTakingS3BucketName",
          "Payload": {
            // 从合并后的输入中获取顶层变量
            "bucket.$": "$.parent_bucket",
            // 从合并后的S3对象数据中获取Key
            "Key.$": "$.s3_object.Key"
          }
        },
        "Retry": [
          {
            "ErrorEquals": [
              "Lambda.ServiceException",
              "Lambda.AWSLambdaException",
              "Lambda.SdkClientException",
              "Lambda.TooManyRequestsException"
            ],
            "IntervalSeconds": 2,
            "MaxAttempts": 6,
            "BackoffRate": 2
          }
        ],
        "End": true
      }
    }
  },
  "Label": "Map",
  "MaxConcurrency": 25,
  "ItemReader": {
    "Resource": "arn:aws:states:::s3:listObjectsV2",
    "Parameters": {
      "Bucket": "s3Bucket",
      "Prefix": "examplePrefixForParallelExecutions"
    }
  },
  "ResultPath": null,
  "End": true
}

3. 验证输入传递

修改后,每个子执行的输入会变成如下结构,Lambda即可同时获取到前序变量和S3对象数据:

{
  "parent_bucket": "your-bucket-name",
  "s3_object": {
    "Etag":"etag",
    "Key":"s3objectURI",
    "LastModified":1.69321E9,
    "Size":500,
    "StorageClass":"STANDARD"
  }
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.11 23:46:30