Skip to content

Commit

Permalink
add notes around SSM transport mode + swap cli options to be less insane
Browse files Browse the repository at this point in the history
  • Loading branch information
redterror committed Aug 3, 2022
1 parent f1ef099 commit 011ea9a
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions bin/ssh-ec2-connect
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ set -eo pipefail
usage () {
cat <<EOSTR
ssh-ec2-connect [OPTIONS]
-e --instance-id=INSTANCE_ID EC2 Instance ID
-i --private-key=path/to/key SSH private key (optional)
-i --instance-id=INSTANCE_ID EC2 Instance ID
-k --private-key=path/to/key SSH private key (optional)
-p --public-key=path/to/key SSH public key (optional)
-r --region=us-tirefire-1 AWS Region
-s --ssm Use AWS SSM for transport
Expand All @@ -26,6 +26,15 @@ Omitting a private key assumes the user has a SSH agent operating.
Environment variables can be set for default keys:
\$SSH_EC2_CONNECT_PRIVATE_KEY
\$SSH_EC2_CONNECT_PUBLIC_KEY
SSM Transport mode expects SSH to be able to handle EC2 instance ID's as hostnames,
e.g. 'ssh i-1234567890'. The SSM docs at:
https://docs.aws.amazon.com/systems-manager/latest/userguide/session-manager-getting-started-enable-ssh-connections.html
Suggest the following in your ~/.ssh/config:
# SSH over Session Manager
Host i-* mi-*
ProxyCommand sh -c "aws ssm start-session --target %h --document-name AWS-StartSSHSession --parameters 'portNumber=%p'"
EOSTR
}

Expand All @@ -50,18 +59,18 @@ set -u
die() { echo "$*" >&2; exit 2; } # complain to STDERR and exit with error
needs_arg() { if [ -z "$OPTARG" ]; then die "No arg for --$OPT option"; fi; }

while getopts e:i:p:r:su:vh-: OPT; do
while getopts i:k:p:r:su:vh-: OPT; do
if [ "$OPT" = "-" ]; then # long option: reformulate OPT and OPTARG
OPT="${OPTARG%%=*}" # extract long option name
OPTARG="${OPTARG#$OPT}" # extract long option argument (may be empty)
OPTARG="${OPTARG#=}" # if long option argument, remove assigning `=`
fi

case "$OPT" in
e | instance-id)
i | instance-id)
needs_arg
INSTANCE_ID=$OPTARG ;;
i | private-key)
k | private-key)
needs_arg
PRIVATE_KEY=$OPTARG ;;
p | public-key)
Expand Down

0 comments on commit 011ea9a

Please sign in to comment.