skip to Main Content

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


  1. 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:

    1. Lambda Function for Date Comparison:
      Create a Lambda function that:

      • Accepts the input date in ISO 8601 format.
      • Computes the difference between the current date and the provided date.
      • Returns a result indicating whether the input date is more than 24 hours old.
    2. 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):

    import datetime
    
    def lambda_handler(event, context):
        input_date_str = event.get('date')
        input_date = datetime.datetime.fromisoformat(input_date_str)
        
        current_date = datetime.datetime.utcnow()
        delta = current_date - input_date
    
        return {
            'isMoreThan24Hours': delta.total_seconds() > 86400  # 24 hours = 86400 seconds
        }
    

    State Machine:

    {
      "Comment": "A state machine that checks if a date is more than 24 hours old.",
      "StartAt": "CheckDate",
      "States": {
        "CheckDate": {
          "Type": "Task",
          "Resource": "arn:aws:lambda:REGION:ACCOUNT_ID:function:YourLambdaFunctionName",
          "Next": "IsOlderThan24Hours"
        },
        "IsOlderThan24Hours": {
          "Type": "Choice",
          "Choices": [
            {
              "Variable": "$.isMoreThan24Hours",
              "BooleanEquals": true,
              "Next": "MoreThan24HoursBranch"
            }
          ],
          "Default": "LessThan24HoursBranch"
        },
        "MoreThan24HoursBranch": {
          "Type": "Pass",
          "Result": "The date is older than 24 hours.",
          "End": true
        },
        "LessThan24HoursBranch": {
          "Type": "Pass",
          "Result": "The date is less than 24 hours old.",
          "End": true
        }
      }
    }
    

    In this design, the state machine invokes the Lambda function, and based on the result, it goes to one of the two branches: MoreThan24HoursBranch or LessThan24HoursBranch.

    Login or Signup to reply.
  2. 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 –

    1. Work with dates as a complex object with day, month, year, hours, minutes, seconds, milliseconds, and time zone attributes i.e.,
    {
        "day": 21,
        "month": 8,
        "year": 2023,
        "hour": 21,
        "minute": 43,
        "second": 32,
        "millis": 345,
        "zone": 0
    }
    
    1. Work with dates in their number format. Dates are basically the number of seconds passed since epoch. For the timestamp defined in (1), the same will be about 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search