Skip to content

Instantly share code, notes, and snippets.

@mrysav
Forked from andsens/benchmark-ec2-boottime.sh
Last active May 20, 2019 21:43
Show Gist options
  • Save mrysav/3e5cc9b8846a9c8941a8 to your computer and use it in GitHub Desktop.
Save mrysav/3e5cc9b8846a9c8941a8 to your computer and use it in GitHub Desktop.
Starts a single instance and measures the time until SSH connectivity
#!/bin/bash -e
# Make sure awscli is configured correctly ("aws configure")
ami_id='ami-60b6c60a' # amazon linux
availability_zone='us-east-1b'
keypair="KEYPAIR_NAME"
keypath="PATH_TO_PEM_FILE"
instance_type="t2.micro"
sg_id="sg-YOURS"
subnet_id="subnet-YOURS"
start_time=`date +%s`
instance_id=`aws ec2 run-instances --image-id "$ami_id" \
--placement AvailabilityZone="$availability_zone" \
--instance-type "$instance_type" \
--key "$keypair" \
--security-group-ids $sg_id --subnet-id $subnet_id \
| grep 'InstanceId' | awk '{print $2}' | sed 's/"//g' | sed 's/,//g'`
printf "Instance ID: %s\n" "$instance_id"
sleep 5
while [ 1 ]; do
instance_uri=`aws ec2 describe-instances --instance-ids "$instance_id" | grep "PublicIpAddress" | awk '{print $2}' | sed 's/"//g' | sed 's/,//g'`
if [[ -n "$instance_uri" && "$instance_uri" != 'pending' ]]; then
break
fi
sleep 1
done
printf "Instance URI: %s\n" "$instance_uri"
ssh_cmd="ssh ec2-user@$instance_uri -i $keypath -o StrictHostKeyChecking=no -o ConnectTimeout=1 echo 'test' 2>/dev/null || true"
while [ -z "$cmd_status" ]; do
printf '.'
sleep 1
cmd_status=`eval $ssh_cmd`
done
end_time=`date +%s`
aws ec2 terminate-instances --instance-ids "$instance_id" > /dev/null
bootup_time="$(($end_time - $start_time))"
printf "Bootup Time: %i seconds\n" "$bootup_time"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment