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

在AWS Step Functions中执行的Lambda如何获取当前Execution ID?

在AWS Step Functions的Lambda中获取Execution ID的实现方式

当然可以实现!当Lambda函数被AWS Step Functions调用时,有两种简单的方式获取当前的Execution ID,下面给你详细拆解:

方式一:利用Step Functions自动传递的执行上下文

Step Functions在调用Lambda时,会默认把完整的执行上下文数据作为事件(event)传递给Lambda。这个上下文里就包含了我们需要的Execution.Id。

以Python Lambda为例,你可以直接从event参数里提取:

def lambda_handler(event, context):
    # 从Step Functions传递的上下文里提取Execution ID
    execution_id = event["Execution"]["Id"]
    print(f"当前Step Functions执行ID: {execution_id}")
    
    # 这里写你的业务逻辑代码
    return {
        "statusCode": 200,
        "body": f"成功获取Execution ID: {execution_id}"
    }

注意:这个默认传递的上下文结构是固定的,除了Execution.Id,还包含状态机ARN、当前状态名等其他有用信息,你可以打印整个event来查看完整结构。

方式二:显式在状态机中配置传递Execution ID

如果你只需要传递Execution ID(不想让Lambda接收完整上下文),可以在Step Functions的状态定义里,通过路径引用显式把$$.Execution.Id传给Lambda。

比如状态机的JSON定义片段:

{
  "States": {
    "CallMyLambda": {
      "Type": "Task",
      "Resource": "arn:aws:lambda:us-east-1:123456789012:function:MyLambdaFunction",
      "Parameters": {
        "executionId.$": "$$.Execution.Id",
        "customData": "some-business-value"
      },
      "End": true
    }
  }
}

这时Lambda里就可以直接通过event["executionId"]获取:

def lambda_handler(event, context):
    execution_id = event["executionId"]
    print(f"显式传递的Execution ID: {execution_id}")
    return {
        "statusCode": 200,
        "body": f"Execution ID: {execution_id}"
    }

两种方式都很可靠,你可以根据自己的需求选择——如果需要更多Step Functions的上下文信息,用第一种;如果只想传递必要字段,用第二种就好。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.27 04:11:47