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?
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.
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).
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).
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.
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 {}
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With