Upgrading your Minecraft EC2 cloud server

Firstly, let me thank everyone who read my first two articles on setting up a free Minecraft server in the cloud with Amazon EC2. The posts have proved very popular and are currently accounting for about 80% of all traffic to this blog. So due to popular demand here is the third article in the series.

Since I last posted on this topic, Minecraft has gone “gold” and dropped the beta tag. This happened during “Minecon” in November. My son was one of the first in the world to download it since after the announcement and while we were watching the delegates rushing to the door over the streaming video, we already had it downloading in another window.  By the end of the day we had upgraded our cloud server and enjoying all the Minecraft 1.0 goodness.

Many readers have complained that the free Micro instance is laggy and only capable of supporting a handful of players at a time. This is true – but it is free! If you want more, you need to start paying. Upgrading to an EC2 “Small” instance server is the next step. This post will guide you through the upgrade process while minimising the cost.

First, let me make this perfectly clear, if you create a Small instance server on EC2, you will pay for it as it is not part of the “free tier”. It’s pretty cheap but not free. Ours is 9.5c per hour or US$2.28/day. That’s about US$70/month for a 24/7 server. That’s expensive but we are not going to build a 24/7 server, we will shutdown the server when its not in use – i.e. during school hours and for most of the night. If we limit little Johnnie or Jessie to 2 hours per day between homework and bedtime that works out to be only $5.77 / month. Add a few more hours on the weekend and we are probably up around $10/month – still cheaper than cable TV.

Check the pricing and make sure you understand what you are committing to. Don’t come running to me when you get your first $70 bill because you forgot to stop your instance.

We setup a 1.7GB small instance Minecraft server on EC2 with CraftBukkit 1.1 (a modified version of Minecraft) and it works very well.

1. Back up your server now

Unfortunately, it’s not possible to just “upgrade” an existing instance – you have to start afresh. A good approach is to build a completely new server from scratch with all the latest plugins and patches and once you are happy with it shutdown the old server and just leave it there for a while – just in case. If you forgot something, or just want to go back to your old world, you can. Amazon only charge for storage space for keeping stopped instances of servers and storage space is very cheap so you can afford to leave it there as a backup.

If you want to be really careful, now may be a good time to copy all the files from the old server into a folder on your local PC. The easiest way to do this is to use WinSCP (Windows) or Cyberduck (MAC). We won’t be using it for this exercise but it’s a good thing to do anyway.

2. Creating a small instance server

Essentially, you just need to follow the steps in part 1 of this series but instead of choosing a Micro Linux instance, choose a Small Linux instance. Follow the rest of the instructions and make sure you download the latest version of the Minecraft server code from here.

Once you have your server up and running, make sure it works OK by connecting to it from a client and checking out your new world as explained in part 2.

3. Tick tick, time is money. Scheduling automatic shutdowns.

We are running a Small Instance server so now you pay for every minute it is running. Move fast!

No, really stay calm. 9.5c per hour is not going to break anyone’s credit card balance – unless of course you forget to turn the server off! Remember shutting down an EC2 instance is different to just shutting down the Minecraft server that is running on it. Amazon will still charge you for a running server instance even if your Minecraft server is not running on it. The server instance must be stopped.

So the first thing we are going to setup is a job that automatically stops the instance at set times of the day – even if we forget to. Linux has a tool for this – called cron. Cron simply runs commands at a set time of day, day of week, day of month etc. We will use Cron to shutdown the server instance at a set time each evening. Say Johnny’s bedtime.

The command we need to issue is shutdown. The shutdown command does the obvious and shuts down the machine – this is the same as stopping an instance in EC2 speak. Pretty simple. The only fly in the ointment is that shutdown is a “protected” command. Only super users (administrators) can issue it. Even though you create this server and you started it, by default, you connect to it as a lowly “user” called ec2-user. This is for your own good. Being a super user on Linux is dangerous and should be avoided. Having said that, for our exercise we do not have much choice, so to become superuser enter the command

$ sudo su

Not much will happen except the prompt will change to a #. This is the standard way Linux uses to warn you are super user (and to be careful). The next thing we need to do is create a crontab. A crontab is just a text file with some entries to tell cron what jobs it has to do and when to run them. Enter the following command:

# nano crontab

This will open a text editor called Nano, ready for you to type in your commands. The format of the crontab is simple but important and must be followed exactly. Each line indicates one job and contains a set of fields. There must be at least one space between each field. The template format is shown below:

# Minute   Hour   Day of Month       Month          Day of Week        Command
# (0-59)  (0-23)     (1-31)    (1-12 or Jan-Dec)  (0-6 or Sun-Sat)

My table has two entries (one for school days) and one for the evenings:

55 21 * * SUN-THU /sbin/shutdown -P +5
55 10 * * * /sbin/shutdown -P +5

Cron reads this as “at 2155 hours on Sunday to Thursday call shutdown command and give users 5 minutes warning before shutting down”.

Be careful with the times – they are in UTC – London time. That’s because the EC2 servers always use UTC. You need to make your own adjustment for your local timezone. For me in Sydney, 2155 UTC equates to 9am (+1 day)  local time – when most kids would be at school. Thats why it says SUN-THU – allow for timezone adjustment and that means MON-FRI 9am. So at 9am each weekday, the server always shuts down. The other entry takes care of the local evening time – bed time.  1055 UTC equates to 11pm local Summer time or 10pm Winter time – either way – well after bedtime.

Type in your crontab and press Ctrl-O to write out the new crontab file. When you are returned to the prompt, type the following command to load the new crontab:

# crontab crontab

To confirm this worked, type this to see the active crontab:

# crontab -l

Now stop being super user by entering

# exit

The prompt should return to the $ sign.

If you can, make sure you are logged in the first time the job runs so you can see the 5 minute warning and watch the server shutting down using the EC2 console.

4. Starting an EC2 server from the desktop

The problem with shutting down a server every night is there is no simple way to automatically start it up again the next day. You could try having a cron job but cron does not run if the server is not running (of course).Worse, young kids have a habit of getting up really early on Saturday mornings… and the last thing you need is your Saturday sleep-in interrupted with pleadings to start the EC2 server.

My solution allows the child to start the server on demand by clicking an icon on their desktop. Some parents may decide this is a little too trusting given the costs involved in running the server but remember we are only talking 9.5c per hour and I don’t know about you, but my peaceful Saturday mornings are worth that.

Create a folder on your desktop called EC2.

Download the EC2 API command line tools from here. Extract them from the zip file and save the folder named ec2-api-tools-1.x.x.x in the EC2 folder.

Next we need our AWS public keys and certificates. Follow the instructions here and save the files in the EC2 folder you created.

Next we are going to create a simple script to start our EC2 server and associate it to the Elastic IP address.

On Mac:

Open text edit. Cut and paste the following lines:

#!/bin/bash
export JAVA_HOME=/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home
export EC2_HOME=$HOME/Desktop/EC2/ec2-api-tools-1.x.x.x
export PATH=$PATH:$EC2_HOME/bin
export EC2_PRIVATE_KEY=$EC2_HOME/pk-<your pk file>.pem
export EC2_CERT=$EC2_HOME/cert-<your cert file>.pem
export EC2_URL=https://<your EC2 host location server>
echo Starting instance...
ec2-start-instances <your instance>
echo Associating address...
ec2-associate-address <your servers IP address> -i <your instance>
echo Minecraft EC2 server started OK...

Save the file in the EC2 folder and call it startmcserver.sh. Run terminal, issue the following commands to make it executable:

$ cd EC2
$ chmod +x startmcserver.sh

To execute the script type

$ ./startmcserver.sh
On Windows:

Open Notepad. Cut and past the following lines:

@echo off
set JAVA_HOME="C:\Program Files\Java\jre6"
set EC2=%HOME%\Desktop\EC2
set EC2_HOME=%EC2%\ec2-api-tools-1.x.x.x
set PATH=%PATH%;%EC2_HOME%\bin
set EC2_PRIVATE_KEY=%EC2%\pk-<your pk name>.pem
set EC2_CERT=%EC2%\cert-<your cert name>.pem
set EC2_URL=https://<your ec2 host location>
echo Starting instance...
call ec2-start-instances <your instance>
echo Associating address...
call ec2-associate-address <your server's ip address> -i <your instance name>
echo Minecraft EC2 server started OK...
pause

Save the file in the EC2 folder as startmcserver.bat. Create a shortcut to it from your desktop. Double click it to execute.

Now your child can start the EC2 Minecraft server on demand and the server will keep running until the next time the cron job is invoked to shut it down. Worst case, the server is running for a few more hours than necessary; best case it isn’t started and there is no expense.

Send me a comment about how you go.

17 comments to Upgrading your Minecraft EC2 cloud server

  • Andrew

    Swap is turned off by default on micro instances.
    You will be better performance if you turn it on

    # 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

  • hollerith

    I’ve read a LOT of howtos. To distill something non-trivial like this to be completely idiot-proof, well this was the best of the lot. Great job. Thanks.

  • John

    If I knew how to script in bash I would write a cron job that runs every five mins and checks to see if there are any users on minecraft. I there were non on two consecutive checks, shutdown the instance. Unfortunately, I don’t know bash scripting, and I also can’t see a way of monitoring the usage of the server.

    I’d also like a web-based start script running on a permanently-running micro instance, that way any user could easily start the server without having to give out the keys.

    If this could be made to work, the server would only ever run when it was needed. You could still apply the bedtime shutdown of course!

  • Daryl

    Thanks for the tutorials!

    Two things I’ve discovered can incur charges:
    1) Elastic Block Storage I/Os – you get 2 million but I used around 3 in the first month (cost: $0.11/million)
    2) Unassigned Elastic IP – it appears that if your EIP is not connected to an instance it incurs $0.01/hour

    So $0.18 for the first month: not bad 😉

  • Thanks for a great tutorial! One question, when we stop the instance, do we need to shut down the Minecraft server first, and then restart it, or does that happen automatically? After restarting our EC2 server, we were unable to connect through Minecraft and are wondering if we did something wrong. WE just shut down the instance by choosing “Stop” in the AWS command center.

    Thanks!

    • Parker

      You need to shutdown your Minecraft server yourself before shutting down the AWS instance. Just type stop at the minecraft server prompt, wait for Minecraft to stop gracefully and then shutdown the AWS instance. AWS does not know anything or care about what you are running on your server so its up to you to take care of it.

      Cheers
      Ben

  • Ace tutorials, thank you. You might want to remind readers that in part 3 they can (should) increase the amount of memory given to the Minecraft server. (What amount do you recommend?)

    Regarding the auto-start batch file: how is the Minecraft server going to start itself? Or will it still be running from the previous “shutdown”?

  • One more thing (feel free to merge my comments) – one way to “upgrade” (without running through part 2 again) would be to “Create image (EBS AMI)” of the micro instance and re-launch it as a small instance.

  • zduba1

    I am using a Micro server, it’s great. Only problem is I get an error in PuTTY saying “server closed connection” or something, how do I use screen? I read that but could not figure it out.

  • Dave

    Fantastic tutorial. My nine year old wants me to set up a Minecraft server also. I was going to pick up a small base unit for this purpose, but will seriously consider doing it this way.

  • Great trio of posts. Thanks!

    I’m looking forward to part 4 — how to install and run plugins for your EC2 Minecraft server. 🙂

  • Dan

    This stuff is fantastic. I had no problems with starting and setting up the micro server but now I want to try out the small server. I’m having problems with the script
    What exactly is my “set EC2_URL=https://”
    And I’m getting errors with the following lines:
    call ec2-start-instances ??? i-cxxxxxx ????
    call ec2-associate-address -i
    ???? Elastic IP address ???? ??? Name I gave it, it has spaces in it. Problem? ???

    Was able to start the server with the terminal commands you gave for the micro server but my kids minecraft games can’t connect.

    Thanks for all the hard work.

    • Dan

      Figured out my problem. So no response necessary.

      To make things super easy, I added the execution string to get the minecraft server started, “sudo java …”, to .bash_profile so the minecraft server starts automatically when the ssh connection is made.
      Then I copied the starting ec2 file and changed some things in it to close down the instance.

      Now the kids can click on two shortcuts, to get the mc server going, then another to stop it.

  • Dan

    Okay, I can get my kids to join now…don’t know what happened.

    Now I would just love to get the script working so the kids can start up the server themselves but would really like directions to make a script for stopping the server for the kids.
    It this possible?

    Thanks again.

  • zelda2711

    Do you have to upgrade? Or can you keep it as a micro server?

  • @dan I wrote something doing just that. But I took a different approach by creating a web service making it easy to simply start a server, download the last world, and run it by pressing on button. Press that button again and the world will be saved – all with bukkit and plugins. My kid is using that while I type this and it works great. So if anyone is interested, visit my blog and write an email to the address in the about section.

    Cheers,
    Dirk

Leave a Reply

 

 

 

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>