How to deploy Erlang on Xen image on Amazon EC2

It is relatively easy to build a Xen image for your Erlang application using the build service. The fast track recipe hints that the process will not take longer than 5 minutes.

Confusion ensues when the build service completes successfully and spews a ‘vmling’ image. The image requires a complete Xen toolstack to boot. While many developers have a Xen-enabled system around, this is not at all a general case.

What we universally have is an access to the Amazom Compute Cloud (EC2) that luckily uses Xen as virtualisation technology. Another lucky coincidence is that a couple of years ago EC2 added support for custom kernels. The EC2 way with such kernels is chain-booting them using ‘pvgrub’. Those who are so inclined may find details on what is pvgrub and how it works elsewhere. We simply give a step-by-step guide that leads to an Amazon Machine Image (AMI) bootable on EC2:

1. Create a file named ‘sample.img’ of sufficient size and initialise it with a filesystem:

# truncate -s 8M sample.img
# mkfs.ext2 sample.img

2. Mount sample.img and create a directory structure inside:

# mkdir mnt
# mount sample.img mnt
# cd mnt
# mkdir boot
# mkdir boot/grub
# cd ..

3. Copy ‘vmling’ image and setup grub menu:

# cp /path/to/image/vmling mnt/boot
# cat > mnt/boot/grub/menu.lst
default 0
timeout 0
title My First Erlang on Xen AMI
	root (hd0)
	kernel /boot/vmling <your-arguments>

Among your arguments you may indicate which application to launch, e.g. ‘-s myapp’.

4. Unmount sample.img:

# sync
# umount mnt

5. Upload sample.img to Amazon S3:

# mkdir data
# ec2-bundle-image -c <your-ec2-certificate>.pem -k <your-ec2-key>.pem
    -u <your-ec2-account> -i sample.img -d data -r x86_64
# ec2-upload-bundle -b erlang-on-xen/sample-1 -m data/sample.img.manifest.xml
    -a <your-access-key-id> -s <your-secret-key>

I am glossing over details here. Please refer to the Amazon AWS documentation.

6. Register your new AMI:

Go to the Amazon AWS Management Console > EC2>AMIs > Register New AMI. Put ‘erlang-on-xen/sample-1/sample.img.manifest.xml’ into AMI manifest path field, fill in the name and description.

IMPORTANT. The kernel ID must be set to ‘aki-88aa75e1’. Alternatively, you may select this kernel ID when launching an instance.

You are done. Now you can use the web console or EC2 command line tools to launch instances that use your newly created AMI.

Beware that Amazon charges for a whole hour even if your instance has been running for a few minutes (or seconds). We can hope that Amazon switches to per second billing soon and opens the way for applications that use lightweight, short-lived Xen instances.

EDIT: The ling_builder plugin now can generate AMI directly. The generated images are kept at Amazon S3 in the erlang-on-xen bucket. They are publicly accessible and removed periodically. The described recipe is still valid for those who want to produce a permanent private AMI.

comments powered by Disqus