Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.