Skip to content

Commit 41c2ee1

Browse files
authored
Update aws-ssm-ec2-proxy-command-start-instance.sh
1 parent bf5a9b2 commit 41c2ee1

File tree

1 file changed

+27
-36
lines changed

1 file changed

+27
-36
lines changed

aws-ssm-ec2-proxy-command-start-instance.sh

+27-36
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@
3232
set -eu
3333

3434
REGION_SEPARATOR='--'
35-
MAX_ITERATION=5
36-
SLEEP_DURATION=5
35+
START_INSTANCE_TIMEOUT=300
36+
START_INSTANCE_CHECK_INTERVAL=5
3737

3838
ec2_instance_id="$1"
3939
ssh_user="$2"
@@ -48,40 +48,31 @@ then
4848
ec2_instance_id="${ec2_instance_id%%${REGION_SEPARATOR}*}"
4949
fi
5050

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-
}
51+
>/dev/stderr echo "EC2 Proxy Command..."
7352

74-
75-
>/dev/stderr echo "⚙️ Ec2 Proxy Command \n"
76-
>/dev/stderr echo "🧪 Check if instance ${ec2_instance_id} is running"
7753
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
54+
if [ $STATUS != 'Online' ]
55+
then
56+
aws ec2 start-instances --instance-ids "${ec2_instance_id}" --profile "${AWS_PROFILE}" --region "${AWS_REGION}"
57+
>/dev/stderr echo "Waiting for EC2 Instance ${ec2_instance_id}..."
58+
START_INSTANCE_START="$(date +%s)"
59+
while [ $(( $(date +%s) - $START_INSTANCE_START)) -le ${START_INSTANCE_TIMEOUT} ]
60+
do
61+
>/dev/stderr echo -n "."
62+
sleep ${START_INSTANCE_CHECK_INTERVAL}
63+
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}`
64+
if [ ${STATUS} == 'Online' ]
65+
then
66+
break
67+
fi
68+
done
69+
>/dev/stderr echo
70+
71+
if [ $STATUS != 'Online' ]
72+
then
73+
>/dev/stderr echo "Timeout."
74+
exit 1
75+
fi
8776
fi
77+
78+
~/.ssh/aws-ssm-ec2-proxy-command.sh $ec2_instance_id $ssh_user $ssh_port $ssh_public_key_path

0 commit comments

Comments
 (0)