Using an amazon step function, how do I write a choice operator that references current time?

Viewed 7010

In the aws-step function documentation, it seems possible to write a 'choice' state comparing variables from the current state. However, is it possible to write a timestamp comparison referencing the current time? For example, say I want a particular state to be enabled when the time in my state's $.myTime property is TimestampGreaterThan the [current time]. For example:

{
   "Variable": "$.myTime",
   "TimestampGreaterThan": "<the current time>",
   "Next": "MyTimeSpecificState"
}

Is it possible to reference the current time, or do I have to manually set that on the state in a separate task?

2 Answers

Yes, it is currently (as of 2020) possible by using the Context Object ($$), here's how:

{
   "Variable": "$.myTime",
   "TimestampGreaterThan": "$$.State.EnteredTime",
   "Next": "MyTimeSpecificState"
}

Here's the link to the AWS Step Functions Developers Guide: Context Object.

You cannot directly reference the current timestamp in a State Choice comparison. There are couple of approaches to solve this.

  • Instead of returning 'myTime', you can return the matched result with current timestamp from the previous task. (Preferred)
  • Use a new task to return whether the myTime matches with the current timestamp and return the results.
Related