Using a Ubuntu Image on EC2 with VNC

So I had to upload a bunch of files for the One Day On Earth project. Luckily I already had these files "in the cloud" (aka on a VM I have at fdcservers.net). Unfortunately this VM has only 256MB of ram, enough to start a desktop and even firefox, but not enough to run the stupid flash uploader as well.

My solution was to transfer them all to another VM instance in the cloud with more memory. It just so happens that Amazon has their EC2 instances which can be rented by the hour, perfect for this task that should take no more than a few minutes.

Starting the VM

Amazon has a very easy wizard for starting a VM, I used that. I selected a the official Ubuntu 12.04 instance (64-bit). I wanted a m1.medium size machine to make sure I had plenty of ram for the desktop and so I could load my videos into the ramdisk (/dev/shm). 

It defaulted to the usa-east region, but it shouldn't matter where you go. I also created a security group which only left open the port for SSH (port 22), as that was all I would need.

Keys for SSH

I had trouble with keys here. First, the VM doesn't allow you to login with a ssh password, you need to use key-based authentication. However the trick is that amazon inserts your public key into the VM at startup, so you need to make sure you have all this set before you start the VM. I didn't have it correct and had to build then shutdown a VM and re-try, since there's no way to update it once its started. There is a menu for administering your keys in the management console under Network & Security -> Key Pairs, but you can't access this during the VM creation wizard.

Since I already have a keypair generated on the laptop I'm using to login to the VM, I wanted to use that. I tried uploading my public key (.ssh/id_rsa.pub), but the fingerprint that amazon gave for it didn't match what I got with ssh-keygen -lf ~/.ssh/id_rsa.pub and when I tried to login it gave me a key failure. :-(

From previous work with EC2 windows machines I had one of the keys generated by amazon in the .pem format. I'd never used one of these with ssh before, but aparently its possible. I just had to select my .pem public key during the VM creation wizard. Then (once the machine was running) I needed to point ssh to my private .pem key.

ssh -i /path/to/my_key.pem ubuntu@ec2-XXX-XXX-XXX-XXX.compute-1.amazonaws.com
(Note the ubuntu@ in front of the server address, the default user is called "ubuntu")

Desktop and VNC

Now that I'm able to login, I just need to set it up so I can get a remote desktop and use firefox to upload the videos. 

sudo apt-get install ubuntu-desktop tightvncserver firefox flashplugin-installer

Edit ~/.vnc/xstartup to read (or copy it from conf/mcclure/dotdirvnc.xstartup:

#!/bin/sh

# Change "GNOME" to "KDE" for a KDE desktop, or "" for a generic desktop
MODE="GNOME"

#Uncommment this line if using Gnome and your keyboard mappings are incorrect.
#export XKL_XMODMAP_DISABLE=1

# Load X resources (if any)
if [ -e "$HOME/.Xresources" ]
then
xrdb "$HOME/.Xresources"
fi

# Try a GNOME session, or fall back to KDE
if [ "GNOME" = "$MODE" ]
then
if which gnome-session >/dev/null
then
echo starting gnome
gnome-session &
else
MODE="KDE"
fi
fi

# Try a KDE session, or fall back to generic
if [ "KDE" = "$MODE" ]
then
if which startkde >/dev/null
then
startkde &
else
MODE=""
fi
fi

# Run a generic session
if [ -z "$MODE" ]
then
xsetroot -solid "#DAB082"
x-terminal-emulator -geometry "80x24+10+10" -ls -title "$VNCDESKTOP Desktop" &
x-window-manager &
fi

Run:

tightvncserver -nolisten tcp -localhost -geometry 1024x786 :1
Then I had to connect to the server from my laptop with a VNC viewer tunneling over SSH:
ssh -f -L 25901:127.0.0.1:5901 $host sleep 60; xtightvncviewer 127.0.0.1:25901 
Finally, don't forget to turn off the EC2 instance when you're done...otherwise the bills will keep pileing up! Total cost for running one server (m1.medium) for one hour, approx $0.18.