Skip to content

Instantly share code, notes, and snippets.

@wata727
Created February 15, 2015 11:49
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wata727/9849b639551c0c176ba4 to your computer and use it in GitHub Desktop.
Save wata727/9849b639551c0c176ba4 to your computer and use it in GitHub Desktop.
EC2のNameタグ名でインスタンスの起動、停止を行う。aws-cliとjqを使用。
#!/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