Sagi Buchbinder Shadur
Sagi Buchbinder Shadur

Reputation: 246

AWS Step Function NumericGreaterThan a Parameter

In an AWS Step Function, in a Choice step, we want to compare a result from an AWS Lambda function to a threshold given as a parameter using "NumericGreaterThan".

In our example, we compare a calculated from a lambda with a threshold given by the event.

I tried defining my step function in the following way:

{
  "StartAt": "Check Enough Data",
  "States": {
    "Check Enough Data": {
      "Type": "Task",
      "Resource": "arn:aws:lambda:REGION:ID:function:FUNCTION:$LATEST",
      "Next": "Validate Count",
      "ResultPath": "$.count"
    },
    "Validate Count": {
      "Type": "Choice",
      "Choices": [
        {
          "Variable": "$.count",
          "NumericGreaterThan": "$.threshold",
          "Next": "Succeed State"
        }
      ],
      "Default": "Wait 24 Hours"
    },
    "Wait 24 Hours": {
      "Type": "Wait",
      "Seconds": 86400,
      "Next": "Check Enough Data"
    },
    "Succeed State": {
      "Type": "Succeed"
    }
  }
}

but got an error Expected value of type: Integer, Float insted of String. If I replace "$.threshold" with a hard-coded value (like 20), it works, but the value is not dynamic as I want.

The following input should cause the lambda to get to the Succeed State:

{
   "country": "japan",
   "threshold": 40
}

I know we can replace the Choice step with another Lambda function, but we do not want to do that from cost-effective issues.

Does anyone have an idea on how to solve the problem?

Upvotes: 4

Views: 3672

Answers (2)

user3042699
user3042699

Reputation: 76

you can use 'NumericGreaterThanPath' operator as per the docs https://docs.aws.amazon.com/step-functions/latest/dg/amazon-states-language-choice-state.html

Within a Choice Rule, the value of Variable can be compared with another value from the state input by appending 'Path' to name of supported comparison operators.

NumericEqualsPath, NumericGreaterThanPath, NumericGreaterThanEqualsPath, etc.

Upvotes: 3

zip
zip

Reputation: 4061

The comparison operators need to have an integer after ":". It can't be an string.

A workaround is that "Variable": "$.count" change to "Variable": "$.count/$.threshold" so that you can have "NumericGreaterThan": 1. In that case, you have count and threshold that define the Choice action.

Let me know if that fixes your problem

Precision: "Variable": "$.count" becomes "Variable": "$.ratio" where ratio = count/threshold

Upvotes: 2

Related Questions