How to Run a Minecraft Server on AWS For Less Than 3 US$ a Month

During the first weeks of the COVID-19 pandemic, back in april 2020 my son ask me to build a Minecraft server in order to play on the same world with his school friend. After checking some available services (yeah not so expensive finally), I have chosen to build a server on a EC2 instance. This article will explain you how to optimize the cost 😜, based on the usage!

Some Tools Used in the Article

AWS

I want to rely only on AWS services as I want to increase my knowledge on this big cloud offering. There is always one service you don't know ! In this particular example I will use the following services:

Reading: minecraft server on aws

  • EC2 (virtual servers in the cloud)
  • Lambda (serverless functions)
  • Simple Email Service (Email Sending and Receiving Service)

Minecraft

Minecraft is a popular sandbox video-game. In this case I will focus on the Minecraft Java Edition, because the server version is running well on Linux server, and my son is running a laptop on Debian.

Global Architecture of the Solution

The first month operating the server, I noticed that my son is using it a couple of hours each day, and then the server was idle. It's built on a EC2 t2.small with a 8 GB disk so I have a monthly cost of about 18 US$. Not a lot but I was thinking that there is room for improvement! The main part of the cost is the EC2 compute cost (~17 US$) and I know that it's not used 100% of the time. The global idea is to start the server only when my son is using it, but he doesn't have access to my AWS Console so I need to find a sweet solution!

Here is the various blocks used:

  • an EC2 instance, the Minecraft server
  • use SES (Simple Email Service) to receive e-mail, and trigger a Lambda function
  • one Lambda function to start the server
  • one Lambda function to stop the server

And that's it. My son is using it this way:

  • send an e-mail to a specific and secret e-mail address, this will start the instance
  • after 8h the instance is shutdown by the lambda function (I estimate that my son must not play on Minecraft more than 8h straight 😅)

Let's Build it Together

Build the EC2 Instance

This is the initial part, you must create a new EC2 instance. From the EC2 dashboard, click on Launch Instance and choose the Amazon Linux 2 AMI with the x86 option.

Launch Instance

Next you must choose the Instance Type. I recommend you the t2.small for Minecraft. You will able to change it after the creation.

Choose Instance Type

Click on Next: Configure Instance Details to continue the configuration. Keep the default settings, and the default size for the disk (8 GB) as it's enough.

For the tag screen I generally provide a Name (it's then displayed on EC2 instance list) and a costcenter (I use it for cost management later).

Instance Tags

For the Security Group, it the equivalent of a firewall on EC2 and you must configure which port will be accessible from internet on your server. I add SSH port and the Minecraft port (25565) like you see on the following screen:

Instance Security Group

Then to start the instance you must select or create a key pair. It's mandatory and allow then to connect remotely to your EC2 instance. In my case I am using an existing key pair but if you create a new key don't forget to download on your laptop the private key file.

Instance Key

Yes my key is named caroline. Why not?

Then you must connect your instance via SSH, I recommend this guide if you need help. Basically you must run this kind of command:

Related: 5 best Minecraft seeds for survival islands | WTBBLUE

The public-ipv4 is available in the instance list:

Get the IPv4 address

You first need java:

And I want a dedicated user:

To install Minecraft you can rely on the Minecraft server page here.

For example for the version 1.17.1 (as of today) I can run the following:

I have created a little service to avoid start manually the server. I want the Minecraft process to start as soon as I start the server.

To do that I have created a file under /etc/systemd/system/minecraft.service with the following content:

Then advise the new service by the following:

More information on systemd here.

Now if you restart the EC2 instance a Minecraft server must be available! You can check ✅ this first step!

I am not speaking of the fact that the IPv4 is dynamic by default. I recommend to setup an static Elastic IP for this server (here!) in order to get a static IP.

Build the Start Scenario

Let's first create our Lambda function. Go into Lambda, and click on Create function to build a new one. Name it mc_start and use a wtbblue.com 14.x or more runtime.

Then you must have this type of screen:

Lambda Start

Replace the content of wtbblue.com file with the following:

In Configuration, set the following:

  • add an environnement variable named INSTANCE_ID with the value that correspond to the Instance Id of your Minecraft server (something like i-031fdf9c3bafd7a34).
  • the role permissions must include the right to start our EC2 instance like this:

Lambda Permissions

In Simple Email Service, it's time to create a new Rule Set in the Email Receiving section:

View more: Minecraft Wii U Edition! How To Get Mods For Minecraft Wii U Edition?

SES EMail Receiving

Click on Create rule inside default-rule-set.

If SES is receiving an email on this particular identity:

SES Config 1

It invoke a Lambda function:

SES Config 2

You must add the domain to the Verified identities to make this work.

Build the Stop Scenario

This time we want to stop the instance after 8h. It's a simple Lambda function.

Let's first create our Lambda function. Go into Lambda, and click on Create function to build a new one. Name it mc_shutdown and use a wtbblue.com 14.x or more runtime.

Replace the content of wtbblue.com file with the following:

In Configuration, set the following:

  • add an environnement variable named INSTANCE_ID with the value that correspond to the Instance Id of your Minecraft server (something like i-031fdf9c3bafd7a34).
  • add an environnement variable named MAX_HOURS with the value that correspond to number of hours allowed after startup, like 8 for 8 hours).
  • the role permissions must include the right to start our EC2 instance like this:

Lambda Permissions

We add a trigger to fire the task every 20 minutes:

Add Trigger

Hurray the configuration is done !

Conclusion

This setup is working nicely here, my son is happy because he start himself the instance when he need. I am happy because it reduce a lot the cost of this service. On the last 3 months I see that the EC2 Compute cost for this server is less than 1 US$ 😅 (around 17 US$ before the optimization) so 95% less expensive !

Currently the configuration is made manually in the console, I would love to spend some time to change that one day, using for example the CDK toolkit.

It's also probably possible to manage the storage of the Minecraft world on S3 instead of the Instance EBS disk (some $$ to save here, but not a lot).

It was a very fun project to build using multiple AWS services! Do you see other usages of dynamically boot EC2 instances using Lambda functions? Let me know in the comments!

View more: Minecraft How To Get Villagers Into Minecarts, How To Push A Villager Into A Minecart

Leave a Comment