Hao Chen
Hao Chen

Reputation: 111

How to avoid simultaneous execution in aws step function

Currently I have a use case that a cloud watch rule will trigger a step function every 5 minutes. I want to have a logic to skip starting another execution if there is one execution already running in step function. Any way to do that?

Upvotes: 8

Views: 7957

Answers (3)

Alexandre
Alexandre

Reputation: 21

This state machine avoids multiple simultaneous executions, and it doesn't cause a deadlock when there are two or more instances in the queue waiting their turn to proceed with the execution. I've tested it and worked perfectly for me.

{
  "StartAt": "Check Executions",
  "States": {
    "Check Executions": {
      "Type": "Task",
      "Parameters": {
        "StateMachineArn": "arn:aws:states:<AWS REGION>:<AWS ACCOUNT>:stateMachine:<STATE MACHINE NAME>",
        "StatusFilter": "RUNNING"
      },
      "Resource": "arn:aws:states:::aws-sdk:sfn:listExecutions",
      "Next": "Executions = 1",
      "ResultSelector": {
        "runningExecutionsCount.$": "States.ArrayLength($.Executions)",
        "firstExecution.$": "$.Executions[-1:]"
      },
      "ResultPath": "$.TaskResult"
    },
    "Executions = 1": {
      "Type": "Choice",
      "Choices": [
        {
          "And": [
            {
              "Variable": "$.TaskResult.firstExecution[0].Name",
              "StringEqualsPath": "$$.Execution.Name"
            }
          ],
          "Next": "Another Step"
        }
      ],
      "Default": "Wait for Completion"
    },
    "Wait for Completion": {
      "Type": "Wait",
      "Seconds": 60,
      "Next": "Check Executions"
    },
    "Another Step": ...
}

Upvotes: 2

Bogdan Niculeasa
Bogdan Niculeasa

Reputation: 95

I know is kinda late but one of the easiest ways to prevent multiple-step function executions in parallel you can do the following:

  1. Add a Step Function -> ListExecutions task at the beginning of the state machine (or where you want it but it makes more sense to check at the beginning) where you filter for the RUNNING instances, something like:

    {
      "StateMachineArn.$": "$$.StateMachine.Id", // Here you access the ARN of the state machine from the Context Object
      "StatusFilter": "RUNNING" // The filter for the RUNNING ones
     }
    
  2. Configure the List Executions task to output a number that represents how many executions are fulfilling the filter (Done in the Output tab in Workflow Studio):

    {
       "runningExecutionsCount.$": "States.ArrayLength($.Executions)" // You can name it however you want. Also, we make use of the ArrayLength intrinsic function to count the elements of the Executions array.
    }
    
  3. Use a Choice flow where you check if the counter returned at the previous step is greater than 1 then just go to a step like SUCCESS otherwise let it continue its usual flow

Upvotes: 4

user189198
user189198

Reputation:

Instead of having your CloudWatch event rule trigger the Step Function directly, you could have it trigger a Lambda function. The Lambda function could check if there are any Step Function executions in the RUNNING state, via the ListExecutions API. If not, the Lambda function could start a new execution via the StartExecution API.

Upvotes: 3

Related Questions