Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
creating an EBS AMI from a running Instance
# in web browser
# canonical karmic 64-bit AMI:
# ami-55739e3c
# canonical karmic 32-bit AMI:
# ami-bb709dd2
# Launch Instance > Community AMIs
# us-east-1b
# on client
# local settings from the instance launched above
host='' &&
keypair=`ls ~/.ec2/ec2-keypair.pem` # full path
# send your private key and certificate to the instance
rsync --rsh="ssh -i $keypair" --rsync-path="sudo rsync" ~/.ec2/{cert,pk}-*.pem ubuntu@$host:/mnt/
# connect to the instance
ssh -i "$keypair" ubuntu@$host
# on server
# add multiverse repositories (for Sun Java)
sudo perl -pi -e 's%(universe)$%$1 multiverse%' /etc/apt/sources.list
# add ec2-tools PPA to get the most recent version
echo "deb karmic main" | sudo tee /etc/apt/sources.list.d/ubuntu-on-ec2-ec2-tools.list &&
sudo apt-key adv --keyserver --recv-keys 9EE6D873
# update all packages and install ec2-api-tools
sudo aptitude update &&
sudo -E aptitude dist-upgrade -y &&
sudo -E aptitude install -y sun-java6-jre ec2-api-tools &&
sudo apt-get clean
# allow the ubuntu user to read the private key and certificate; set up environment variables
sudo chown ubuntu /mnt/*.pem &&
export EC2_CERT=$(echo /mnt/cert-*.pem) &&
export EC2_PRIVATE_KEY=$(echo /mnt/pk-*.pem)
# local settings from launching the instance above
region='us-east-1' &&
# create a new 15GB ebs volume
instanceid=$(wget -qO- http://instance-data/latest/meta-data/instance-id) &&
volumeid=$(ec2-create-volume --size 15 --availability-zone $zone | cut -f2)
# attach the ebs volume
ec2-attach-volume $volumeid --instance $instanceid --device /dev/sdh &&
while [ ! -e /dev/sdh ]; do echo -n .; sleep 1; done
# create an ext3 filesystem on the ebs volume and mount it
sudo mkfs.ext3 -F /dev/sdh &&
sudo mkdir /mnt/ebsimage &&
sudo mount /dev/sdh /mnt/ebsimage
# set up some system stuff on the ebs volume - not sure if all this is needed
sudo mkdir /mnt/ebsimage/mnt
sudo mkdir /mnt/ebsimage/proc
sudo mkdir /mnt/ebsimage/sys
sudo mkdir /mnt/ebsimage/dev
sudo mknod /mnt/ebsimage/dev/null c 1 3
sudo mknod /mnt/ebsimage/dev/zero c 1 5
sudo mknod /mnt/ebsimage/dev/tty c 5 0
sudo mknod /mnt/ebsimage/dev/console c 5 1
sudo ln -s null /mnt/ebsimage/dev/X0R
# copy the current instance onto the ebs volume
sudo rsync --stats -av --exclude=/sys --exclude=/mnt --exclude=/proc --exclude=/dev --exclude=/media --exclude=/tmp --exclude=/sys / /mnt/ebsimage
# unmount the ebs volume and detach it from the instance
sudo umount /mnt/ebsimage &&
ec2-detach-volume $volumeid --instance $instanceid
# create a snapshot from the ebs volume
snapshotid=$(ec2-create-snapshot --region $region $volumeid | cut -f2)
#ec2-delete-volume $volumeid
# wait for the snapshot to finish creating
while ec2-describe-snapshots "$snapshotid" | grep -q pending
do echo -n .; sleep 10; done
# register the snapshot as a new ec2 ami
now=$(date +%Y%m%d-%H%M)
description="Ubuntu $release $codename $tag $arch $now"
ebsid=$(ec2-register --region $region --snapshot "$snapshotid" --architecture $arch --kernel $kernelid --ramdisk $ramdiskid --block-device-mapping /dev/sdb=ephemeral0 --description "$description" --name "$prefix" --root-device-name /dev/sda1 | cut -f2)
# display the ids, for reference
cat <<EOF
ami-instance $instanceid
snapshot $snapshotid
ebs-instance $ebsid
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.