Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS Step Function - Adding dynamic value to Pass state type

I have the following state defined in my state machine.

 "loop":{
      "Type": "Pass",
      "Result":{
        "totalCount": "$.newFieldsResponse.body.count",
        "currentCount": 0,
        "step": 1
      },
      "ResultPath": "$.iteration",
      "Next":"iterateLoop"
    },

I expect the output of the state to be:

"newFieldsResponse": {
      "isSuccess": true,
      "error": "",
      "body": {
        "count": 2,
        "fields": [...]
      }
    },
    "iteration": {
      "totalCount": 5,
      "currentCount": 0,
      "step": 1
    }
  }

iteration property is added to the input with totalCount property to be set to count of items in fields array.

However, the output for "iteration" property is set as:

"iteration": {
      "totalCount": "$.newFieldsResponse.body.count",
      "currentCount": 0,
      "step": 1
    }

It looks like the value "$.newFieldsResponse.body.count" is not getting resolved and is output as is.

Is there something I am doing wrong ? Can someone please advice on how to make it work ?

like image 297
Furqan Shaikh Avatar asked Jan 31 '19 03:01

Furqan Shaikh


People also ask

How do you pass input parameters to Step Functions?

Step Functions paths use JsonPath syntax. To specify that a parameter use a path to reference a JSON node in the input, end the parameter name with . $ . For example, if you have text in your state input in a node named message , you could pass that to a parameter by referencing the input JSON with a path.

Is AWS Step Functions a state machine?

Step Functions is based on state machines and tasks. A state machine is a workflow. A task is a state in a workflow that represents a single unit of work that another AWS service performs. Each step in a workflow is a state.

How do you pass Lambda output to step function?

In the console, go to the “Step Functions” section (under Application Services). Choose “Create a State Machine”. Once you have pasted in the code, you will need to add the ARN (resource locators) for your two lambda functions.

What state type can be used to execute a set of steps for each element of an input array in parallel?

The Map state ( "Type": "Map" ) can be used to run a set of steps for each element of an input array. While the Parallel state executes multiple branches of steps using the same input, a Map state will execute the same steps for multiple entries of an array in the state input.


2 Answers

It works in a combination of two of the solutions here exposed:

  1. Add Parameter in Pass step
  2. Add *.$ in the name of the var you want to pass:
 {
  "loop": {
    "Type": "Pass",
    "parameters": {
      "totalCount.$": "$.newFieldsResponse.body.count",
      "currentCount": 0,
      "step": 1
    },
    "ResultPath": "$.iteration",
    "Next": "iterateLoop"
  }
 }
like image 35
Rows Priego Avatar answered Sep 17 '22 15:09

Rows Priego


This is possible through "Parameters" in pass state

JSON

{
  "loop": {
    "Type": "Pass",
    "Parameters": {
      "totalCount": "$.newFieldsResponse.body.count",
      "currentCount": 0,
      "step": 1
    },
    "ResultPath": "$.iteration",
    "Next": "iterateLoop"
  }
}
like image 145
shashi Avatar answered Sep 16 '22 15:09

shashi