简体   繁体   中英

How can I access preceding state inputs using AWS Step Functions?

Here is my step function:

{
  "Comment": "A description of my state machine",
  "StartAt": "lambda1",
  "States": {
    "lambda1": {
      "Type": "Task",
      "Resource": "arn:aws:states:::lambda:invoke",
      "OutputPath": "$.Payload",
      "Parameters": {
        "Payload.$": "$"
      },
      "Next": "lambda2"
    },
    "lambda2": {
      "Type": "Task",
      "Resource": "arn:aws:states:::lambda:invoke",
      "OutputPath": "$.Payload",
      "Parameters": {
        "Payload.$": {
          "item_id.$": "$.item_id" 
        }
      },
      "Next": "lambda3"
    },
    "lambda3": {
      "Type": "Task",
      "Resource": "arn:aws:states:::lambda:invoke",
      "OutputPath": "$.Payload",
      "Parameters": {
        "Payload.$": {
          "item_id.$": "" <------- How to access the id here
          "data.$": $.data
        }
      },
      "End": true
    }
  }
}

I am passing an item_id to lambda2 , and that lambda returns some output data.

In lambda3 , I need the output data of lambda2 as well as the item_id that was passed to lambda2 from lambda1 .

I cannot modify the application code for lambda2 to also return the item_id passed to it.

Is it possible for me to pass the item_id from lambda1 all the way to lambda3 ?

In lambda3 , I need the output data of lambda2 as well as the item_id that was passed to lambda2 from lambda1 .

Yes, this is possible using ResultPath .

I've detailed the various outcomes of using ResultPath in my answer here .

In your case, you need a ResultPath of $.DataFromLambda1 set for lambda2 which would set the state input of lambda3 to:

  1. the state input of lambda1 (which is the item_id )
  2. + the state output of lambda2 .
"lambda2": {
  ...
  "OutputPath": "$.Payload",
  "ResultPath": "$.DataFromLambda1",
  "Parameters": {
    "Payload.$": {
      "item_id.$": "$.item_id" 
    }
  },
  "Next": "lambda3"
}

Then change lambda3 to:

"lambda3": {
  "Type": "Task",
  "Resource": "arn:aws:states:::lambda:invoke",
  "OutputPath": "$.Payload",
  "Parameters": {
    "Payload.$": {
      "item_id.$": "$.DataFromLambda1.item_id"
      "data.$": $.data
    }
  },
  "End": true
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM