DEV Community

Cover image for Minecraft Server on AWS
Evandro Pires da Silva for AWS Heroes

Posted on

Minecraft Server on AWS

My 7 years old son is a fan of Minecraft. He started playing last year (2023) and he only talk about that the last 6 months. Now, he want to play with us, me, my wife, my 5 years old daughter, as well with some cousins.

It's available lots of servers on the internet, both paid or free, but those server don't have the security I want to my kid.

It's true that I'm a nerd father, so I had the idea of creating my own Minecraft Server on AWS. A server it will be used only by us.

So, if you want to learn a little about AWS in a fun way or you're a parent wanting to create a Minecraft server to your kids, here a GitHub repository that I created for that: https://github.com/epiresdasilva/aws-minecraft-server

To create this server, we'll use the following AWS services:

If you're not familiar with these services, I recommend to study the basics first, including Security Groups on EC2.

Steps

It's very simple to create a Minecraft server. To do this faster and reproduceble, I create a CloudFormation code for that.

In the repository, you'll find a file called minecraft-server.yaml.

Basically, in this file I create a Security Group to allow only few ports I want to expose to the internet, like SSH and the port 25565 that is the port Minecraft Launcher connects.

  MinecraftSecurityGroup:
    Type: 'AWS::EC2::SecurityGroup'
    Properties:
      GroupDescription: 'Enable SSH and Minecraft server port'
      SecurityGroupIngress:
        - IpProtocol: tcp
          FromPort: 22
          ToPort: 22
          CidrIp: 0.0.0.0/0
        - IpProtocol: tcp
          FromPort: 25565
          ToPort: 25565
          CidrIp: 0.0.0.0/0
Enter fullscreen mode Exit fullscreen mode

Another important part is creating the EC2 instance. Basically, you need to choose the Linux image you want, as well the instance type. I choose a t4g.small and it's running very well. I just need to understand if can be a smaller instance yet.

      InstanceType: t4g.small
      ImageId: ami-02cd6549baea35b55
      KeyName: Minecraft Server
Enter fullscreen mode Exit fullscreen mode

The instance type you can find in the EC2 page or using the AWS Calculator, so doing that it's easier to understand how much it will costs.

The image ID you'll find on the AMI Marketplace on the EC2 section on your AWS Management Console.

You need to create a KeyPair also on the EC2 section. After creating, just put the name in the "KeyName" on the YAML file.

Finally, you just need to run the following command:
aws cloudformation create-stack --stack-name MinecraftServerStack --template-body file://minecraft-server.yaml --region us-east-1

Security

Remember, DO NOT create that stack using your root user. Create an especific one to that. On doing that, remember to give only the necessary permissions you need. For instance, I used AmazonEC2FullAccess and AWSCloudFormationFullAccess and worked fine, but maybe can be more restricted yet.

This template doesn't create an advanced security for that server, so if you need to improve security it's important working on new layers of security in that template.

In my server, I made a configuration using Security Group to allow only connections from my IP, so I avoid other users to connect to my server even if they know my EC2 address.

Following it's how you can do that in the AWS Management Console:
Image of a Security Group configuration showing where is the My IP option

Reboot

The script for running the Minecraft will run only for the first time when the EC2 instance is created. So, if your instance reboot the Minecraft Server won't start again.

After creating my EC2 instance, I create a systemd to handle with that, but I didn't add that to my template.

  1. Create a systemd service file:
sudo vi /etc/systemd/system/minecraft.service
Enter fullscreen mode Exit fullscreen mode
  1. Create the content:
[Unit]
Description=Minecraft Server
After=network.target

[Service]
User=root
Nice=5
KillMode=none
SuccessExitStatus=0 1
InaccessibleDirectories=/root /sys /srv /media -/lost+found
NoNewPrivileges=true
WorkingDirectory=/
ExecStart=/opt/jdk-17.0.9/bin/java -Xmx1024M -Xms1024M -jar minecraft_server.jar nogui
ExecStop=/bin/kill -SIGINT $MAINPID

[Install]
WantedBy=multi-user.target
Enter fullscreen mode Exit fullscreen mode
  1. Reload the systemd:
sudo systemctl daemon-reload
Enter fullscreen mode Exit fullscreen mode
  1. Enable the service:
sudo systemctl enable minecraft.service
Enter fullscreen mode Exit fullscreen mode
  1. Start the service:
sudo systemctl start minecraft.service
Enter fullscreen mode Exit fullscreen mode
  1. Check the service status:
sudo systemctl status minecraft.service
Enter fullscreen mode Exit fullscreen mode
  1. Service logs:
sudo journalctl -u minecraft.service
Enter fullscreen mode Exit fullscreen mode

Playing Minecraft

Now, you have your own Minecraft Server and you are able to play with your friends and family.

Top comments (1)

Collapse
 
rdarrylr profile image
Darryl Ruggles

I set one of these up a few years back for my kids but my setup was more of a hack. This looks much more clean. Thanks for sharing!