Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mvn-bachhuynh-dn/7d6cfdbb6778ba286225402118c7ecdd to your computer and use it in GitHub Desktop.
Save mvn-bachhuynh-dn/7d6cfdbb6778ba286225402118c7ecdd to your computer and use it in GitHub Desktop.
Watch CloudFormation Events during `aws cloudformation deploy`
#!/usr/bin/env bash
set -e
function echo_help {
echo "usage: $0 [options]"
echo ""
echo "example: aws cloudformation deploy ... | $0 [options]"
echo ""
echo "options:"
echo " --stack-name"
echo " --profile"
echo " --region"
echo " --help"
}
function reverse {
local output
while read line
do
output="${line}\n${output}"
done
echo -en "${output}"
}
if [[ $# = 0 ]]
then
>&2 echo_help
exit 1
fi
opt_stack_name=''
opt_profile=''
opt_region=''
while [[ $# -gt 0 ]]; do
case $1 in
--stack-name) opt_stack_name="$2"; shift 2;;
--profile) opt_profile="$2"; shift 2;;
--region) opt_region="$2"; shift 2;;
--help) echo_help; exit 0;;
*)
>&2 echo "[ERROR] unexpected argument: $1"
>&2 echo_help
exit 1;;
esac
done
if [[ ! "${opt_stack_name}" ]]
then
>&2 echo "[ERROR] '--stack-name' parameter missing"
>&2 echo_help
exit 1
fi
exit_code=0
while read line
do
echo "${line}"
if [[ "${line}" = "Waiting for stack create/update to complete" ]]
then
while true
do
recent_deploy_events="$(aws cloudformation describe-stack-events --stack-name "${opt_stack_name}" \
$([[ "$opt_profile" ]] && echo "--profile" "$opt_profile") \
$([[ "$opt_region" ]] && echo "--region" "$opt_region") \
--query 'StackEvents[*].[Timestamp,ResourceType,LogicalResourceId,ResourceStatus,ResourceStatusReason]' --output text \
| awk "NR==1,/\tAWS::CloudFormation::Stack\t${opt_stack_name}\t.*\tUser Initiated\$/" | reverse)"
comm -13 <(echo "${prior_deploy_events:-}") <(echo "${recent_deploy_events}") \
| awk -F '\t' '{printf "%s %-32s %-32s %-24s %s\n",$1,$2,$3,$4,$5}' \
| sed 's/None$//'
tab=$'\t'
if (echo "${recent_deploy_events}" | grep -q "AWS::CloudFormation::Stack${tab}${opt_stack_name}${tab}.*\(_ROLLBACK_COMPLETE|_FAILED\)${tab}")
then
exit_code=1
break
fi
if (echo "${recent_deploy_events}" | grep -q "AWS::CloudFormation::Stack${tab}${opt_stack_name}${tab}.*\(_COMPLETE\)${tab}")
then
exit_code=0
break
fi
prior_deploy_events="${recent_deploy_events}"
sleep 2
done
fi
done
exit ${exit_code}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment