Accessing execution data from the Context object in Step Functions - AWS Step Functions

Accessing execution data from the Context object in Step Functions

Managing state and transforming data

Step Functions recently added variables and JSONata to manage state and transform data.

Learn about Passing data with variables and Transforming data with JSONata.

The Context object is an internal JSON structure that is available during an execution, and contains information about your state machine and execution. The context provides your workflows information about their specific execution. Your workflows can reference the Context object in a JSONata expression with $states.context.

Accessing the Context object

To access the Context object in JSONata

To access the Context object in JSONata states, use $states.context in a JSONata expression.

{ "ExecutionID" : "{% $states.context.Execution.Id %}" }

To access the Context object in JSONPath

To access the Context object in JSONPath, you first append .$ to the end of the key to indicate the value is a path. Then, prepend the value with $$. to select a node in the Context object.

{ "ExecutionID.$": "$$.Execution.Id" }

JSONPath states can refer to the context ($$.) from the following JSONPath fields:

  • InputPath

  • OutputPath

  • ItemsPath (in Map states)

  • Variable (in Choice states)

  • ResultSelector

  • Parameters

  • Variable to variable comparison operators

Context object fields

The Context object includes information about the state machine, state, execution, and task. This JSON object includes nodes for each type of data, and is in the following format.

{ "Execution": { "Id": "String", "Input": {}, "Name": "String", "RoleArn": "String", "StartTime": "Format: ISO 8601", "RedriveCount": Number, "RedriveTime": "Format: ISO 8601" }, "State": { "EnteredTime": "Format: ISO 8601", "Name": "String", "RetryCount": Number }, "StateMachine": { "Id": "String", "Name": "String" }, "Task": { "Token": "String" } }

During an execution, the Context object is populated with relevant data. RedriveTime Context object is only available if you've redriven an execution. If you've redriven a Map Run, the RedriveTime context object is only available for child workflows of type Standard. For a redriven Map Run with child workflows of type Express, RedriveTime isn't available.

Content from a running execution includes specifics in the following format.

{ "Execution": { "Id": "arn:aws:states:us-east-1:123456789012:execution:stateMachineName:executionName", "Input": { "key": "value" }, "Name": "executionName", "RoleArn": "arn:aws:iam::123456789012:role...", "StartTime": "2019-03-26T20:14:13.192Z" }, "State": { "EnteredTime": "2019-03-26T20:14:13.192Z", "Name": "Test", "RetryCount": 3 }, "StateMachine": { "Id": "arn:aws:states:us-east-1:123456789012:stateMachine:stateMachineName", "Name": "stateMachineName" }, "Task": { "Token": "h7XRiCdLtd/83p1E0dMccoxlzFhglsdkzpK9mBVKZsp7d9yrT1W" } }
Note

For Context object data related to Map states, see Context object data for Map states.

Context object data for Map states

Managing state and transforming data

Step Functions recently added variables and JSONata to manage state and transform data.

Learn about Passing data with variables and Transforming data with JSONata.

There are two additional items available in the Context object when processing a Map state: Index and Value. For each Map state iteration, Index contains the index number for the array item that is being currently processed, while Value contains the array item being processed. Within a Map state, the Context object includes the following data:

"Map": { "Item": { "Index": Number, "Value": "String" } }

These are available only in a Map state, and can be specified in the ItemSelector (Map) field.

Note

You must define parameters from the Context object in the ItemSelector block of the main Map state, not within the states included in the ItemProcessor section.

Given a state machine using a JSONPath Map state, you can inject information from the Context object as follows.

{ "StartAt": "ExampleMapState", "States": { "ExampleMapState": { "Type": "Map", "ItemSelector": { "ContextIndex.$": "$$.Map.Item.Index", "ContextValue.$": "$$.Map.Item.Value" }, "ItemProcessor": { "ProcessorConfig": { "Mode": "INLINE" }, "StartAt": "TestPass", "States": { "TestPass": { "Type": "Pass", "End": true } } }, "End": true } } }

If you execute the previous state machine with the following input, Index and Value are inserted in the output.

[ { "who": "bob" }, { "who": "meg" }, { "who": "joe" } ]

The output for the execution returns the values of Index and Value items for each of the three iterations as follows:

[ { "ContextIndex": 0, "ContextValue": { "who": "bob" } }, { "ContextIndex": 1, "ContextValue": { "who": "meg" } }, { "ContextIndex": 2, "ContextValue": { "who": "joe" } } ]