Skip to content

Instantly share code, notes, and snippets.

@pgib
Created March 11, 2023 23:06
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 pgib/ed11edb4a48d69a40c12ef67ddf1da19 to your computer and use it in GitHub Desktop.
Save pgib/ed11edb4a48d69a40c12ef67ddf1da19 to your computer and use it in GitHub Desktop.
#!/bin/bash
# based on https://github.com/Skedulo/aws-instance-connect-proxy
set -eu
if [ $# -lt 2 ] ; then
echo "Usage: $0 [--profile profile] [--region region] [--key key] [--filter filterkey] [--value filtervalue] user host [port]"
exit
fi
while true; do
case $1 in
--profile)
PROFILE="--profile $2"
shift 2
;;
--region)
REGION="--region $2"
shift 2
;;
--key)
KEY=$2
shift 2
;;
--filter)
FILTER=$2
shift 2
;;
--value)
VALUE=$2
shift 2
;;
*)
break
;;
esac
done
if [ -z "${VALUE}" ]; then
exit 1
fi
PROFILE="${PROFILE:-}"
KEY="${KEY:-id_rsa}"
FILTER="${FILTER:-tag:aws:autoscaling:groupName}"
ARGS="--ssh-public-key file://~/.ssh/$KEY.pub --instance-os-user $1"
PORT="${3:-22}"
LOGIN=$1
read -r instance_id availability_zone private_ip_address <<< $(aws ec2 describe-instances $PROFILE $REGION --filter "Name=$FILTER,Values=$VALUE" 'Name=instance-state-name,Values=running' --query 'Reservations[*].Instances[*].[InstanceId,Placement.AvailabilityZone,PrivateIpAddress]' --output text | head -n 1)
aws ec2-instance-connect send-ssh-public-key $REGION $PROFILE --instance-id $instance_id --availability-zone $availability_zone $ARGS
echo "Connecting to ${private_ip_address}"
nc $private_ip_address $PORT
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment