Deploying a

Minecraft Server on AWS with Ansible



Automate Your Minecraft Server Deployment and Save Money

Published: 18th September 2022; Last updated: 18th September 2022

Preface

After writing two blog posts on how to manually setup a Minecraft server on AWS, I thought about automating this tedious work. Ansible is an automation tooling that allows you to deploy, configure and orchestrate servers and applications.
Nothing more to say here, let's get started!

Requirements

  • An AWS account (requires a credit card).
  • A UNIX environment.
    Ansible will only run on Unix-like environments, such as Linux or MacOS. Windows is not supported. However, Windows users can use WSL2 as a workaround. It requires some additional tweaks until Ansible is working properly. At the point in time writing this blog post, I used WSL2 with Ubuntu.
  • Python 3.8 or higher.
  • Knowledge about configuring and running a Minecraft server (will not be covered in this blog post).

Installing Ansible and AWS CLI

Assumption: you have already installed python in version 3.8 or higher.
In your terminal, enter the following installation commands:

$ sudo apt install awscli
$ pip install boto
$ pip install boto3
$ pip install ansible
$ ansible-galaxy collection install amazon.aws
After having installed the required tooling, clone the repository containing the playbooks.

Configuration

AWS Config

  • Create an IAM User Group named Administrators and attach the AdministratorAccess policy to this group.
  • Create an IAM User (preferably in AWS Management Console) with both credential types, Access key for Programmatic access and Password for AWS Management Console access and assign it to the Administrators group.
  • Next, configure the AWS CLI. Use your IAM User access key and secret and choose a region:
    $ aws configure
    AWS Access Key ID: ********************
    AWS Secret Access Key: ********************
    Default region name: eu-central-1
    Default output format: json
    
    Check your configuration with aws configure list.
  • Create an S3 bucket in your region. In your bucket, create the following folder structure:
    /ansible
    /ansible/backup
    /ansible/setup
    
    • Edit the file minecraft.service and replace <mc-server-jarfile-name> with your minecraft server jar-file name.
    • Place the minecraft.service file in the /ansible/setup folder.
    • Rename the folder which contains your minecraft-server.jar file to mc-server. Place the folder into another folder named minecraft and zip this folder:
      $ tar cf mc-server.tar minecraft/mc-server
      
    • Upload the .tar file to /ansible/setup/mc-server.tar.
  • Create a key pair of type ED25519 and select .pem as file format.
    Download the created .pem file and save it to your ~/.ssh/ folder. Note: You can only download it once!

Ansible Playbook Config

  • Once you have cloned the repository containing the Ansible playbooks, checkout the ansible branch and navigate to the configuration/ directory. Copy the file bucket-policy.json.template and remove the .template suffix. Your copy should be located in configuration/bucket-policy.json. Edit the file and replace all occurrences of <s3-bucket-name> with your S3 bucket's name.
  • Next, do the same to configuration/vars/config.yml.template. Create a copy and remove the .template suffix. Replace all <placeholders> correspondingly. Note: don't forget to enter your localhost's public IPv4 address (placeholder is <1.2.3.4>). This is needed to allow ansible sending installation commands to the EC2 instance through an SSH connection.

Ansible Playbooks Explained

Ansible playbooks are nothing but configuration files written in some advanced YAML-syntax.
They are invoked like this:

$ ansible-playbook setup.yml

$ ansible-playbook teardown.yml

Setup

The setup.yml playbook wraps four other playbooks that will take care of:

  1. Creating an EC2 instance, including all the necessary infrastructure like VPC, subnet, gateway, routing tables and a security group. In addition, a CloudWatch alarm is registered, which will send an email notification and try to stop the EC2 instance, if no network traffic is observed over a period of 15 minutes.
  2. Installing dependencies on the EC2 instance, e.g. Java 17.
  3. Copying the Minecraft server files from the S3 bucket to the EC2 instance, unzipping them and launching the Minecraft server daemon.
  4. Finally, printing out the server's public IP address, which you will need to enter in minecraft as the server address.

Teardown

The teardown.yml playbook includes:

  1. Stopping the Minecraft server and creating a backup in your S3 bucket. Only the latest 3 backups will be kept. Older backups are deleted automatically.
  2. Terminating the EC2 instance and cleaning up all the infrastructure components like VPC, subnet, etc.
In case you just want to terminate the EC2 instance, you can directly invoke the teardown/terminate-ec2.yml playbook. WARNING: this is a destructive action and will not create a backup of your server files. All game progress will be lost!

Conclusion

If you want to run your own Minecraft server and only play every now and then, AWS is the perfect place to go. Ansible can not only help you automating the tedious setup, but will also save you money, since you will only require AWS resources when your server is actually running (plus some cents for cold storage in an S3 bucket). In addition, you can easily switch your instance type by changing the yml-config, just in case you need some extra performance.

If my blog article helped you in setting up your own Minecraft server on AWS, I'd be glad if you would leave me a ★ Star on the GitHub repository. In case you find the ansible playbooks to be broken, feel free to open an issue on GitHub.

Excursion: our Minecraft Server

To make use of the EC2 instance's full power, we do not run a vanilla server, but have chosen to go for PaperMC as it is "compatible with Spigot plugins and offering uncompromising performance". We strongly recommend it, they're also fast with updates (only few days to weeks behind public Minecraft releases).

Have fun!

Image: Minecraft Server hosted on AWS, using Ansible


To the top