Skip to content

Instantly share code, notes, and snippets.

@csrwng
Last active June 24, 2020 15:11
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 csrwng/645bf7f0586dbc33b74c422e94b84e5b to your computer and use it in GitHub Desktop.
Save csrwng/645bf7f0586dbc33b74c422e94b84e5b to your computer and use it in GitHub Desktop.
#!/bin/bash
set -e
CLUSTER_ID="${CLUSTER_ID:-}"
if [[ -z "${CLUSTER_ID}" ]]; then
CLUSTER_ID="$(cat ~/data/install/current/metadata.json | jq -r '.aws.identifier[] | select(keys[]|contains("kubernetes.io")) | keys[0]')"
fi
echo "CLUSTER_ID=${CLUSTER_ID}"
echo "Instances for current cluster"
aws ec2 describe-instances --filters "Name=tag-key,Values=${CLUSTER_ID}" | \
jq -r '.Reservations[].Instances[] | "\(.InstanceId) \(.State.Name) \(.Tags[] | select(.Key == "Name") | .Value)"' | sort -k2
INSTANCES="$(aws ec2 describe-instances --filters "Name=tag-key,Values=${CLUSTER_ID}" | \
jq -r '.Reservations[].Instances[].InstanceId' | tr '\n' ' ')"
echo "Starting instances..."
aws ec2 start-instances --instance-ids ${INSTANCES} > /dev/null
#!/bin/bash
set -e
CLUSTER_ID="${CLUSTER_ID:-}"
if [[ -z "${CLUSTER_ID}" ]]; then
CLUSTER_ID="$(cat ~/data/install/current/metadata.json | jq -r '.aws.identifier[] | select(keys[]|contains("kubernetes.io")) | keys[0]')"
fi
echo "CLUSTER_ID=${CLUSTER_ID}"
echo "Instances for current cluster"
aws ec2 describe-instances --filters "Name=tag-key,Values=${CLUSTER_ID}" | \
jq -r '.Reservations[].Instances[] | "\(.InstanceId) \(.State.Name) \(.Tags[] | select(.Key == "Name") | .Value)"' | grep -i "running" | sort -k2
INSTANCES="$(aws ec2 describe-instances --filters "Name=tag-key,Values=${CLUSTER_ID}" | \
jq -r '.Reservations[].Instances[] | "\(.InstanceId) \(.State.Name)"' | grep -i "running" | awk '{ print $1}' | tr '\n' ' ')"
echo "Stopping instances..."
aws ec2 stop-instances --instance-ids ${INSTANCES} > /dev/null
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment