Friday, December 28, 2012

Setting up a Minecraft Server in EC2 by hand

Like many pre-teens in 2012, my oldest son spends most of his free-time playing Minecraft.   He is much more interested in creative mode servers and has invested lots of time building unique spaces on someone else's world.  He now has many ideas which he can only implement through his own server.  I decided to use this both as an opportunity for me to get some more hand-on-keyboard experience with EC2 and to give my son an introduction to systems beyond Scratch.

I started by creating a Micro instance (since they are free for the first year), copying the minecraft jar to that instance and then starting the server.   I gave Cameron the ssh key and created a bash alias so that he could connect and start the server whenever he wanted.  He uploaded an interesting world, added a few plugins and shared the address with some friends.  Incredibly simple! 

With this simple setup, though, whenever the ssh connection died, so did the server.  This annoyed Cameron, especially as there seems to be a timeout so that after a period of inactivity the connection is automatically killed.  Cameron did a quick search and found an O/S level configuration setting that he thought would solve the problem.  We applied the change, rebooted and ... all ssh connections were rejected.  Whoops!  Now there is no way to connect to the machine at all, so we have to start over.

This time, I'm being a little more regimented so I can get as close to a push-button set-up as possible.

If you are unfamiliar with Amazon Web Services, the first thing needed is an Amazon account that can log into the AWS Management Console and is signed up for EC2.  Ben Garton has a nice series of blog posts describing in detail all the steps from creating your first EC2 instance via the web interface through starting up the minecraft server.   I followed his procedures to ensure the manual process works before moving on to automation.

I want to have a relatively easy-to-remember name for the machine so I created a (free) account at no-ip.com and added a host mapping my chosen domain name to the IP address Amazon provided:



Now, I can give my son (and his friends) that name (mctesting.no-ip.org) rather than either the IP address or the 42-character unmemorable name that Amazon provides.

Although Putty is great for Windows, I switch among a Windows PC, a Mac laptop and a Linux desktop so I prefer a common command-line experience.  For the Windows PC, I have installed openSSH under Cygwin which provides a close enough match for most purposes.

For ease of access to the EC2 instance, I stored the Amazon-provided keypair (from step 3e of Ben's first post) in my ~/.ssh directory as EC2Minecraftserver.pem.  At this point I can connect to the EC2 instance via the following command line statement:

ssh -i ~/.ssh/EC2Minecraftserver.pem -l ec2-user mctesting.no-ip.org

Given that this is a lot to type, I updated my ssh config file with the following stanza:

# contents of $HOME/.ssh/config
Host mctest
Hostname mctesting.no-ip.org 
User ec2-user 
IdentityFile ~/.ssh/EC2Minecraftserver.pem

so now I can connect to the instance with the much shorter:

ssh mctest


Craftbukkit

Ben Garton's instructions lead us to install a vanilla minecraft server.  Most serious pre-teens apparently want to be able to incorporate many interesting plugins that have been built for minecraft.  These often require the Bukkit server framework, which means we need a different jar file.  There is both a recommended build and a beta build available from the Bukkit download page.  Although I would usually suggest using the recommended build, as of this writing (12/28/2012) I think most people will need the beta build as a recent update to the minecraft client makes it incompatible with the recommended build.

After downloading the build, I get a file named something like

craftbukkit-some_version.jar

The beta build I downloaded was craftbukkit-1.4.6-R0.1.jar.  I now need to get that file from my machine to the EC2 instance.  For that, I use the secure copy program, scp.   If I hadn't created my ssh config file, I'd have to enter all of this:

scp -i ~/.ssh/EC2Minecraftserver.pem craftbukkit-1.4.6-R0.1.jar ec2-user@mctesting.no-ip.org:.

With the ssh config file, it's just:

scp craftbukkit-1.4.6-R0.1.jar mctest:.

To get the craftbukkit server setup running we need to log into the EC2 instance, create a directory to house the jar file (and the various files that will be created when the server runs) and then start the server the first time:

ssh mctest
mkdir CraftServer
mv craftbukkit-1.4.6-R0.1.jar CraftServer
cd CraftServer
java -Xms512M -Xmx512M -jar craftbukkit-1.4.6-R0.1.jar -o true

In addition to all of the normal looking messages about preparing the world and spawn area there are also a number not so happy messages because the system couldn't allocate enough memory.   That means I need to up the heap size java provides for the craftbukkit server.  Unfortunately, the micro instance server doesn't have much memory.  When I run:

free -m

I see that there is only 594M total available.  With no swap space allocated, that means I can't increase the heap size much beyond the 512M on the original line.  Let's add 1024M of swap:

sudo /bin/dd if=/dev/zero of=/var/swap.1 bs=1M count=1024
sudo /sbin/mkswap /var/swap.1
sudo /sbin/swapon /var/swap.1

 When I restart the server with a 1024M heap:

java -Xms1024M -Xmx1024M -jar craftbukkit-1.4.6-R0.1.jar -o true

the server starts successfully with no errors.  Success!

Warning:  If you activate swap, you will likely incur some small charges for EBS I/O requests.  On a day that my son spent most of the day starting and stopping the server (which generates a tremendous number of I/O requests), the charge was 75 cents.  You can continuously monitor what your server is costing (if anything) via the AWS activity report.


3 comments:

  1. I was hoping that I could get the question answered "Can I run a minecraft server for/with my sons on an EC2 micro instance?" Thank you for the answer!

    Now I'll have to read on in your blog to see if it works ok for 1, 2? 3 connections? I'll keep reading.

    ReplyDelete
  2. There is a good tutorial on how to set one up here:

    http://www.blog.gartonhill.com/setting-up-a-free-minecraft-server-in-the-cloud-part-1/

    It's a little outdated, but I was able to follow it and set a small server up tonight in about an hour.

    I too am sad that the server drops connection when I stop my SSH connection. The person playing on the server is in a different city, and I'd like to know if there's any way for me to keep the server running without needing to be constantly connected.

    ReplyDelete