AWS Step Functions adds updates to ‘choice’ state, global access to context object, dynamic timeouts, result selection, and intrinsic functions to Amazon States Languages

This post was originally published on this site

Developers can use AWS Step Functions to design and execute workflows that connect services such as AWS Lambda, AWS Fargate, and Amazon SageMaker into a rich application. A workflow consists of a series of steps, with the output of one step being the input to the next step. Application development becomes more intuitive using the AWS Step Functions, allowing developers to configure each applications with chain of functions such as a AWS Lambda function, or a function on the container that is developed stateless as a set of states.

Today, we are announcing enhancements of AWS Step Functions with updates to Amazon States Language (ASL). ASL is a JSON-based structured language that defines state machines and collections of states that can perform work (Task states), determines which state to transition to next (Choice state), and stops execution on error (Fail state). Today’s updates allow customers to write simplified workflow applications, increase flexibility within the state machine definition, reduce lambda calls, and reduce state transitions to save money.

If you access the AWS Step Functions management console, you’ll see new code snippets under the definition step.

Updates to Choice State

Choice State basically adds branch logic to the state machine. This update adds several new operators and provides additional selection that allow operators to simplify existing definitions or add dynamic behavior within state machine definitions.

  1. Comparison Operator – supports a test for below values;
    IsNULL – null
    IsString – string
    IsNumeric – numeric
    IsBoolean – boolean
    IsTimestamp – timestamp
    {
    "Variable": "$.foo",
    "IsNull|IsString|IsNumeric|IsBoolean|IsTimestamp": true|false
    }
  2. Existence Test – supports a test for the existence or non-existence of a particular field.
    {
    "Variable": "$.foo",
    "IsPresent": true|false
    }
  3. Wildcarding – supports shell “glob” style wildcards, so customers can test for log-*.txt or *LATEST*.
    {
    "Variable": "$.foo",
    "StringMatches": "log-*.txt"
    }
  4. Variable to Variable Comparison – allows for the comparison of an input field to another input field. Currently, the choice state allows for comparison to a fixed value.
    {
    "Variable": "$.foo",
    "StringEqualsPath": "$.bar"
    }

Global access to the context object

In the past, the context object was only accessible in the Parameters block, but with this update removing this restriction you have the flexibility to reference the context object outside the parameter block. Accessing the context object will now be allowed wherever ASL allows JSON reference paths. This will give you access to the context object in the following fields:

  •  InputPath
  • OutputPath
  • ItemsPath (in Map states)
  • Variable (in Choice states)
  • ResultSelector
  • Variable to variable comparison operators

Below is example of how global access to the context object simplify existing description.

Dynamic Timeouts

ASL optionally supported two time out parameters before this update, “TimeoutSeconds” and “HeartbeatSeconds”. “TimeoutSeconds” returns an error if the task runs longer than the specified number of seconds, and “HeartbeatSeconds” returns an error if the heartbeat interval from the task is longer than the specified number of seconds. Some applications want to set up those parameters to fluctuate over time dynamically, which you can now use the new parameters TimeoutSecondsPath” and “HeartBeatSecondsPath” to do.

{
"Type": "Task",
"Resource": "arn:aws:states:::glue:startJobRun.sync",
"Parameters": {
"JobName": "GlueJob-JTrRO5l98qMG"
},
"TimeoutSecondsPath": "$.params.maxTime",
"HeartbeatSecondsPath": "$.params.heartBeat"
}

Result Selector

The execution result may include metadata along with the payload. For example, a task state that calls a lambda function returns a payload, but a call through some service integration frameworks may use the path state to return the payload, resulting in metadata that the customer needs to filter again. In the past, if you didn’t need metadata, you had to use another state to manipulate it. This new feature eliminates the need for this and also allows customers to reduce their payload size. You can add a parameter style object to help customers filter the task status output and pass the fields of interest to the result path.

{
  "Type": "Task",
  "Resource": "arn:aws:states:::elasticmapreduce:createCluster.sync",
  "Parameters": {
    ...
  },
"ResultSelector": {
         "ClusterId.$": "$.output.ClusterId",
         "ResourceType.$": "$.resourceType"
  },

"ResultPath": "$.EMRoutput"
}

String Construction

This update makes it possible to complement input values ​​and concatenate character strings. You can also add a string constructor to allow customers to build field values ​​from inputs.

{
  "Parameters": {
    "foo.$": "States.Format('Hello, {} {}', $.firstName, $.lastName)"
  }
}

You can only use string as acceptable data types.

JSON to String and StringToJSON

When the customer submitted the input to DynamoDB, there was no way to change object to string within the object, so customers couldn’t directly submit the JSON object and had to use a Lambda function. With this update, customer can directly convert JSON to string in the object.

{
  "Type": "Task",
  "Resource": "arn:aws:states:::some.future.integration:run.sync",
  "Parameters": {
    "FieldThatNeedsToBeAString.$": "states.JsonToString($.JSONInputField)",
  }
}

This also works the other way, allowing the customer to convert string to JSON without calling an external Lambda function.

{
"Type": "Task",
"Resource": "arn:aws:states:::some.future.integration:run.sync",
"Parameters": {
"FieldThatNeedsToBeJSON.$": "states.stringToJson($.EscapedInputField)"
}
}

State Array

States now can be set up as array to handle multiples under the same definition.

"X": {
"Type": "Task",
"Resource": "arn:aws:lambda:us-east-1:123456789012:function:HelloWorld",
"Parameters": {
"PayloadString.$": "States.Format('[[{}]]', States.JsonToString($.in.summary))",
"CmdLine.$": "States.Array('--maxp', $.params.maxpr, '--minp', $.params.minpr)",
"ControlBlock.$": "States.StringToJson($.output.control)"
},
"Next": "AllDone"
}

Available Today

These updates are available today for all AWS Regions where AWS Step Functions are available except for China regions. Please visit our documentation for more detail.

– Kame;

 

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.