Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS Step Function: check for null

Step Function is defined like that:

{
  "StartAt": "Decision_Maker",
  "States": {
    "Decision_Maker":{
      "Type": "Choice",
      "Choices": [
        {
          "Variable": "$.body.MyData",
          "StringEquals": "null", //that doesn't work :(
          "Next": "Run_Task1"
        }],
        "Default": "Run_Task2"
    },
    "Run_Task1": {
      "Type": "Task",
      "Resource": "url_1",
      "Next": "Run_Task2"
    },
    "Run_Task2": {
      "Type": "Task",
      "Resource": "url_2",
      "End": true
    }
  }
}

Basically it's a choice between 2 tasks. Input data is like this:

{
    "body": {
        "prop1": "value1",
        "myData": {
            "otherProp": "value"
        }
    }
}

The problem is that sometimes there's no myData in JSON. So input may come like this:

{
    "body": {
        "prop1": "value1",
        "myData": null
    }
}

How do I check whether or not myData is null?

like image 722
Ivan Studenikin Avatar asked Jul 22 '20 16:07

Ivan Studenikin


People also ask

What is choice state in step function?

A Choice state ( "Type": "Choice" ) adds conditional logic to a state machine. In addition to most of the common state fields, Choice states contains the following additional fields. Choices (Required) An array of Choice Rules that determines which state the state machine transitions to next.

How do you pass data between Step Functions?

You can give AWS Step Functions initial input data by passing it to a StartExecution action when you start an execution, or by passing initial data using the Step Functions console . Initial data is passed to the state machine's StartAt state. If no input is provided, the default is an empty object ( {} ).

What is ASL in AWS?

The Amazon States Language is a JSON-based, structured language used to define your state machine, a collection of states, that can do work ( Task states), determine which states to transition to next ( Choice states), stop an execution with an error ( Fail states), and so on.


2 Answers

As of August 2020, Amazon States Language now has an isNull and isPresent Choice Rule. Using these you can natively check for null or the existence of a key in the state input inside a Choice state.

Example:

{ "Variable": "$.possiblyNullValue", "IsNull": true }

https://docs.aws.amazon.com/step-functions/latest/dg/amazon-states-language-choice-state.html#amazon-states-language-choice-state-rules

like image 118
adamwong Avatar answered Oct 20 '22 05:10

adamwong


The order matters. Set the "IsPresent": false first, then the "IsNull": true, finally the scalar comparison last.

    "Check MyValue": {
      "Comment": "Check MyValue",
      "Type": "Choice",
      "Default": "ContinueWithMyValue",
      "Choices": [
        {
          "Or": [
            {
              "Variable": "$.MyValue",
              "IsPresent": false
            },
            {
              "Variable": "$.MyValue",
              "IsNull": true
            },
            {
              "Variable": "$.MyValue",
              "BooleanEquals": false
            }
          ],
          "Next": "HaltProcessing"
        },
        {
          "Variable": "$.MyValue",
          "BooleanEquals": true,
          "Next": "ContinueWithMyValue"
        }
      ]
    },
like image 43
Peter Wilmersdorf Avatar answered Oct 20 '22 03:10

Peter Wilmersdorf