Created
March 23, 2023 10:26
-
-
Save shishi/f17718c4eb246e0c8084770497e623d2 to your computer and use it in GitHub Desktop.
ssm script with fzf
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 fzf" | |
echo "" | |
echo "Optinal Arguments:" | |
echo " All arguments excluded options pass to initial queries of fzf" | |
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, fzf" | |
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 | fzf --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