lukesmurray / mc-server

Luke and Mateo's Minecraft Server.
MIT License
1 stars 0 forks source link
aws aws-ec2 aws-lambda minecraft serverless terraform

mc-server

Luke and Mateo's Minecraft Server. Runs itzg/minecraft-server. Has automatic backups and loading from s3. In order to reduce costs the server turns itself off if no one is playing for 12 minutes. A passsword protected website hosted on Amazon Lambda can be used to turn the server back on.

To deploy yourself just follow all the instructions below carefully.

Server Setup

Aws Setup

  1. Create a Key Pair.
    • aws ec2 create-key-pair --key-name MyKeyPair --query 'KeyMaterial' --output text > MyKeyPair.pem
    • chmod 400 MyKeyPair.pem
  2. Copy the key pair to this folder.

Terraform Setup

  1. Install terraform
  2. Install aws-cli
  3. Install serverless
  4. Run aws configure. Follow instructions here
    1. Set your access keys and region, default output format can be json.
  5. Run terraform init mc-server to initialize local settings
  6. Import the key pair from aws. terraform import -config=mc-server aws_key_pair.mc_auto_key_pair MyKeyPair where MyKeyPair is the name of the key you downloaded in aws set up.
    1. You can see possible key names using aws ec2 describe-key-pairs | jq '.KeyPairs[].KeyName'

Optional Bucket Import

  1. Run terraform import -config=mc-server aws_s3_bucket.mc_auto_bucket your-bucket-name

Server Deploy

  1. set the variables in ./terraform.tfvars. Descriptions of these variables can be found in ./mc-server/variables.tf
  2. Run ./deploy.sh
  3. Endpoint is printed out or can be found by running ./url.sh.

Server Shutdown (Safe Mode Data Preserved)

This will destroy any compute instances but keep back ups of your world in the bucket specified by bucket name. You may be charged for these. You will see an error, bucket not empty but all other resources will have been destroyed.

  1. Run ./destroy.sh

Server Shutdown (DANGER MODE)

This will destroy compute instances and the bucket.

  1. Go into server.tf and change force_destroy in mc_auto_bucket to true.
  2. Run terraform apply --auto-approve mc-server
  3. Run ./destroy.sh

S3 Bucket

The server automatically performs versioned backups of the server data directory to s3://<your_bucket_name>/mc-auto-backups. If you have an existing data directory from itzg/minecraft-server you can use aws s3 sync <your_data_directory> s3://<your_bucket_name>/mc-auto-backups to pass the data to the server before deployment. The server will automatically load your data into the initial world.

Dockerfile

Feel free to edit the variables in the docker-compose file to customize your server. Variable definitions can be found in the itzg/minecraft-server readme. Do not change the volumes since the mount point is important for backups. Feel free to change the memory limit if you have a smaller or larger instance.

Upgrading

First download the existing bucket to a local folder. where mc-mc-auto-bucket is the name of your s3 bucket from ./terraform.tfvars.

aws s3 sync s3://mc-auto-bucket/mc-auto-backups ./mc-server-example

Next move the world in the folder to your minecraft local save.

cp -r ./mc-server-example/world /Users/lukemurray/Library/Application Support/minecraft/saves/world

Now optimize the world in local minecraft. Then copy the world back to the local save.

cp -r /Users/lukemurray/Library/Application Support/minecraft/saves/world ./mc-server-example/world

Sync the local copy back to the bucket

aws s3 sync ./mc-server-example s3://mc-auto-bucket/mc-auto-backups

Now destroy and restart the server.

terraform destroy --auto-approve mc-server

./deploy.sh

Useful Terraform Commands

terraform [cmd]