Turnkey solution for on-demand pushbutton minecraft server
Forked from d10n/minecraft-on-demand-lambda Changes:
- destroy and deploy no longer run terraform from a lambda which is clunky. They use the boto3 APIs to simply stop the server instance. This has the advantage of being simpler and more reliable (I had issues with running terraform w/in lambda). However it does cost slightly more as the EBS volume is not destroyed.
- Because of the first change the status is now read directly from the instance status instead of inferred from terraform.
- Discord now optional. The discord send message would not work for me so it will now work even if discord bot integration fails.
- Creates an API URL that deploys the server
- Auto shutoff after 30 minutes
- Backup every 5 minutes
- Costs about $0.30/mo with light usage. (From d10n, not confirmed)
Requirements:
-
Terraform
-
JDK 1.8 (if you want spigot)
-
An ssh key pair to SSH into the running Minecraft server:
ssh-keygen -t rsa -b 2048 -f ~/.ssh/id_minecraft -
Ensure you have awscli credentials configured: http://docs.aws.amazon.com/cli/latest/userguide/cli-chap-getting-started.html
-
Copy core/terraform.tfvars.example to core/terraform.tfvars and fill in the values.
- See the variables section below for instructions to get the values.
- The s3 buckets and dynamodb tables will be created with the supplied names; you don't need to create them manually
-
Edit the backend variables at the top of instance/instance.tf
- Copy
core/terraform.tfvars.exampletocore/terraform.tfvars - Fill in
core/terraform.tfvars, for more help see the Variables section. makewill build the lambdas and deploy everything, also creating your minecraft server instance immediately.- Spigot setup (optional):
make spigot# if you prefer spigot to vanilla minecraft
To get the discord client token:
- Click "New Application" on the Applications page: https://discordapp.com/developers/applications/me
- Enter a name and click "Create Application"
- Click "Create Bot User" and click the confirmation button
- Click the token reveal link
To add the discord bot to your channel (and get the channel ID):
- Get the Client ID from the top of your bot's page
- Visit https://discordapp.com/oauth2/authorize?client_id=INSERT_CLIENT_ID_HERE&scope=bot&permissions=2048
- Follow the instructions to add the bot to your channel
- On Discord, open User Settings -> Appearance -> Enable Developer Mode
- Right click on the channel that you added the bot to and click "Copy ID" to get the channel ID
To get the AWS access key and secret key:
- Visit the IAM console and click Add User
- Set a user name, check Programmatic Access, and click Next
- Click the Create group button, enter a group name, check AdministratorAccess, and click Create Group
- Click Next and then click Create user
- Write down the Access key ID and Secret access key from the success page, because this is the last time you can get the secret access key!
To get the region:
- Pick one from the Region column in the tables at https://docs.aws.amazon.com/general/latest/gr/rande.html. Make sure it supports Amazon API Gateway and AWS Lambda.
- I suggest us-east-1
The SSH terraform public key should point to the public key you want to use to SSH into the Minecraft server.
For dynamodb and s3 names, any value is fine as long as it hasn't been used by another AWS user.
-
coreconfigures the AWS infrastructure that can create the Minecraft server on demand -
core/core.tfconfigures all of the permanent AWS resources, like the S3 buckets, Lambda functions, and API Gateway methods -
core/terraform.tfvarsholds your individual settings -
core/variables.tftells terraform what variables to expect -
core/auto_shutoff.pyis downloaded by the instance and periodically run to shut the server off if there were no players for the last 30 minutes -
core/lambda_destroy_deploy/lambda_destroy_deploy.pyis the code for the Lambda destroy and deploy functions -
core/lambda_status/lambda_status.pyis the code for the Lambda status function -
core/lambda_status/requirements.txtlists the dependencies to be installed forlambda_status.py -
core/instance.tfconfigures all of the on-demand AWS resources, including the Minecraft EC2 server and its VPC -
web/index_src.htmlis the template forweb/index.html. The core deployment plugs in the deploy and status URLs. -
web/index.htmlis a basic web page with a button to deploy the Minecraft server -
Makefilehas recipes to run all the required setup commands in the right ordermakedeploys or updates the coremake initinitializes your terraform backendmake planrunsterraform planafter building the lambda function zip filemake infoshows the variable output from the core deploymentmake spigotcompiles spigot and uploads it to your s3 world bucketmake terraform-bundlecompiles a terraform bundle with all provider dependencies included (not currently used)
- If you need more RAM, set a bigger instance size than t2.micro in instance.tf and increase Xmx and Xms in
provision_minecraft.shto be a little below the instance size's total allocated RAM - The Lambda functions can have bundled dependencies or they can install dependencies when they run. I don't know which I prefer yet and I have both approaches: the destroy and deploy functions install dependencies at runtime, while the status function bundles its dependencies.
- Elastic IP is a static IP that works across redeploys of the server. Using Elastic IP is convenient to avoid DNS TTL caching, but it costs extra. Enable by uncommenting all blocks containing "eip" references in
core/core.tfandcore/instance.tf. - Restore from backups using s3-pit-restore
- Example:
s3-pit-restore --bucket d10n-minecraft-world-backup --dest world-restore --timestamp '2017-10-11 4:30PM EDT' aws s3 sync --delete world-restore s3://d10n-minecraft-world-backup
- Example:
Improvements and ideas
- Fix discord integration, add discord commands to start/stop the server. This is challenging since discord bots, unlike slack bots, cannot be set to hit a webhook and must be listening (of polling). Meaning lambda discord bots are not a simple idea and paying to run a nano instance for the discord bot nearly defeats the purpose of all the work to make this as cost effective as possible. This would be relatively simple with an existing discord bot however.
- Destroy EBS volume. Then you would need to restore a new one from a snapshot at deployment. Seems like a lot of hassle for such a small EBS volume.
- Restore API endpoint would be a nice addition
- Route53 alias of s3 bucket for easier access?