1+ #! /usr/bin/env sh
2+ # ####### Source ################################################################
3+ #
4+ # https://github.com/qoomon/aws-ssm-ec2-proxy-command
5+ #
6+ # ####### Usage #################################################################
7+ # https://github.com/qoomon/aws-ssm-ec2-proxy-command/blob/master/README.md
8+ #
9+ # Install Proxy Command
10+ # - Check Install Steps for aws-ssm-ec2-proxy-command.sh
11+ # - Move this script to ~/.ssh/aws-ssm-ec2-proxy-command-start-instance.sh
12+ # - Ensure it is executable (chmod +x ~/.ssh/aws-ssm-ec2-proxy-command.sh)
13+ # Add following SSH Config Entry to ~/.ssh/config
14+ # host i-* mi-*
15+ # IdentityFile ~/.ssh/id_rsa
16+ # ProxyCommand ~/.ssh/aws-ssm-ec2-proxy-command-start-instance-start.sh %h %r %p ~/.ssh/id_rsa.pub
17+ # StrictHostKeyChecking no
18+ #
19+ # Ensure SSM Permissions for Target Instance Profile
20+ # https://docs.aws.amazon.com/systems-manager/latest/userguide/setup-instance-profile.html
21+ #
22+ # Open SSH Connection
23+ # ssh <INSTANCE_USER>@<INSTANCE_ID>
24+ #
25+ # Ensure AWS CLI environment variables are set properly
26+ # e.g. AWS_PROFILE='default' ssh ec2-user@i-xxxxxxxxxxxxxxxx
27+ #
28+ # If default region does not match instance region you need to provide it like this
29+ # ssh <INSTANCE_USER>@<INSTANCE_ID>--<INSTANCE_REGION>
30+ #
31+ # ###############################################################################
32+ set -eu
33+
34+ REGION_SEPARATOR=' --'
35+ MAX_ITERATION=5
36+ SLEEP_DURATION=5
37+
38+ ec2_instance_id=" $1 "
39+ ssh_user=" $2 "
40+ ssh_port=" $3 "
41+ ssh_public_key_path=" $4 "
42+ ssh_public_key=" $( cat " ${ssh_public_key_path} " ) "
43+
44+
45+ if [[ " ${ec2_instance_id} " == * " ${REGION_SEPARATOR} " * ]]
46+ then
47+ export AWS_DEFAULT_REGION=" ${ec2_instance_id##* ${REGION_SEPARATOR} } "
48+ ec2_instance_id=" ${ec2_instance_id%% ${REGION_SEPARATOR} * } "
49+ fi
50+
51+ function start_instance(){
52+ # Instance is offline - start the instance
53+ > /dev/stderr echo " \n🚀 Starting ec2 Instance ${ec2_instance_id} "
54+ aws ec2 start-instances --instance-ids $ec2_instance_id --profile ${AWS_PROFILE} --region ${AWS_REGION}
55+ sleep ${SLEEP_DURATION}
56+ COUNT=0
57+ > /dev/stderr echo " ⏳ Wait until ${ec2_instance_id} is running"
58+ while [ ${COUNT} -le ${MAX_ITERATION} ]; do
59+ STATUS=` aws ssm describe-instance-information --filters Key=InstanceIds,Values=${ec2_instance_id} --output text --query ' InstanceInformationList[0].PingStatus' --profile ${AWS_PROFILE} --region ${AWS_REGION} `
60+ if [ ${STATUS} == ' Online' ]; then
61+ break
62+ fi
63+ # Max attempts reached, exit
64+ if [ ${COUNT} -eq ${MAX_ITERATION} ]; then
65+ exit 1
66+ else
67+ > /dev/stderr echo " ⁃ [${COUNT} |${MAX_ITERATION} ] - retry in ${SLEEP_DURATION} seconds"
68+ let COUNT=COUNT+1
69+ sleep ${SLEEP_DURATION}
70+ fi
71+ done
72+ }
73+
74+
75+ > /dev/stderr echo " ⚙️ Ec2 Proxy Command \n"
76+ > /dev/stderr echo " 🧪 Check if instance ${ec2_instance_id} is running"
77+ STATUS=` aws ssm describe-instance-information --filters Key=InstanceIds,Values=${ec2_instance_id} --output text --query ' InstanceInformationList[0].PingStatus' --profile ${AWS_PROFILE} --region ${AWS_REGION} `
78+
79+ # If the instance is online, start the session
80+ if [ $STATUS == ' Online' ]; then
81+ > /dev/stderr echo " − State: 🟢 ${STATUS} "
82+ ~ /.ssh/aws-ssm-ec2-proxy-command.sh $ec2_instance_id $ssh_user $ssh_port $ssh_public_key_path
83+ else
84+ > /dev/stderr echo " − State: 🔴 Offline"
85+ start_instance
86+ ~ /.ssh/aws-ssm-ec2-proxy-command.sh $ec2_instance_id $ssh_user $ssh_port $ssh_public_key_path
87+ fi
0 commit comments