Writing April 3, 2023 · 5 min read

Set up an on-demand Minecraft Server on AWS Fargate


Do you love playing Minecraft with friends but worry about the cost of running a dedicated server 24/7?

Recently I was looking for a way to run Minecraft on a container in AWS, and I stumbled across an excellent project that solved my problem in a way I hadn’t considered, namely this GitHub repo by doctorray117. In this blog post, I’ll give you a broad overview of the steps required to create an on-demand Minecraft server using ECS Fargate that starts and stops automatically. For a more detailed implementation guide, please check out the project README.

Prerequisites

  • Basic understanding of AWS services: Familiarity with AWS services such as EC2, EFS, ECS, Lambda, CloudWatch, Route 53, and VPC will be helpful.
  • AWS Account: You’ll need an active AWS account with the ability to access and manage the necessary services mentioned above.
  • Domain name: A domain name that you own or control, which will be used to connect to your Minecraft server.

Estimated Cost

Due to the serverless nature of the services used in this deployment and their associated free tiers, the base cost is fairly minimal. The biggest costs are likely to be Route53 and ECS Fargate. The project author estimates a cost of around $2/month for 20 hours of play, and that matches my own experience running the solution for several months. A word of caution, however: while running ECS Fargate tasks for a few hours a day at low RAM can be inexpensive, if you run an ECS Fargate task for a month straight it will end up costing you more than an equivalent on-demand EC2 instance running 24/7. I’d recommend setting a $5 budget alert on your account to avoid any surprises.

Step 1: Configure AWS Infrastructure

First, set up the following resources in your AWS account:

  1. A VPC with at least one public subnet and an Internet Gateway.
  2. An Elastic Container Service (ECS) cluster.
  3. An Elastic File System (EFS) for storing Minecraft data.
  4. A security group to allow incoming traffic on port 25565 via TCP (for Java Edition).
  5. A Route 53 hosted zone for your domain to handle DNS requests for your server.

Step 2: Prepare the Docker Images

Next, choose your Docker images. I recommend using the highly customizable Java edition itzg/minecraft-server image for the server, and doctorray’s minecraft-ecsfargate-watchdog for the watchdog container.

Step 3: Create an AWS Lambda Function

To automatically start the Minecraft server when a player tries to connect, create an AWS Lambda function that listens for DNS requests and triggers the ECS task.

  1. Write a Python script for the Lambda function. The script should:
    1. Check for DNS requests for your domain.
    2. Start the ECS task if it’s not already running.
    3. Send a text message to your phone when the server is ready (optional).
  2. Create an AWS Lambda function with the script and grant it the necessary permissions.

Step 4: Configure ECS Task and Service

Now, create an ECS task definition and service for the Minecraft server.

  1. Create a task definition with two containers: the Minecraft server and a watchdog container.
    1. The Minecraft server container should use the Docker image prepared earlier.
    2. The watchdog container monitors the server and stops the task when no players are connected.
  2. Create an ECS service linked to the task definition and set the desired task count to 0.

Step 5: Set Up CloudWatch

Finally, configure CloudWatch to trigger the Lambda function when a player tries to connect to the server. Find your domain’s Route 53 Query Log Configuration in the CloudWatch console and do the following:

  1. Create a Lambda subscription filter for the log group.
  2. Configure the filter to trigger the Lambda function when it detects a DNS request for your server domain.

Usage and Customization

To start your server, visit your server’s domain in a web browser. The server will start automatically, and you can monitor its progress in the ECS console. Connect to your server in Minecraft once it’s up and running.

For customizing server settings, you have two options:

  1. Mount the EFS share directly on an EC2 instance and edit the relevant files.
  2. Use AWS DataSync to sync files between EFS and an S3 bucket for editing.

Remember to copy the latest files from EFS to S3 before making changes and copy them back after editing.

Troubleshooting

If you run into issues, check the following:

  • CloudWatch: Ensure DNS queries are logged correctly.
  • Lambda: Check the logs to verify the function is working correctly.
  • ECS: Ensure the task starts correctly and containers switch to the RUNNING state.
  • Connection: Make sure the security group allows traffic on port 25565.

That’s it! You’ve now set up an on-demand Minecraft server on AWS that starts when you want to play and shuts down when you’re finished. I ran this solution in my personal AWS environment for a few months with around 10 hours of playtime and found the cost projections to be accurate. Originally I was considering Fargate on EKS for my server, but after seeing this project and how it was utilizing DNS triggers I had to try it out. Thanks again to doctorray17 for making this available on GitHub.

Happy CloudForming

All writing Start a conversation