Skip to content

Instantly share code, notes, and snippets.

@rodrickbrown
Created June 26, 2017 03:28
Show Gist options
  • Select an option

  • Save rodrickbrown/45c60682e9dc5ccc30bc540f12ef0482 to your computer and use it in GitHub Desktop.

Select an option

Save rodrickbrown/45c60682e9dc5ccc30bc540f12ef0482 to your computer and use it in GitHub Desktop.
#!/bin/bash
force=0
function getInstanceIdByEnvTag {
export instance_ids=$(aws ec2 describe-instances --filter Name=tag:Environment,Values=${environment} | jq '.Reservations[].Instances[].InstanceId' | tr -d '"')
echo "The following instances with Tag:${environment} will be stopped"
for instance in ${instance_ids[@]};
do
echo ${instance}
done
}
function execInstancesByInstanceID {
echo "executing: aws ec2 stop-instances --instance-ids ${instance_ids[@]}" |tr -s '\n' ' '
for instance in ${instance_ids[*]};
do
echo "Executing $1 on instance: ${instance}"
aws ec2 $1-instances --instance-ids ${instance}
done
}
while getopts ":e:sf" opt; do
case $opt in
e)
environment=$OPTARG
;;
x)
getInstanceIdByEnvTag;
execInstancesByInstanceID "stop";
;;
s)
echo "starting nodes"
getInstanceIdByEnvTag;
execInstancesByInstanceID "start";
;;
f)
force=1
;;
\?)
echo "Invalid option: -$OPTARG" >&2
exit 1
;;
:)
echo "Option -$OPTARG requires an argument." >&2
exit 1
;;
esac
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment