Skip to content

Instantly share code, notes, and snippets.

@liangfu
Created January 30, 2023 21:24
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 liangfu/76a4c6b6622f60e469ade1d68f999ba5 to your computer and use it in GitHub Desktop.
Save liangfu/76a4c6b6622f60e469ade1d68f999ba5 to your computer and use it in GitHub Desktop.
Manage AWS EC2 instances with a single-line command
#!/bin/sh
ProgName=$(basename $0)
instance_ids=i-05806fb95d65fffff
region=us-west-2
sub_help(){
echo "Usage: $ProgName <subcommand> [options]\n"
echo "Subcommands:"
echo " start Start mldev instances"
echo " stop Stop mldev instances"
echo " describe Describe mldev instances"
echo " modify Modify mldev instance types"
echo ""
echo "For help with each subcommand run:"
echo "$ProgName <subcommand> -h|--help"
echo ""
}
sub_start(){
echo "Running 'start' command."
aws ec2 start-instances --instance-ids $instance_ids --region $region
}
sub_stop(){
echo "Running 'stop' command."
aws ec2 stop-instances --instance-ids $instance_ids --region $region
}
sub_describe(){
echo "Running 'describe' command."
aws ec2 describe-instances --instance-ids $instance_ids --region $region --query "Reservations[*].Instances[*].{PublicIP:PublicIpAddress,Name:Tags[?Key=='Name']|[0].Value,Status:State.Name,InstanceID:InstanceId,Instancetype:InstanceType}"
}
sub_modify(){
instance_type=$1
echo "Running 'modify $instance_type' command."
aws ec2 modify-instance-attribute --instance-id $instance_ids --instance-type "{\"Value\": \"$instance_type\"}"
}
subcommand=$1
case $subcommand in
"" | "-h" | "--help")
sub_help
;;
*)
shift
sub_${subcommand} $@
if [ $? = 127 ]; then
echo "Error: '$subcommand' is not a known subcommand." >&2
echo " Run '$ProgName --help' for a list of known subcommands." >&2
exit 1
fi
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment