I have a state machine which has a choice state. In that choice state I want one of the conditions to be if a certain date is less that the current date minus 24 hours.
How can I achieve this? Must I use a lambda function?
My date is in ISO 8601 format like 2023‐08‐21T15:41:42Z
2
Answers
In AWS Step Functions, the "Choice" state enables you to perform conditional branching in your state machine based on the values of the input. However, the "Choice" state doesn’t have the ability to perform arithmetic operations or compute time differences out-of-the-box. To make date comparisons like the one you described, you’d typically need to involve a Lambda function.
Here’s how you can achieve this:
Lambda Function for Date Comparison:
Create a Lambda function that:
Choice State in Step Functions:
After invoking the Lambda function, use a Choice state to branch based on the result of the Lambda function.
Here’s a high-level example:
Lambda Function (Python):
State Machine:
In this design, the state machine invokes the Lambda function, and based on the result, it goes to one of the two branches:
MoreThan24HoursBranch
orLessThan24HoursBranch
.AFAIK, there is no support from AWS States Language to work with dates.
Is passing the limits as part of the body an option for you? If so, there may be a couple of choices –
1692654212.345
seconds.Following one of the approaches mentioned above should let you use the state machine language when defining the conditions for a choice state.