AWS for Games Blog

Setting up a Minecraft Java server on Amazon EC2

Introduction

This blog covers how to deploy your own personal Minecraft Java server on AWS. Hosting your server on AWS can eliminate common networking challenges and security concerns associated with at-home servers. Because you have control over the virtual machine, you can configure any mods or plugins that you want. We will use Amazon Elastic Compute Cloud (EC2) to run a Minecraft server for you and your friends. We will not cover cost optimization in this post, but there are many ways to reduce costs for your server.

Solution Overview

We will use Amazon EC2 to launch a virtual machine for our Minecraft Server. EC2 grants you control over your network configurations and allows you to choose from hundreds of EC2 instance types with various CPU and RAM options. This flexibility allows us to host servers which support both small and large playerbases.

The virtual machine will be launched in an Amazon Virtual Private Cloud (VPC) which is a virtual private network on the AWS cloud. Additionally, we will protect our EC2 instance with a security group. Security groups are virtual firewalls which will allow us to control access to our EC2 instance. To make server set up easier we have developed a bash script to help us install the required software and run the server.

Minecraft Blog Post Diagram

Walkthrough

Prerequisites

Steps

  • Setting up your EC2 instance
  • Automating Minecraft server set up with a custom script
  • Dedicating an IP address for your Minecraft server
  • Setting up server operators, changing world files, and more
  • Clean up

Setting up your EC2 instance

In order to run our Minecraft Java server, we need to create a virtual machine using Amazon EC2.

  1. Log into the AWS Console.
  2. In the top right of the console, select the AWS region appropriate for your players’ location (For example, if the playerbase is on the US West Coast, select one of the us-west regions).
  3. Search for “EC2” and go to the EC2 dashboard. Press Launch Instance.
  4. Configure the correct settings for the EC2 instance here. Give the instance a name, such as “Minecraft Server”. Select the first Amazon Linux AMI and change the architecture to 64-bit (Arm). We used Amazon Linux 2023 AMI.
  5. Under Instance type, select t4g.small. If you end up having more players than your server can handle, you can always upgrade your server to a bigger EC2 instance.
  6. Under Key pair name press Create a new key pair. Enter a key name and press Create key pair. The private key will be automatically downloaded.

Player accesses the Minecraft server running on an EC2 instance. The EC2 instance is placed in a VPC with a security group allowing traffic over port 25565.

  1. Scroll to the Network settings section. Click the Edit button on the top right. This is where we will create a security group to secure our EC2 instance and prevent insecure connections. Select the default VPC, leave the subnet as no preference, and enable Auto-assign public IP. The default VPC has one public subnet in each Availability Zone so it can be chosen without any changes.

Click edit under the Network settings in the EC2 launch screen.

  1. Under Firewall, select Create security group. Change Security Group name to: “Minecraft Security Group”. Optionally add a description such as “Security group with ports open for Minecraft Server & SSH”.

Create a Firewall (security group) in your EC2 instance configuration.

We will now add two inbound rules to allow connectivity. EC2 Instance Connect is a way to securely connect to your EC2 instance from the AWS console. In order to connect to your EC2 instance, you need to allow SSH traffic from the EC2 Instance Connect IP addresses on AWS. The IP address ranges for EC2 Instance Connect are dependent on the AWS region your EC2 instance is in. Here are some common US IP ranges for EC2 Instance Connect.

IP CIDR Ranges
us-east-1: 18.206.107.24/29
us-east-2: 3.16.146.0/29
us-west-1: 13.52.6.112/29
us-west-2: 18.237.140.160/29

If the region you are launching your EC2 instance is not listed above, you can find the IP range in the AWS IP address documentation. Download the JSON file and open it in a browser. Display it as raw data, and then use “Ctrl+F” or “Command + F” to search text in document. Type “EC2_INSTANCE_CONNECT” in your search bar, and find the Instance Connect IP for your region.

  1. Head to Inbound security group rules. Under the default SSH traffic rule change the source type to Custom. Input the IP address range for the region you are using into the Source field. See the screenshot below for an example using the us-east-1 IP.

Add the EC2 instance connect IP CIDR to your inbound security group as a custom ssh rule.

  1. Add another inbound rule on your security group to allow TCP traffic from anywhere (0.0.0.0/0) for port 25565. This will allow Minecraft players to join your server.

Your Networking settings should look like this (we used the us-east-1 region in this example).

Networking settings in the security group show SSH traffic allowed for the specific EC2 Instance Connect for us-east-1 region. There is also a TCP rule that allows all traffic from port 25565.

  1. Move on to the storage section in EC2 set up. Select 8GB for root volume EBS. This is how much storage your EC2 instance has associated with it. You can add more storage if necessary but 8GB will be enough to get started. Free tier eligible customers can get up to 30GB SSD on EBS for free.

Configure User Data to Automatically Download and setup Minecraft

Now you will add a bash script to automatically download, configure, and run our Minecraft server every time we start our EC2 instance.

IMPORTANT: The script automatically signs the End User License Agreement (EULA) with Minecraft for you. This is necessary to be able to run the Minecraft server jar file and is normally done by the user. By using this script, you are agreeing to the EULA to run a Minecraft server.

  1. Scroll all the way to the bottom of the page, find the Advanced Details section, and expand it. Scroll to the bottom of the page again and find the User data field.
  2. You should now be at the User data field in the EC2 instance set up page. Find the script below in the “Automating Minecraft Set Up Script” section, and copy and paste the entire script into the User data section.

Automating Minecraft Set Up Script

#!/bin/bash

# *** INSERT SERVER DOWNLOAD URL BELOW ***
# Do not add any spaces between your link and the "=", otherwise it won't work. EG: MINECRAFTSERVERURL=https://urlexample


MINECRAFTSERVERURL=


# Download Java
sudo yum install -y java-21-amazon-corretto-headless
# Install MC Java server in a directory we create
adduser minecraft
mkdir /opt/minecraft/
mkdir /opt/minecraft/server/
cd /opt/minecraft/server

# Download server jar file from Minecraft official website
wget $MINECRAFTSERVERURL

# Generate Minecraft server files and create script
chown -R minecraft:minecraft /opt/minecraft/
java -Xmx1300M -Xms1300M -jar server.jar nogui
sleep 40
sed -i 's/false/true/p' eula.txt
touch start
printf '#!/bin/bash\njava -Xmx1300M -Xms1300M -jar server.jar nogui\n' >> start
chmod +x start
sleep 1
touch stop
printf '#!/bin/bash\nkill -9 $(ps -ef | pgrep -f "java")' >> stop
chmod +x stop
sleep 1

# Create SystemD Script to run Minecraft server jar on reboot
cd /etc/systemd/system/
touch minecraft.service
printf '[Unit]\nDescription=Minecraft Server on start up\nWants=network-online.target\n[Service]\nUser=minecraft\nWorkingDirectory=/opt/minecraft/server\nExecStart=/opt/minecraft/server/start\nStandardInput=null\n[Install]\nWantedBy=multi-user.target' >> minecraft.service
sudo systemctl daemon-reload
sudo systemctl enable minecraft.service
sudo systemctl start minecraft.service

# End script
  1.  Now you need to add a link to the script in order for it to download the Minecraft server file. To run the most updated version of Minecraft server, go to the official Minecraft Server download page. Find where it says “Download minecraft_server.version.jar and run it with the following command:”. Right click on the hyperlink Minecraft_server.jar and copy the link. If this link is unavailable then you can use any other download link for the Minecraft server.jar file for the correct version you are playing. The script will work as long as the URL is a valid link to the server.jar file.

The image below shows how to get the server jar file download link from the official Minecraft site.

Right click on the Minecraft server jar file download link to copy the link.

  1. With the link you copied from the previous step, head back to the User data in the EC2 set up page. In the script, find the “MINECRAFTSERVERURL=” field near the top. Paste the Minecraft server link into the MINECRAFTSERVERURL variable field. Do not leave a space between the ‘=’ and the Minecraft jar file url you are pasting. Do not edit any other part of the script. The image below shows an example of a correct set up in the User data field.

E.g: for Minecraft 1.20.1, the command in user data becomes “MINECRAFTSERVERURL=https://piston-data.mojang.com/v1/objects/84194a2f286ef7c14ed7ce0090dba59902951553/server.jar”

User data field contains the script from above. The MINECRAFTSERVERURL= field is filled with the Minecraft server jar link from the Minecraft website.

  1. After completing all the steps above, press Launch Instance. The EC2 instance may take up to 5-10 minutes on first launch to configure and start the server. You can test out your Minecraft server by finding the public IPv4 address under instance settings and connecting to that IP on Minecraft a few minutes after launch.

If you are having trouble connecting to your Minecraft Server, try these options:

  • The user data script can take up to 10 minutes to run on first launch so ensure you wait long enough.
  • Try stopping and starting your EC2 instance.
  • Review your networking settings in your security group.

Dedicating an IP address for your Minecraft Server

Every time an EC2 instance turns off, its public IP address will change. Having to search for your new IP every time you turn your server on and off is inefficient. We will associate an Elastic IP with our EC2 instance to solve this problem. Elastic IP addresses are dedicated IP addresses that do not change and can be attached to different resources.

Public IPv4 addresses such as an Elastic IP incur a charge on AWS. A strategy to save on cost if your server will be idle for a time is to free the Elastic IP address and turn your EC2 instance off before your hiatus. Please note that freeing and re-associating an Elastic IP will often result in the server’s IP address changing.

Setting up Elastic IP

  1. On the EC2 dashboard, expand the left side toolbar. Under Network & Security, click Elastic IPs.

On the left task bar of the EC2 dashboard, select Elastic IPs.

  1. Click on the top right button Allocate Elastic IP address. Select Public IPv4 from Amazon’s pool of IPv4 addresses. Press allocate.
  2. Select your Elastic IP, click actionsAssociate Elastic IP Address. Click Resource type as Instance, then choose your Minecraft Server instance from the drop-down menu. Then click associate.

Select the Elastic IP you created and then associate it with the Minecraft server instance. It will be an option in the dropdown from "actions".

Select your Minecraft server instance in the Associate Elastic IP address menu.

  1. Go back to your Minecraft Server EC2 instance and look under the Public IPv4 field. It will be your Elastic IP address.

Head to the EC2 instance we created in the EC2 dashboard. Click on the checkbox, and under "Details", copy the public IPv4 address.

  1. To connect to your Minecraft server, head to the multiplayer screen in game and either add server or use direct connect. In the “Server Address” field, put in your EC2 instance’s public IPv4 address. If you associated an Elastic IP with your instance, your Elastic IP will be your server address.

Setting up server operators, changing world files, and more

Now that you have set up your Minecraft server, you will need to add server operators to run commands in-game. You can also upload new world files, add plugins, and more. You can do this through EC2 Instance Connect, which we configured during the EC2 instance set up.

  1. First, head to the EC2 dashboard, find your Minecraft server EC2 instance, click the checkbox next to it and then hit the Connect button at the top of the console.

Pressing the connect button for your EC2 instance allows you to use EC2 Instance Connect.

  1. On the “connect to instance” screen, scroll down and hit Connect. Your user should already be ec2-user. If it isn’t, please change it to ec2-user. If your connection fails, you can reboot your EC2 instance and wait a few minutes before trying again. If you do not have the option to connect, you may have configured your EC2 instance incorrectly and should go back to the EC2 instance set up section to double check. Here are some common troubleshooting tips.
  2. After logging in successfully, you will see a terminal screen. You will now input a series of commands to go to the Minecraft server directory and edit any configuration files you need to.
  3. First, type in cd /opt/minecraft/server/ and hit enter. This is the directory that contains your server files in the EC2 instance, and you should remember it in case you’d like to edit your server configs in the future.
  4. Confirm that the server files are all present by typing in ls and hitting enter. You should see the same output that is listed under the “ls” command in the image below (e.g “world”, “logs”, “server.jar”). Notice the “start” and “stop” are highlighted green. The “start” file is an executable that automatically runs whenever you turn on/reboot your EC2 instance. Its function is to start the Minecraft server so you don’t manually have to. Do not edit these.
  5. The Minecraft server console, although not visible, is still running. You cannot edit any files or folders without turning the server off first. To do this, we will run the “stop” script by typing and entering sudo ./stop. This runs the stop script as admin. Now we can add a new world, plugins, etc.

The image below shows the EC2 Instance connect screen, along with the sequence of commands boxed in red that you should input.

In the EC2 Instance Connect terminal, type in these commands in order: "sudo su", "cd /opt/minecraft/server/", "ls", and "./stop".

  1. You can also upload a new world, or any other file, from your local computer to your EC2 instance either through the SCP command or using Amazon S3 to upload. You can also edit your “server.properties” file with a UNIX text editor such as vim or nano.
  2. In order to add operators or run commands from the console itself, we must run the server again by typing in sudo ./start. This will run the start script as admin, and load up the Minecraft server. You will see the server console begin to run like in the image below. It may take a few minutes for the server to start up.

In the EC2 Instance Connect terminal, type in "./start".

  1. After the console says “Done”, your server is up and running. You can type in any normal Minecraft command in the terminal now, without the ‘/’, to run it. For example, to add a player as an operator, type op *PLAYERNAMEHERE*.

Here is a reference image of what the server console outputs when it is done loading. We used an example player name to add as operator for our server.

After the console says "Done", you can op your players with "OP your-player-name".

Well done! You’ve successfully accessed your server directory and modified it to your liking. You have full control over the server files you want to upload. Add mods, plugins, and more!

Cleaning up

To stop incurring charges from the resources provisioned in this post, we will clean up the resources.

  1. On the EC2 dashboard, select your EC2 instance with the select box. On the top right, select Instance State, and then select Terminate.
  2. On the same EC2 screen, open the sidebar and select Elastic IPs. Press the select box next to your Elastic IP used for your EC2 instance, press Actions on the top right, and then press Release Elastic IP Addresses.

You have successfully cleaned up your resources!

Conclusion

Congrats! Now you have a Minecraft Server on an EC2 instance with a dedicated IP address. We highly recommend that you be diligent with stopping your EC2 instance when you are not playing on your Minecraft server. This will help save costs and ensure you only pay for when players are on the Minecraft Server. For next steps, we suggest looking into developing a solution to automatically stop the EC2 instance when the Minecraft Server is not in use.

Mitul Ganger

Mitul Ganger

Mitul Ganger is a Solutions Architect working in the World Wide Public Sector. He is interested in data analytics, sustainability, and gaming.

Caleb Grode

Caleb Grode

Caleb Grode is a Solutions Architect working in the World Wide Public Sector. He enjoys building with serverless technologies, playing video games with friends and going on runs.