Setting up an EC2 instance using VMWare

From KallestadWiki

Jump to: navigation, search

So I'm interested in using Amazon's EC2 service. I'm wary of using public Amazon Machine Instances (AMI's) as a base for a variety of probably not very good reasons, but they are my reasons.

The idea I had was to set up a fresh instance of my OS locally using VMWare, configure it according to my own needs, upload it, and start using EC2. Pretty simple in theory. In practice it's a whole different ball game. These are my notes on getting started.

Contents

Requirements

  • tar 1.15 - I am using CentOS 4.5, which comes with an older version of tar. Upgrading to the new tar is fairly easy, once you know how:
wget http://mirror.centos.org/centos/5/os/SRPMS/tar-1.15.1-23.0.1.el5.src.rpm
rpmbuild --rebuild tar-1.15.1-23.0.1.el5.src.rpm
rpm -Uhv /usr/src/redhat/RPMS/i386/tar-1.15.1-23.0.1.i386.rpm
  • Amazon Utilities
EC2 Tools
AMI Tools
  • Ruby (at least 1.85)
yum install ruby

but if you have CentOS 4.5 - you have to use the test repository by creating a file in /etc/yum.repos.d (file must end in .repo):

[c4-testing]
name=CentOS-4 Testing 
baseurl=http://dev.centos.org/centos/$releasever/testing/$basearch/
enabled=1
gpgcheck=1
gpgkey=http://dev.centos.org/centos/RPM-GPG-KEY-CentOS-testing

After running 'yum install ruby', you should change the "enabled" value to zero in that file.

  • Java

I'm not sure which java is required - it could be that the out of the box java version will work, but for my plans for this particular machine, Sun Java is required, so I'm going to go ahead and get that installed.

  • a boatload of disk space

Certificates

When you go to your amazon account, there is a link for "Access Identifiers". It took me a while to figure this out. You may have noticed that pretty much all of the ec2 utilities require a certificate and a private key. If you created your own, you have to upload them to this page. Otherwise, Amazon can generate them for you.

Make it Happen

So at this point, I have my base OS installed, I have the AMI utilities installed, and I have the EC2 tools installed. I also have java installed, my amazon keys ready, and a big giant chunk of disk space available on a samba mount.

I'm not quite sure what the deal is with this, but ec2-bundle-vol kept failing on me with dd errors. I would run the following command

ec2-bundle-vol -c mycert.pem -k mykey.pem --user myuser123 -e /mnt/mymountedshareddrive,/tmp

Oddly, it was creating a 10GB file successfully, but still I received the following error:

dd: advancing past xxx bytes in output file '/mnt/ec2/image': File too large
execution failed: "dd if=/dev/zero of=/mnt/ec2/image bs=1M count=1 seek=xxx"

I thought I might be hitting one of those 2GB barrier issues, so I tried with a -s parameter of 2048 and still received the error. I tried with a simple 1GB size (-s 1024 and it seems to be working) so...

 ec2-bundle-vol -c mycert.pem -k mykey.pem --user myuser123 -e /mnt/mymountedshareddrive,/tmp -s 1024

hmph. Still a problem. I think this might be due to a limitation with samba, so I've provisioned additional disk space directly from within vmware.

Funny - redhat/centos doesn't seem to contain much in the way of software to create and format partitions - at least not that I could find. I did have pretty handy an iso for The System Rescue CD, so I booted to that and added partitions and formatted them once I had allocated the space from within the VMWare configuration.

So far, this seems to have worked. A simple df -h showed me that my system took up about 2.9 gigs of space, so I set the destination image to 3172 megabytes (3GB). ect-bundle-vol did successfully create the image (woohoo!) and it also generated 93 10 Megabyte image.part files, a 4.2 Megabyte image.part file, and a 15k manifest. Before I upload this to S3 and try to boot it, I'm going to read a bit more of the documentation to ensure that I get this right the first time and I don't end up with an instance that I can't attach to.

For my base image, I'd like to have something that I can build from, so I'm also going to take some time and come up with a strategy that will be most effective and most economical for me. For instance - I know that I want an oracle database set up on some instances. It may make more sense to run the install from a live S3 instance, build a volume from there and save it to S3 rather than do the install locally. I would pay for a few hours of uptime on an instance, but doing the install locally, I'd end up having to pay for the data transfer to S3 in order to upload it.

Having the instance local however - I can snapshot it and run the install to ensure that I have all of the necessary prerequisites in place - as well as have the procedure laid out to follow so that my time spent on EC2 is most efficient. I know the last time I installed oracle it took a few days to get everything figured out. I'd hate to spend all that money on a live instance just figuring things out again.

NX Server

FreeNX is akin to vnc or Remote Desktop for remote access, although in my humble opinion it does a much better job than vnc. It's not open source, but it is free.

Installing FreeNX is pretty easy on CentOS/RHEL (as root):

yum install nx freenx
cd /etc/nxserver
cp node.conf.sample node.conf
then open node.conf and uncomment the line reading:
#ENABLE_PASSDB_AUTHENTICATION="1"

nxserver --adduser myuser

From there, it's just a matter of configuring your client to connect. Now that this is done, I know that I have access to the graphical environment once I upload the machine to the cloud. Not that I'll need to use it very often, but sometimes downloading software requires me to log in to a site and this can save me the headache of downloading it locally and then uploading it to my instance. Other things like user management and vpn connectivity can sometimes be easier via the gui.

Once this is done, I bundle my image as stated above:

ec2-bundle-vol -c mycert.pem -k mykey.pem --user myuser123 -e /mnt/mymountedshareddrive,/tmp -s 3450

Uploading to S3

This is my first time working with S3, so I downloaded S3Fox to create a bucket (folder in s3), then I ran the ec2-upload-bundle command:

ec2-upload-bundle -b myamibucket -m /mnt/d/image.manifest.xml -a myaccesskey -s mysecretkey

So far, it's uploading fairly quickly - and it's uploading the image.part files first. I'm curious if it might just leave the image file itself alone. That would be nice because at this point there are 95 image parts making my total image file just under a gig.

S3 access is as quick as you would think because as the image.part files are uploading I can see them in my S3 bucket.

Registering with Amazon

In order to run the api tools, you have to have your environment set up properly.

export EC2_HOME=/path/to/ec2_api_tools
export EC2_PRIVATE_KEY=/path/to/private_key.pem
export EC2_CERT=/path/to/cert.pem
export JAVA_HOME=/path/to/java
export JAVA_BIN=$JAVA_HOME/bin

After that, registering is relatively easy:

ec2-register s3bucket/image.manifest.xml

This will return an image ID for you. Make sure you keep that id stored somewhere. You won't be getting it again.

Running your instance

ec2-run-instances your instance image id

but make sure you know how to turn it off!!!

ec2-terminate-instances your instance id

In order to connect via ssh, you have to allow traffic on that port

ec2-authorize default -p 22

Your hostname is returned by running the command:

 ec2-describe-instances
 OR
 ec2-describe-instances imageid

Troubleshooting

If you are unable to connect to your instance, check the console output.

ec2-console instanceid

If you see the following line:

Bringing up interface eth0:  Device eth0 has different MAC address than expected, ignoring [FAILED] 

You need to edit your /etc/sysconfig/network-scripts/ifcfg-eth0 file and remove or comment out the line beginning with HWADDRESS. Rebundle, re-upload,re-register, and re-try.

Network Access

ifcfg-eth0 should read:

DEVICE=eth0
BOOTPROTO=dhcp
ONBOOT=yes
TYPE=Ethernet
USERCTL=yes
PEERDNS=yes
IPV6INIT=no
Personal tools