Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 440
Pål Brattberg Avatar asked Nov 15 '20 13:11

Pål Brattberg


People also ask

How does a step functions state machine work?

Each Step Functions state machine can have multiple simultaneous executions, which you can initiate from the Step Functions console, or by using the AWS SDKs, the Step Functions API actions, or the AWS Command Line Interface (AWS CLI). An execution receives JSON input and produces JSON output.

What is a state machine execution?

A state machine execution occurs when an AWS Step Functions state machine runs and performs its tasks. Each Step Functions state machine can have multiple simultaneous executions, which you can initiate from the Step Functions console, or by using the AWS SDKs, the Step Functions API actions, or the AWS Command Line Interface (AWS CLI).

What is state machine execution in AWS step functions?

A state machine execution occurs when an AWS Step Functions state machine runs and performs its tasks. Each Step Functions state machine can have multiple simultaneous executions, which you can initiate from the Step Functions console, or by using the AWS SDKs, the Step Functions API actions, or the AWS Command Line Interface (AWS CLI).

How does the failed state machine work?

It does so by using the failed state machine execution history, which is identified by the Amazon Resource Name (ARN) of the execution. The failed state is marked in the execution history, along with the input to that state (which is also the output of the preceding successful state). The script is able to parse these values from the log.


2 Answers

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 {} 
like image 58
Pål Brattberg Avatar answered Oct 20 '22 21:10

Pål Brattberg


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

like image 3
speedysinghs. Avatar answered Oct 20 '22 23:10

speedysinghs.