Skip to content

Instantly share code, notes, and snippets.

@ento
Forked from andsens/benchmark-ec2-boottime.sh
Last active December 22, 2015 21:49
Show Gist options
  • Save ento/6535534 to your computer and use it in GitHub Desktop.
Save ento/6535534 to your computer and use it in GitHub Desktop.
#!/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="[email protected]"
instance_type="t1.micro"
subnet_id='subnet-123abc12'
login_user='root'
login_key='[email protected]'
start_time=`date +%s`
run_cmd="ec2-run-instances $ami_id \
--availability-zone $availability_zone \
--instance-type $instance_type \
--key $keypair \
"
if [[ ! -z "$subnet_id" ]]; then
run_cmd+=" --subnet $subnet_id "
fi
instance_id=`$run_cmd | 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" ]] && [[ ! "$instance_uri" =~ "compute.internal" ]]; 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 $login_user@$instance_uri -i $login_key -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"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment