Deploy Minecraft server using terraform to AWS.
Uses lambda functions to auto-destroy a Minecraft server instance after inactivity. S3 is used for Minecraft world backups and for storing terraform state.
Future functionality:
- add Discord bot for both starting and stopping the Minecraft instance
- An AWS account with user credentials for programmatic access - see https://docs.aws.amazon.com/IAM/latest/UserGuide/id_users_create.html#id_users_create_console
- Download and install terraform from https://www.terraform.io/downloads.html
- For running the scripted setup of the Terraform resoources, install the AWS CLI at https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-install.html
- For local development (optional): python 3.7, and pip installed
- Install virtualenv:
sudo pip install virtualenv
- Change into source directory
cd src
- Activate venv:
. venv/bin/activate
- Install dependencies:
pip install -r requirements.txt
- Create IAM credentials for programmatic access and add locally as named AWS credential
- Update
config/account.tfvars
,iac/mc-static/config.tf
, andiac/mc-server/config.tf
with your credentials and your region and an unique name for your S3 bucket for Terraform state
- Update
Manual setup
- Create S3 bucket and DynamoDB table for terraform state via the AWS console
- Create the DynamoDB table with
LockID
as key attribute
- Create the DynamoDB table with
- Create EC2 key via the AWS console
Scripted Setup
- Run
./init_tf_req.sh
in the root of the locally cloned repo
- Copy latest Minecraft server download URL into
src/mc-server.sh
.
- Init terraform:
terraform init
iniac/mc-static
andiac/mc-server
- Change to static infrastructure setup:
cd iac/mc-static
- Execute:
terraform apply -var-file=../../config/account.tfvars
- Creates: S3 bucket for Minecraft World backup, Public IP, SNS topic plus attached Lambda for auto-destroy
- Change to server infrastructure setup:
cd iac/mc-server
- Execute:
terraform apply -var-file=../../config/account.tfvars
- Creates: Minecraft Server with attached Public IP
Beyond the allocation of AWs resources, the terraform script triggers modification of the ec2 instance. It installs the Minecraft server, downloads the S3 backed-up minecraft world to the local instance, and add's a crontab script for detecting idle state. Once idle state has been detected, it triggers a backup of the current minecraft world to S3, and triggering the destruction of the ec2 instance by sending an empty message on the SNS destroy topic.
Attached to the SNS Topic is a lambda function, which downloads and installs terraform locally within the lambda context and executes a 'terraform destroy' on the server resources.
- Logging into ec2 instance using the EC2 key:
ssh -i ~/.ssh/minecraft.pem ec2-user@<eip>
- Listing available screen sessions:
screen -ls
- Re-attaching to minecraft screen session:
screen -r minecraft
- Add new Minecraft version download URL in
src/mc-setup.sh
- Remove
eula.txt
file in root of minecraft backup S3 bucket - Re-apply terraform with
terraform apply -var-file=../config/account.tfvars
- https://www.codingforentrepreneurs.com/blog/install-django-on-mac-or-linux - installing python on MacOS
- https://jeremievallee.com/2017/03/26/aws-lambda-terraform.html - deploying AWs Lambda with terraform