Skip to content

Instantly share code, notes, and snippets.

@utahcon
Last active July 3, 2023 17:28
Show Gist options
  • Save utahcon/908a1e161b3cf2b077dc61829b6a1257 to your computer and use it in GitHub Desktop.
Save utahcon/908a1e161b3cf2b077dc61829b6a1257 to your computer and use it in GitHub Desktop.
Oneliner: stop all running AWS step-functions for given state-machine

NOTE: You will be required to have PROFILE and STATE_MACHINE_ARN set in your environment variables for the below to work. Alternatively set them as part of the oneliner.

The blow will query AWS for all RUNNING executions of your state-machine, then loop through each one sending a stop request. This will terminate the function in whatever state it happens to be in. This will NOT stop external work that has already been invoked. If you run into errors with the below, not that it is trying to run the stop command in parallel, so a smaller parallelism number may be appropriate for your use case.

$ aws --profile ${PROFILE} stepfunctions list-executions --state-machine-arn ${STATE_MACHINE_ARN} --status-filter RUNNING | grep executionArn | awk '{print $2}' | sed -e 's/\"//g' | sed -e 's/,//g' | xargs -L 1 -n 1 -P 10 aws --profile ${PROFILE} stepfunctions stop-execution --execution-arn

@suds-sky
Copy link

Nice one!

@kdiwakar
Copy link

kdiwakar commented Nov 2, 2022

This script will still kill each execution one at a time, is there a way to add some multiplicity/scalability to issue lets say 10 stop execution commands parallely.

Or even better, is there a way we can purge the whole queue tied to an ARN at once to stop 1000 running executions at the same time ?

@ivan-scale
Copy link

maybe too late, but to run this over a big list without need of accepting each output/response from aws, you can add > /tmp/output.txt to the end, like:

$ aws --profile ${PROFILE} stepfunctions list-executions --state-machine-arn ${STATE_MACHINE_ARN} --status-filter RUNNING | grep executionArn | awk '{print $2}' | sed -e 's/\"//g' | sed -e 's/,//g' | xargs -L 1 -n 1 -P 10 aws --profile ${PROFILE} stepfunctions stop-execution --execution-arn > /tmp/output.txt

that way you don't need to do anything, and it will stop all running sfn.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment