Search code examples
amazon-web-servicesaws-lambdaaws-step-functions

AWS Step Functions history event limitation


I use step functions for a big loop, so far no problem, but the day when my loop exceeded 8000 executions I came across the error "Maximum execution history size" which is 25000.

There is there a solution for not having the history events?

Otherwise, where I can easily migrate my step functions (3 lambda) because aws batch will ask me a lot of code rewrite ..

Thanks a lot


Solution

  • One approach to avoid the 25k history event limit is to add a choice state in your loop that takes in a counter or boolean and decides to exit the loop.

    Outside of the loop you can put a lambda function that starts another execution (with a different id). After this, your current execution completes normally and another execution will continue to do the work.

    Please note that the "LoopProcessor" in the example below must return a variable "$.breakOutOfLoop" to break out of the loop, which must also be determined somewhere in your loop and passed through.

    Depending on your use case, you may need to restructure the data you pass around. For example, if you are processing a lot of data, you may want to consider using S3 objects and pass the ARN as input/output through the state machine execution. If you are trying to do a simple loop, one easy way would be to add a start offset (think of it as a global counter) that is passed into the execution as input, and each LoopProcessor Task will increment a counter (with the start offset as the initial value). This is similar to pagination solutions.

    Here is a basic example of the ASL structure to avoid the 25k history event limit:

    {
      "Comment": "An example looping while avoiding the 25k event history limit.",
      "StartAt": "FirstState",
      "States": {
    
        "FirstState": {
          "Type": "Task",
          "Resource": "arn:aws:lambda:REGION:ACCOUNT_ID:function:FUNCTION_NAME",
          "Next": "ChoiceState"
        },
    
        "ChoiceState": {
          "Type" : "Choice",
          "Choices": [
            {
              "Variable": "$.breakOutOfLoop",
              "BooleanEquals": true,
              "Next": "StartNewExecution"
            }
          ],
          "Default": "LoopProcessor"
        },
    
        "LoopProcessor": {
          "Type" : "Task",
          "Resource": "arn:aws:lambda:REGION:ACCOUNT_ID:function:ProcessWork",
          "Next": "ChoiceState"
        },
    
        "StartNewExecution": {
          "Type" : "Task",
          "Resource": "arn:aws:lambda:REGION:ACCOUNT_ID:function:StartNewLooperExecution",
          "Next": "FinalState"
        },
    
        "FinalState": {
          "Type": "Task",
          "Resource": "arn:aws:lambda:REGION:ACCOUNT_ID:function:FUNCTION_NAME",
          "End": true
        }
      }
    }
    

    Loop Processor Example

    Hope this helps!