Sub topic 5
Moving data between states
This chapter assumes you can read Amazon States Language (ASL) and have written a Task state that calls a Lambda function. It covers the five fields that shape data as it moves through a state, and the mistakes that most often go with them.
The model: one JSON document
A state machine carries a single JSON document. Each state receives it as input, does its work, and passes a document on as output. Five fields let you reshape the data at different points. They run in a fixed order:
InputPathselects part of the incoming document.Parametersbuilds the exact input sent to the task.- The task runs and produces a result.
ResultSelectortrims or reshapes that result.ResultPathdecides where the result goes in the original input.OutputPathselects what the state passes to the next state.
Selecting and building input
InputPath takes a JSONPath expression. The default is $, the whole document. Parameters then builds a new object. A key ending in .$ takes its value from a path. A key without the suffix is a literal.
{
"Type": "Task",
"Resource": "arn:aws:states:::lambda:invoke",
"InputPath": "$.order",
"Parameters": {
"FunctionName": "arn:aws:lambda:us-east-1:123456789012:function:ValidateOrder:$LATEST",
"Payload": {
"id.$": "$.id",
"source": "web"
}
},
"End": true
}Paths inside Parameters are relative to the output of InputPath. Here $.id means $.order.id in the original document.
The context object, written $$, holds execution metadata such as $$.Execution.Id and $$.Task.Token. Reach it with a .$ key, for example "executionId.$": "$$.Execution.Id".
Shaping the result
A Lambda invoke through the optimized integration returns a large object. It includes Payload, StatusCode, and ExecutedVersion. Usually you want only the payload. ResultSelector trims it:
"ResultSelector": {
"validation.$": "$.Payload"
},
"ResultPath": "$.result"ResultPath controls where the selected result lands:
| ResultPath value | Effect |
|---|---|
| omitted or `$` | The result replaces the entire input. |
| `$.result` | The result is added to the input under the key `result`. The rest of the input is kept. |
| `null` | The result is discarded and the input passes through unchanged. |
OutputPath filters last. After ResultPath has merged the result, "OutputPath": "$.result" would pass on only that branch.
Common errors
- Lost input. If you omit
ResultPath, the task result overwrites the whole document. Later states then cannot find$.orderId. SetResultPathto a sub-path to keep the earlier data. - Missing
.$suffix."Payload": "$"sends the literal string$. Use"Payload.$": "$". - Wrong path base. After
InputPathnarrows the document,Parameterspaths are relative to the narrowed value. - Path not found. A path that points at a missing field fails the state with
States.Runtime. A Choice state can test for presence withIsPresentfirst. - Catch overwriting input. A
Catchwithout its ownResultPathreplaces the input with the error object. Use"ResultPath": "$.error"to keep both.
Intrinsic functions
Inside Parameters, you can call intrinsic functions to build values without a Lambda function. Examples include States.Format for strings and States.JsonToString for serializing an object. They also need the .$ suffix:
"Parameters": {
"message.$": "States.Format('Order {} accepted', $.orderId)"
}Exercise
Exercise
A Task state has no ResultPath. The input is {"orderId": "1001"} and the task returns {"ok": true}. What does the next state receive, and how do you keep orderId available?
Show answer
The next state receives {"ok": true}, because the result replaces the input by default. Add "ResultPath": "$.result" so the output becomes {"orderId": "1001", "result": {"ok": true}}.
Exercise
Which field would you use to drop the StatusCode and ExecutedVersion keys from a Lambda invoke result before storing it?
Show answer
ResultSelector. It reshapes the raw task result before ResultPath places it in the document.