Last active
February 16, 2021 05:15
-
-
Save yudoufu/9be2f5a54b4e509c2df79db0c2c13f4b to your computer and use it in GitHub Desktop.
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 | |
set -e | |
usage() { | |
echo "USAGE: `basename $0` [Options]" | |
echo " ssm target selection using peco" | |
echo "" | |
echo "Optinal Arguments:" | |
echo " All arguments excluded options pass to initial queries of peco" | |
echo "" | |
echo "Options:" | |
echo " -h, --help show this help." | |
echo " -p, --profile select aws profile.(default: 'default')" | |
echo " -v, --verbose show detail commands." | |
echo " --dry dry run mode." | |
echo "" | |
echo "Dependent packages:" | |
echo " aws-cli, ssm-plugin, peco" | |
exit 1; | |
} | |
main() { | |
declare -a argv=() | |
while (( $# > 0 )); do | |
case $1 in | |
-h|--help) usage;; | |
-p|--profile) readonly profile=$2; shift;; | |
-v|--verbose) readonly is_verbose=1;; | |
--dry) readonly is_dry=1;; | |
-*) fatal "Unkown option: $1"; usage;; | |
*) argv=("${argv[@]}" "$1");; | |
esac | |
shift | |
done | |
set -- "${argv[@]}" | |
local query="$*" | |
local params="" | |
if [ "$profile" != "" ];then | |
params="$params --profile $profile" | |
fi | |
local instance=$(aws ec2 describe-instances --query 'sort_by(Reservations[].Instances[].{Id:InstanceId,Name:Tags[?Key==`Name`].Value|[0]},&Name)' --output text $params | peco --prompt="ssm profile:${profile:-default}>" --query="$query") | |
if [ "$instance" == "" ]; then | |
echo "Instance is not choosed." | |
exit 1 | |
fi | |
local instance_id=$(echo "$instance" | cut -f 1) | |
run aws ssm "$params" start-session --target "$instance_id" | |
} | |
run() { | |
if [ $is_dry ]; then | |
echo "[dry run] $@" | |
else | |
if [ $is_verbose ];then | |
echo "[run] $@" | |
fi | |
eval "$@" | |
fi | |
} | |
# call main. | |
main "$@" | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Install:
Usage:
For example:
Use default profile.
Use specific profile.
Set some initial query for selection.