Minecraft Server on an Amazon EC2 Micro Instance

Over the past couple of weeks, I’ve been using my free Amazon EC2 Micro instance as a Minecraft server. It isn’t ideal, but it works well enough to replace the aging PC I was using before. Plus, I can feel safe allowing people onto the server, as before I would have to expose my home network to the Internet for the same experience.

UPDATE 2013-05-09: I’ve added a section on how to add swap space to EC2. This can alleviate problems encountered when more than a couple of players are on the server.

Setup

(This post assumes that you already have a running instance. If you are interested in setting up your own EC2 Instance from scratch, have a look at http://www.stratumsecurity.com/blog/2010/12/03/shearing-firesheep-with-the-cloud/. It also shows you how to setup an OpenVPN server.)

As it is my first Minecraft server, I decided to use the vanilla server from Mojang Specifications. It isn’t as feature-packed as others like HeyO, but it is relatively simple to get going. My instance (Ubuntu Server 10.04, ami-4a0df923 on EC2) didn’t have the Java Runtime Environment pre-installed, so I installed it using apt-get:

sudo apt-get install openjdk-6-jre-headless

I then proceeded to download the latest version of the server:

wget http://www.minecraft.net/download/minecraft_server.jar?v=1299034714859

At this point, I also made sure that GNU screen was installed so I could run minecraft-server in the background between SSH sessions.

sudo apt-get install screen

Since I am running the server in a low-memory environment, I adjusted the instructions given on the download page to suit and put it all in a small startup script, start.sh, which I placed in the same directory as minecraft-server.jar:

#!/bin/bash
# Minecraft Server startup script
java -Xmx500M -Xms500M -jar minecraft-server.jar nogui

This should allow the server to startup without error. Make sure that start.sh has executable permissions via chmod +x start.sh .

Now it’s time to start the server with the following commands, replacing /path/to/minecraft-server with whatever path you downloaded minecraft-server.jar to:

screen -DR
cd /path/to/minecraft-server
./start.sh

You should now be at the minecraft server console. For the final step, you need to adjust your Amazon EC2 Security Policy to allow TCP connections to port 25565. After that, you can finally fire up Minecraft, go to Multiplayer, and punch in your server’s public IP address.

Setting up Swap Space

If you run into performance issues when running the server, you can try adding some swap space to supplement the RAM. I found the following script to do just that (copy & paste it into a file called swap.sh):

#!/bin/bash -e

# Set default variable values
: ${SWAP_SIZE_MEGABYTES:=1024}
: ${SWAP_FILE_LOCATION:=/var/swap.space}

if (( SWAP_SIZE_MEGABYTES <= 0 )); then
    echo 'No swap size provided, exiting.'
    exit 1
elif [ -e "$SWAP_FILE_LOCATION" ]; then
    echo "$SWAP_FILE_LOCATION" already exists,  skipping.  
fi

if ! swapon -s | grep -qF "$SWAP_FILE_LOCATION"; then
    echo Creating "$SWAP_FILE_LOCATION", "$SWAP_SIZE_MEGABYTES"MB.
    dd if=/dev/zero of="$SWAP_FILE_LOCATION" bs=1024 \
        count=$(($SWAP_SIZE_MEGABYTES*1024))
    mkswap "$SWAP_FILE_LOCATION"    
    swapon "$SWAP_FILE_LOCATION"
    echo 'Swap status:'
    swapon -s
else
    echo Swap "$SWAP_FILE_LOCATION" file already on.
fi

echo 'Done.'

Next, run the following commands:

chmod a+x swap.sh # allows the script file to be executed as a program
sudo su # the script needs root priveledges so we need to switch users to root
./swap.sh # runs the script

That should give you an extra buffer of memory to work with.

Practicality

The free tier that Amazon provides is just enough bandwidth, CPU power, and memory for small groups of no more than four players. That means it’s only any good as a personal creative server that you can show off to friends every now and then. But hey, free is free, and if you follow the guide at Stratum Security, you’ll have a nice little OpenVPN server too.