'How to stop all running Step Functions of a specific state machine?

I accidentally started very many step functions and now wish to terminate all of them.

Any smart ways to do this using the CLI or web console?



Solution 1:[1]

OK, let's do this using the CLI.

You can stop an execution using the following:

aws stepfunctions stop-execution \
  --execution-arn <STEP FUNCTION EXECUTION ARN>

But since I started way too many executions, it's helpful to be able to list all running executions of a state machine:

aws stepfunctions list-executions \
  --state-machine-arn <STEP FUNCTION ARN> \
  --status-filter RUNNING \
  --output text

Next, make sure to only list execution ARN's for these executions and list each execution ARN on a separate line:

aws stepfunctions list-executions \
  --state-machine-arn <STEP FUNCTION ARN> \
  --status-filter RUNNING \
  --query "executions[*].{executionArn:executionArn}" \
  --output text

Now, we put this together into one command using xargs:

aws stepfunctions list-executions \
  --state-machine-arn <STEP FUNCTION ARN> \
  --status-filter RUNNING \
  --query "executions[*].{executionArn:executionArn}" \
  --output text | \
xargs -I {} aws stepfunctions stop-execution \
  --execution-arn {} 

Now all running executions should be shut down. Make sure you do this with care so that you don't mess up production!

On that note, if you user aws-vault to minimize that very risk, the command above would look something like this:

aws-vault exec test-env -- aws stepfunctions list-executions \
  --state-machine-arn <STEP FUNCTION ARN> \
  --status-filter RUNNING \
  --query "executions[*].{executionArn:executionArn}" \
  --output text | \
xargs -I {} aws-vault exec test-env -- aws stepfunctions stop-execution \
  --execution-arn {} 

Solution 2:[2]

For me xargs was giving issue because my execution-arn was quite big enough.

aws stepfunctions list-executions \
  --state-machine-arn <ARN> \
  --status-filter RUNNING \
  --query "executions[*].{executionArn:executionArn}" \
  --output text | \
    awk '{print}' |
    while read line;
    do aws stepfunctions stop-execution --execution-arn  $line
    done

This did the trick for me. Thanks to @Pål Brattberg

Solution 3:[3]

for some reason after each iteration, it stuck in Mac,

adding >> out.t solves it

aws stepfunctions list-executions \
--state-machine-arn arn:aws:states:us-east-1:322348515048:stateMachine:workflow-dev-acknowledge-Awaiter \
--status-filter RUNNING \
--query "executions[*].{executionArn:executionArn}" \
--output text | \
xargs -I {} aws stepfunctions stop-execution \
--execution-arn {} >> out.t

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Pål Brattberg
Solution 2 speedysinghs.
Solution 3 kfir yahalom