Skip to content

Instantly share code, notes, and snippets.

@haversnail
Created May 24, 2022 23:48
Show Gist options
  • Save haversnail/b4ec88887fd06c03d771640d4d01d88f to your computer and use it in GitHub Desktop.
Save haversnail/b4ec88887fd06c03d771640d4d01d88f to your computer and use it in GitHub Desktop.
A lightweight wrapper around the AWS CLI for interacting with the App Runner service.
#!/bin/sh
usage() {
echo >&2
echo "A lightweight wrapper around the AWS CLI for interacting with AWS App Runner." >&2
echo "Provides feedback during operations." >&2
echo >&2
echo "Usage: $0 <command> [options...]" >&2
echo >&2
echo "pause Pauses a running service" >&2
echo "resume Resumes a paused service" >&2
echo "deploy Triggers a re-deployment" >&2
echo >&2
echo "Note that additional arguments can be passed to the AWS CLI command by appending" >&2
echo "them after the specified command (e.g. \"pause --profile=prod\")." >&2
echo >&2
exit 1
}
if [ -z "$1" ]; then usage; fi
case "$1" in
pause)
COMMAND=pause-service
;;
resume)
COMMAND=resume-service
;;
deploy)
COMMAND=start-deployment
;;
esac
if [ -z "$COMMAND" ]; then
echo "$0: invalid command -- $1" >&2
usage
fi
shift
POLL_INTERVAL=10
SERVICE_ARN=$(aws apprunner list-services $@ | jq -r '.[] | first | .ServiceArn')
RESPONSE=$(aws apprunner $COMMAND --service-arn $SERVICE_ARN $@)
OPERATION_ID=$(echo "$RESPONSE" | jq -r '.OperationId // empty')
if [ -z "$OPERATION_ID" ]; then
echo "$RESPONSE" >&2
exit 1
fi
while
OPERATION=$(aws apprunner list-operations --service-arn $SERVICE_ARN $@ | jq -c --arg ID $OPERATION_ID '.OperationSummaryList[] | select(.Id == $ID)')
STATUS=$(echo "$OPERATION" | jq -r '.Status')
ENDED=$(echo "$OPERATION" | jq -r '.EndedAt // empty')
if [ "$STATUS" = "$OLD_STATUS" ]; then
printf "."
else
printf "\n$STATUS."
fi
OLD_STATUS=$STATUS
[ -z "$ENDED" ]
do sleep $POLL_INTERVAL; done
printf "\n"
# See https://github.com/stedolan/jq/issues/1053#issuecomment-191287612
START_TIME=$(echo "$OPERATION" | jq -r '.StartedAt | sub("(?<before>.*):"; .before ) | strptime("%Y-%m-%dT%H:%M:%S%z") | todate | fromdateiso8601')
END_TIME=$(echo "$OPERATION" | jq -r '.EndedAt | sub("(?<before>.*):"; .before ) | strptime("%Y-%m-%dT%H:%M:%S%z") | todate | fromdateiso8601')
DURATION=$(($END_TIME - $START_TIME))
echo "Operation completed in $(($DURATION / 60))m $(($DURATION % 60))s"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment