Skip to content

Instantly share code, notes, and snippets.

@gerad
Created September 18, 2010 19:38
Show Gist options
  • Select an option

  • Save gerad/585980 to your computer and use it in GitHub Desktop.

Select an option

Save gerad/585980 to your computer and use it in GitHub Desktop.
easy ec2 ubuntu server creation
#! /usr/bin/env bash
if [ -z $1 ]; then
echo "ec2-ubuntu - easy ec2 ubuntu server creation"
echo "Usage: $0 instance-name [instance-type [ami [region]]]"
exit
fi
instancename=$1
instancetype=${2:-'t1.micro'}
ami=${3:-'ami-1234de7b'}
region=${4:-'us-east-1'}
function run() {
echo "$@"
eval "$@"
}
if [ ! -f $HOME/.ec2/$instancename.pem ]; then
run "ec2-add-keypair $instancename > $HOME/.ec2/$instancename.pem"
run chmod 0600 $HOME/.ec2/$instancename.pem
fi
echo "starting instance..."
run 'instance=`ec2-run-instances '"$ami"' --instance-type '"$instancetype"' --region '"$region"' --key '"$instancename"' | grep INSTANCE`'
iid=`echo $instance | cut -d' ' -f2`
status='pending'
while [ $status != 'running' ]; do
run 'description=`ec2-describe-instances '"$iid"' | grep INSTANCE`'
status=`echo "$description" | cut -f6`
echo "waiting for instance to boot (status: $status)..."
sleep 3
done
hostname=`echo "$description" | cut -f4`
while true; do
nc -z $hostname 22 && break
echo 'waiting for ssh to start...'
sleep 3
done
sleep 10 # ssh needs a bit of time to warm up
echo
echo "setting up ~/.ssh/config..."
run 'cat <<END >> ~/.ssh/config
Host '"$instancename"'
HostName '"$hostname"'
HostKeyAlias '"$hostname"'
User ubuntu
ForwardAgent yes
StrictHostKeyChecking no
IdentityFile ~/.ec2/'"$instancename.pem"'
END'
echo
# prepare it for micro
if [ $instancetype == 't1.micro' ]; then
run ssh $instancename "'"'sudo sed -i.dist "\,/dev/sda2,s,^,#," /etc/fstab'"'"
fi
echo "setup complete, to get started:"
echo " > ssh $instancename"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment