Skip to content

Instantly share code, notes, and snippets.

@hekki

hekki/awssh Secret

Last active January 13, 2021 14:53
Show Gist options
  • Save hekki/33a36952142667d9eb43c0e49ee9f578 to your computer and use it in GitHub Desktop.
Save hekki/33a36952142667d9eb43c0e49ee9f578 to your computer and use it in GitHub Desktop.
aws ssm start-session helper
#!/bin/bash
PROGNAME="$( basename $0 )"
function usage() {
cat << EOS >&2
Usage: ${PROGNAME} [-h] [--profile VALUE]
aws ssm start-session helper
Options:
--profile AWS profile
-h, --help Show usage.
EOS
exit 1
}
for OPT in "$@"; do
case $OPT in
'--profile' )
if [[ -z "$2" ]] || [[ "$2" =~ ^-+ ]]; then
echo "${PROGNAME}: option requires an argument -- $( echo $1 | sed 's/^-*//' )" 1>&2
exit 1
fi
PROFILE="$2"
shift 2
;;
'-h' | '--help' )
usage
;;
esac
done
if [ -z "$PROFILE" ]; then
usage
fi
INSTANCE_ID=$(aws ec2 describe-instances --filter "Name=instance-state-name,Values=running" --query 'Reservations[].Instances[].{InstanceId: InstanceId,Name: Tags[?Key==`Name`]|[0].Value,InstanceType: InstanceType,LaunchTime: LaunchTime}' --output json --profile "$PROFILE" | jq -c '.[]' | peco | jq -r .InstanceId)
aws ssm start-session --target $INSTANCE_ID --profile "$PROFILE"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment