Raj
Raj

Reputation: 274

Make AWS Step function Parameter Optional

Is there a way to make AWS Step function parameter optional? or accept an expression to say if the value is passed pick the value else default it to a certain value?

Example:

Lets say my Parameters are defined as follows:

"Parameters": {
        "comment": "Selecting what I care about.",
        "MyDetails": {
          "size.$": "$.inputSize"
        }
      },

If I don't pass inputSize, the step function fails. is there a way to make this an optional parameter or have an expression like inputSize || 10 where 10 would be picked if nothing is passed

Upvotes: 3

Views: 4960

Answers (2)

Phill
Phill

Reputation: 18796

I managed to solve this myself by including an extra step.

So my StepFunction loops around back on itself, so the first time it's executed the value doesn't exist and I need to pass in 0, then increment it as it loops around.

"Prepare": {
  "Type": "Task",
  "TimeoutSeconds": 60,
  "Resource": "...",
  "ResultPath": "$.prepareResult",
  "Parameters": {
    "Runs.$": "$.prepareResult.runs"
  },
  ...
  "Next": "PrepareDecision"
},

The first time this is run prepareResult does not exist, so this throws an error when executed.

Once executed it returns a result which is appended to prepareResult and contains the value for runs.

To default the value I included a PrepareDefaults step, which is executed before this step.

"PrepareDefaults": {
  "Type": "Pass",
  "ResultPath": "$.prepareResult",
  "Parameters": {
    "runs": 0
  },
  "Next": "Prepare"
},

The parameters property is returned as is and assigned to the path prepareResult which allows it to exist on the next step.

When the StepFunction is executed, the default value of 0 is passed in.

Note: On the PrepareDefaults state, you must set a ResultPath for the parameters to be assigned to, if you don't it will override /all/ the data in the current execution.

Note 2: You can use this method with Choice to say if a value exists go directly to state X, if the value doesn't exist go to Y to default it before going to X

Upvotes: 2

Hammad Akhtar
Hammad Akhtar

Reputation: 327

Short answer is NO.

Two things to note about Step Function state input/output and state InputPath/OutputPath

  1. Input processing doesn't support default property values. All the properties have to come either from the previous state or as input to step function invocation. (Input and Output processing)
  2. JsonPath can be used only to reference a property in InputPath or OutputPath. If a poperty is not present in the input or result an error is thrown. (Reference Paths)

Suggestion

My suggestion is to set the inputSize property to zero or a negative number either in previous state or during step function invocation (don't forget to pass it along various intermediate states). Then you can have a choice state where you can take different routes in your workflow depending upon the value of the inputSize property.

Another way could be to have a mandatory boolean property which tells if a particular property is present or not in the input or result. This can be helpful in the case we want to check for nullness of a property and take different routes in the workflow.

Upvotes: 3

Related Questions