Skip to content

Instantly share code, notes, and snippets.

@yokada
Created July 6, 2019 19:26
Show Gist options
  • Save yokada/cb4ad6b74352d5d3710e5ddbeb30eb24 to your computer and use it in GitHub Desktop.
Save yokada/cb4ad6b74352d5d3710e5ddbeb30eb24 to your computer and use it in GitHub Desktop.
Execute statemachine in bash
#!/bin/env bash
# Before exec, you shold create a statemachine named WaitExecution, like below:
# $ aws stepfunctions create-state-machine --endpoint http://localhost:18083 --definition "$(gist -r 799bc111ce4ea7801b2f251e21177aec)" --name WaitExecution --role-arn "arn:aws:iam::012345678901:role/DummyRole" --region ap-northeast-1
echo bash version: ${BASH_VERSION}
#set -x
endpoint="--endpoint http://localhost:18083"
region="--region ap-northeast-1"
stateMachineName="WaitExecution"
query1="stateMachines[?name==\`"
query2="\`].stateMachineArn"
query="--query ${query1}${stateMachineName}${query2}"
output="--output text"
stateMachineArn=$(aws stepfunctions list-state-machines ${endpoint} ${region} ${query} ${output})
if [[ -z ${stateMachineArn} ]]; then
echo "[error] "
exit 1
fi
# Start Execution of the StateMachine. And retrieve the execution ARN.
executionArn=$(aws stepfunctions start-execution ${endpoint} ${region} --state-machine-arn ${stateMachineArn} --query 'executionArn' --output text)
lastStatus=
while [ 1 ]; do
lastStatus=$(aws stepfunctions describe-execution ${endpoint} --execution-arn ${executionArn} ${region} --query 'status' --output text)
for breakStatus in "FAILED" "SUCCEEDED" "ABORTED"; do
if [[ ${lastStatus} = ${breakStatus} ]]; then
echo "execution completed"
break 2
fi
done
echo "$(date '+%Y-%m-%d %H:%M:%S') the last status: ${lastStatus}"
sleep 5
done
# Check lastStatus whether SUCCEEDED or not.
echo ${lastStatus}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment