Search code examples
amazon-web-servicesstate-machineaws-step-functions

aws step function parallel with input parameters


I am trying to use AWS step functions to create parallel branches of execution. One of the parallel branches starts another step function invocation, how can we pass input from this parallel branch to next step function execution

{
  "Comment": "Parallel Example.",
  "StartAt": "FunWithMath",
  "States": {
    "FunWithMath": {
    "Type": "Parallel",
    "End": true,
    "Branches": [
      {
        "StartAt": "Add",  /// This receives some json object here input {}
        "States": {
          "Add": {    
            "Type": "Task",  ***//How to pass the received input to the following arn as input?***
            "Resource": ""arn:aws:states:::states:startExecution",
            Parameters: {
                 "StateMachineArn": "anotherstepfunctionarnpath"
                }
            "End": true
          }
        }
      },
      {
        "StartAt": "Subtract",
        "States": {
          "Subtract": {
            "Type": "Task",
            "Resource": "some lambda arn here,
            "End": true
          }
        }
      }
    ]
   }
  }
}

anotherstepfunctionarnpath :

{

        "Comment": "Second state machine",
        "StartAt": "stage1",
         "Resource": "arn:aws:states:::glue:startJobRun.sync",
         "Parameters":{
             "Arguments":{
                 "Variable1" :"???" / how to access the value of the input passed to here
                }
           }
}

Solution

  • You can use Input to pass output from one SFN to other one:

    First SFN(It will call second SFN)

    {
      "Comment": "My first SFN",
      "StartAt": "First SFN",
      "States": {
        "First SFN": {
          "Type": "Task",
          "ResultPath": "$.to_pass",
          "Resource": "arn:aws:lambda:us-east-1:807278658150:function:test-lambda",
          "Next": "Trigger Next SFN"
        },
        "Trigger Next SFN": {
          "Type": "Task",
          "Resource": "arn:aws:states:::states:startExecution",
          "Parameters": {
            "Input": {
              "Comment.$": "$"
            },
            "StateMachineArn": "arn:aws:states:us-east-1:807278658150:stateMachine:MyStateMachine2"
          },
          "End": true
        }
      }
    }
    

    Second SFN (MyStateMachine2)

    {
      "Comment": "A Hello World example of the Amazon States Language using Pass states",
      "StartAt": "Hello",
      "States": {
        "Hello": {
          "Type": "Pass",
          "Result": "Hello",
          "Next": "World"
        },
        "World": {
          "Type": "Pass",
          "Result": "World",
          "End": true
        }
      }
    }
    

    First SFN's Execution

    enter image description here

    Second SFN's Execution

    enter image description here

    Explanation The Lambda test-lambda is returning:

    {
      "user": "stackoverflow",
      "id": "100"
    }
    

    Which is stored in "ResultPath": "$.to_pass" here in to_pass variable. I am passing the same output to next state machine MyStateMachine2 which is done by

    "Input": {
       "Comment.$": "$"
    }
    

    In the next State Machine's execution you see that same data is received as input which was created by first Lambda.

    You can read more about it here.