public
Last active

Starts a single instance and measures the time until SSH connectivity

  • Download Gist
benchmark-ec2-boottime.sh
Shell
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
#!/bin/bash -e
# These need to be set.
#export EC2_HOME="/path/to/ec2-api-tools"
#export AWS_ACCESS_KEY='XXXXXXXXXXXXXXXXXXXX'
#export AWS_SECRET_KEY='XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
#export PATH="$PATH:${EC2_HOME}/bin"
 
ami_id='ami-123abc12'
availability_zone='eu-west-1a'
keypair="johndoe@example.com"
instance_type="t1.micro"
 
start_time=`date +%s`
instance_id=`ec2-run-instances "$ami_id" \
--availability-zone "$availability_zone" \
--instance-type "$instance_type" \
--key "$keypair" \
| grep 'INSTANCE' | awk '{print $2}'`
printf "Instance ID: %s\n" "$instance_id"
sleep 5
while [ 1 ]; do
instance_uri=`ec2-describe-instances --filter "instance-id=$instance_id" | grep "INSTANCE" | awk '{print $4}'`
if [[ -n "$instance_uri" && "$instance_uri" != 'pending' ]]; then
break
fi
sleep 1
done
if [[ ! "$instance_uri" =~ "compute.amazonaws.com" ]]; then
printf "'$instance_uri' does not look like an instance URI\n"
exit 1
fi
printf "Instance URI: %s\n" "$instance_uri"
 
ssh_cmd="ssh root@$instance_uri -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`
ec2-terminate-instances "$instance_id" > /dev/null
bootup_time="$(($end_time - $start_time))"
printf "Bootup Time: %i seconds\n" "$bootup_time"

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.