Created
February 15, 2015 11:49
-
-
Save wata727/9849b639551c0c176ba4 to your computer and use it in GitHub Desktop.
EC2のNameタグ名でインスタンスの起動、停止を行う。aws-cliとjqを使用。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# example: sh instance-controler.sh [action] [name] | |
# action -> [start,stop] | |
# name -> ec2 tag name | |
if [ $# -ne 2 ]; then | |
echo "Error: Invalid argument count." 1>&2 | |
exit 1 | |
fi | |
action=$1 | |
name=$2 | |
id=`aws ec2 describe-instances \ | |
--filters "Name=tag-key,Values=Name" \ | |
--filters "Name=tag-value,Values=${name}" | jq -r '.Reservations[].Instances[].InstanceId'` | |
if [ ${id} ]; then | |
if [ ${action} = "start" ]; then | |
aws ec2 start-instances --instance-ids ${id} | |
elif [ ${action} = "stop" ]; then | |
aws ec2 stop-instances --instance-ids ${id} | |
else | |
echo "Error: Invalid action." 1>&2 | |
exit 1 | |
fi | |
else | |
echo "Error: ${name} instance not found." 1>&2 | |
exit 1 | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment