Question:
I have the following state defined in my state machine.
1 2 3 4 5 6 7 8 9 10 11 |
"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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
"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:
1 2 3 4 5 6 |
"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 ?
Answer:
Looks like this may not be possible.
The workaround I did is to use “Parameters” property. From AWS documentation:
“For key-value pairs where the value is selected using a path, the key name must end in *.$. “.
So resolved the above by :
- Changing Pass state to remove any dynamic value reference
1 2 3 4 5 6 7 8 9 10 |
"loop":{ "Type": "Pass", "Result":{ "currentCount": 0, "step": 1 }, "ResultPath": "$.iteration", "Next":"iterateLoop" }, |
- creating a Parameters property where I need the values as below:
1 2 3 4 5 6 7 8 9 10 11 12 |
"iterateLoop":{ "Type":"Task", "Resource": "arn:aws:lambda:....r", "Parameters":{ "totalCount.$": "$.newFieldsResponse.body.count", "currentCount.$": "$.iteration.currentCount", "step.$": "$.iteration.step" }, "ResultPath": "$.iteration", "Next":"continueLoop" }, |
totalCount, currentCount and step all read the value from using a path in the state input. The key needs to be appended with a “.$” at the end.