Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
creating an EBS AMI from a running Instance
# in web browser
# http://uec-images.ubuntu.com/releases/karmic/release/
# canonical karmic 64-bit AMI:
# ami-55739e3c
# canonical karmic 32-bit AMI:
# ami-bb709dd2
# https://console.aws.amazon.com/ec2/home#c=EC2&s=Instances
# Launch Instance > Community AMIs
# us-east-1b
# on client
# local settings from the instance launched above
host='YOURHOST.compute-1.amazonaws.com' &&
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 http://ppa.launchpad.net/ubuntu-on-ec2/ec2-tools/ubuntu karmic main" | sudo tee /etc/apt/sources.list.d/ubuntu-on-ec2-ec2-tools.list &&
sudo apt-key adv --keyserver keyserver.ubuntu.com --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' &&
zone='us-east-1b'
# 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
release=9.10
codename=karmic
tag=server
arch=x86_64
#arch=i386
now=$(date +%Y%m%d-%H%M)
prefix=ubuntu-$release-$codename-$tag-$arch-$now
description="Ubuntu $release $codename $tag $arch $now"
kernelid='aki-fd15f694'
#kernelid='aki-5f15f636'
ramdiskid='ari-c515f6ac'
#ramdiskid='ari-0915f660'
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
EOF
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment