AWS Step Functions Context Object - Owlcation - Education
Updated date:

AWS Step Functions Context Object

Author:
aws-step-functions-context-object

The context object is a powerful new construct for AWS Step Functions. As the name implies it allows you to access contextual information about your state machine and state executions such as the time a state was entered and how many retries have been performed.

Additionally states like Map as well as nested workflows can access special information through the context object. Before we dive into the context object if you need a refresher on AWS Step Functions I recommend the below video.

The special context object properties can be accessed via the Parameters attribute of a given state. Where $ refers to the input JSON of a state $$ refers to the context object. For example the Parameters property below is accessing two of the attributes from the context object.

"Parameters": {
   "Input.$": "$",
   "TaskToken.$": "$$.Task.Token"
 }

Below are the properties you can access from the context object, the $$.Task.Token attribute can be used with the callback pattern for asynchronous executions.

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

Context Object in the Map State

The Map state exposes the $$.Map.Item.Index and $$.Map.Item.Value of the item associated with the given parallel execution. This allows your parallel executions the ability to determine where in the array of items being processed they are. You could also use a Choice state to make decisions based on the Index of the current execution for example.

"Map": {
   "Item": {
      "Index": "Number",
      "Value": "String"
   }
}
  • AWS Step Function Map State
    AWS Step Functions has released support for dynamic parallelism in the form of the new Map state. This article takes a look at the new state and it's special parameters and properties.

Context Object in Nested Workflows

Although there is no special metadata associated with calling a nested workflow you can use $$.Task.Token to define the AWS_STEP_FUNCTIONS_STARTED_BY_EXECUTION_ID variable which can be used to allow the child workflow to know what parent workflow started it. This allows you to much more easily trace executions for debugging and auditing.

{  
   "Type":"Task",
   "Resource":"arn:aws:states:::states:startExecution",
   "Parameters":{  
      "StateMachineArn":"arn:aws:states:us-east-1:123456789012:stateMachine:HelloWorld",
      "Input": {
        "Comment": "Hello world!",
        "AWS_STEP_FUNCTIONS_STARTED_BY_EXECUTION_ID.$": "$$.Execution.Id"
       }
   },
   "End":true
}
  • AWS Step Functions Nested Workflows
    AWS Step Functions recently added nested workflows, a feature customers have been asking for a lot. In this article we take a look at the new features added for nested workflows.

Related Articles