Deploying a Minecraft Server to EC2 (with some cost analysis)

Caveat… this is likely not the option you want to pursue for a small to medium scale Minecraft server… Lightsail will be more cost-effective due to the amount of data transfer involved. Also, this is partly an exercise it navigating Amazon Linux as a largely Ubuntu user.

Purchase a Domain

I’m going through Namecheap for a .online domain because… well… it’s cheap… and registering revrestfarcenim.online (.online domain through Route 53 would be $39 vs. $1.88 for the first year with Namecheap).

Launch an EC2 instance

If you’re trying to use free-tier resources for this, you’ll want to go for a t2.micro, but you’ll also need to modify the java parameters for the server to fit within those memory limits.

  • Go to Services -> EC2
  • Scroll down in the main window to “Launch instance” and click [Launch Instance]
  • Select Amazon Linux 2 AMI (should be the first option)
  • I’m selecting t3a.small for this to be similar in “virtual” resources as the $10-15/mo virtual server hosting providers (including Amazon Lightsail).
  • Click [Next: Configure Instance Details]
  • You’ll get a default vpc and subnet created and selected… if you don’t want to use these, you can click the available links to create new ones.
  • For “Auto-assign Public IP”, I have “Use subnet setting (Enable)” because I’m going to want to have this publicly accessible.
  • Scroll to near the bottom of the “Step 3” form and find “T2/T3 Unlimited” and uncheck “Enable” unless you want to run up a bill because you forgot about the Minecraft server.
  • Click [Next: Add Storage] to configure your space.
  • Click [Next: Add Tags]
  • Click [Next: Configure Security Group]
  • Click [Add Rule] and add a Custom TCP Rule that allows traffic to port 25565 (the port for Minecraft) from 0.0.0.0/0
  • Click [Review and Launch]
  • Click [Launch] and choose “Create a new key pair” named “minecraftserver”
  • Click on Instances and select your new instance, noting the IPv4 Public IP in the instance details below (leave tab open for reference in the next section)

Create a Hosted Zone in Route 53

  • In a new tab, go to Services -> Route 53 -> Hosted Zones
  • Click [Create hosted zone]
  • Type in your domain name for your server and select “Public hosted zone”
  • Copy the values for the NS record and populate those as the nameservers for your domain (for me, this is on Namecheap
Route 53 NS record
Namecheap Custom DNS settings
  • Now go back to Route 53 and [Create Record]
  • Choose “Simple routing”
  • Click [Define simple record]
  • Leave the record name blank.
  • Under “Value/Route traffic to” select “IP address or another value depending on the record type” and paste your IP in.
  • Be sure “Record type” is “A” and click [Define simple record]
  • Click [Create records] on the “Configure records” screen.

Install and setup Minecraft

  • Using your keypair from instance creation, ssh into your your instance
chmod 400 minecraftserver.pem # or whatever the filename is
ssh -i minecraftserver.pem ec2-user@revrestfarcenim.online
  • Create minecraft server folder and user
sudo mkdir /srv/minecraft-server # assuming EBS mount
sudo adduser --system --home /srv/minecraft-server minecraft
sudo chown minecraft.minecraft /srv/minecraft-service
  • Do updates and install java
sudo yum update
sudo amazon-linux-extras install java-openjdk11
  • Download minecraft server, run for the first time, and set the eula.txt
sudo -u minecraft wget https://launcher.mojang.com/v1/objects/a412fd69db1f81db3f511c1463fd304675244077/server.jar
sudo -u minecraft java -Xmx1024M -Xms1024M -jar server.jar nogui
# you'll get an error, so edit the eula.txt
sudo -u minecraft nano eula.txt # or vi, just set to eula=true
# run minecraft again and try to connect to server at revrestfarcenim.online

Making Minecraft a service

  • Run sudo nano /lib/systemd/minecraft-server.service
  • Paste a config similar to:
[Unit] 
Description=start and stop the minecraft-server 

[Service]
WorkingDirectory=/srv/minecraft-server
User=minecraft
Group=minecraft
Restart=on-failure
RestartSec=20 5
ExecStart=/usr/bin/java -Xms1024M -Xmx1024M -jar server.jar nogui

[Install]
WantedBy=multi-user.target
  • Now, enable the service with sudo systemctl enable /lib/systemd/minecraft-server.service
  • And start the service with sudo systemctl start minecraft-server.service
  • As it’s starting, you can check the status with sudo systemctl status minecraft-server.service.
  • With this systemctl setup, you should also be able to reboot the instance and have the Minecraft server come back up

Troubleshooting

  • If you don’t go with the default gateway and subnets created with the instance, you may find yourself having to explicitly set up an Internet Gateway and a Route Table (0.0.0.0/0 to your igw)
  • Make sure the associated security group is allowing port 25565 to connect (especially if SSH is working)

Teardown

Be sure to delete your hosted zone (you’ll need to delete the A record before deleting the zone) and terminate your instance to avoid running up charges for things you’re not using. I deleted the VPC as well just to avoid clutter and half-baked subnets and security groups, but that’s only because I have nothing of long-term value in the account.

Cost Analysis

There are multiple hits that you’re going to take by hosting this on EC2:

  • egress costs (9¢ per GB after your first GB): In my limited tests, the Minecraft worlds we started up required 100-200MB to initially download per session. It’s unclear if that’s the case for every session, but if you have 20 of your friends use a server, that might be 9¢ x 0.20 GB x 20 for one session each… 36¢ per average number of sessions… that could add up quickly. By contrast, you could get a different hosting provider (including Lightsail) to bundle 2TB of transfer instead.
  • hosted zone cost (50¢ for a distinct domain’s hosted zone)
  • If you use a separate EBS volume, minimum cost there is 80¢.
  • t3a.small cost is 1.88¢ per hour or $13.53/month… you could have the server shut down during off-hours, but then you’re not comparing to “always-on” options.