Using a Python Flask application and AWS, this repository launches an AWS EC2 Instance to host a Minecraft server upon request from users through the web application. The server will automatically shut down after the server has crashed or is empty for 15 minutes. This makes server hosting for small communities very inexpensive. For up to 20 players you can expect $0.02 per hour the server runs. The largest benefit of this system is that the server bill diminishes if your community decides to take a break from the game, and will be ready to pick back up when you want to play again. No subscriptions are required.
Note that this configuration will likely require familiarity with programming, SSH, and the command line.
This step will properly configure your AWS account and configuration.py file so that an instance can be created via the createInstance.py script.
Create or access an AWS Account. Under the User Dropdown in the Toolbar, select Security Credentials, then Access Keys, and finally Create New Access Key. Download this file, open it, and copy the values of AWSAccessKeyId and AWSSecretKey to ACCESS_KEY and SECRET_KEY in the configuration.py file in the root directory of the repository.
ACCESS_KEY = 'YourAWSAccessKeyIdHere'
SECRET_KEY = 'YourAWSSecretKeyHere'
Navigate to the EC2 Dashboard under the Services Dropdown and select Security Groups in the sidebar. Select Create Security Group, input minecraft for the Security group name. Create Inbound Rules for the following:
In configuration.py in the root directory, set ec2_secgroups to the name of the security group.
ec2_secgroups = ['YourGroupNameHere']
Under the EC2 Dashboard navigate to Key Pairs in the sidebar. Select Create Key Pair, provide a name and create. Move the file that is downloaded into the root directory of the project. In configuration.py in the root directory, set ** ec2_keypair** to the name entered, and SSH_KEY_FILE_NAME to the name.pem of the file downloaded.
THIS MIGHT BE SUBJECT TO CHANGE
ec2_keypair = 'YourKeyPairName'
SSH_KEY_FILE_PATH = './YourKeyFileName.pem'
This step is concerned with creating the AWS instance. View https://docs.aws.amazon.com/general/latest/gr/rande.html (Or google AWS Regions), and copy the Region column for the Region Name of where you wish to host your server. In configuration.py of the root directory, set the ec2_region variable to the copied value.
ec2_region = "Your-Region-Here"
Navigate to https://aws.amazon.com/ec2/instance-types/ and select one of the T3 types (with the memory and CPU you desire, I recommend 10 players/GB). Copy the value in the Model column. I've configured mine to use t3.small. In configuration.py of the root directory, set the ec2_instancetype variable to the copied value.
ec2_instancetype = 't3.yourSizeHere'
Then we must select an image for the instance to boot. Navigate to https://cloud-images.ubuntu.com/locator/ec2/, in the filter at the bottom of the screen, select your region of choice under Zone, pick any LTS (Latest Stable) under Version, under Arch select amd64, and hvm:ebs under Instance Type. Select one of the images available and copy the AMI-ID. In configuration.py of the root directory, set the ec2_amis variable to the copied value.
ec2_amis = ['ami-YourImageIdHere']
At this point you should have the necessary configuration to create a new instance through the createInstance.py script in the root folder. Open a command line in the utilityScripts directory of the project, and execute:
pip install -r requirements.txt
After successful installation of dependencies execute:
Copy the Instance ID that is output into the terminal. In configuration.py of the root directory, set the INSTANCE_ID variable to the copied value.
INSTANCE_ID = 'i-yourInstanceIdHere'
In this step the project will get deployed to Heroku's free hosting. This part of the application provides a rudimentary UI and Web URL for users to start the server.
Before deployment it will be important to set the password for the server to start. In configuration.py of the root directory, set the SERVER_PASSWORD variable to the password of your choosing.
heroku create YourProjectNameHere
git push heroku master
This step will configure the AWS Linux server to run the minecraft server. It will include SSH connecting to the server, gaining admin privileges, installing java, directory setup, moving shell scripts onto the server, and making a CRON job for these shell scripts. Note that this step will include both an SSH client and a File Transfer client (such as FileZilla) on your PC.
The first step will be to get SSH into the server instance. Using the key downloaded from AWS in the section above, add this key to PuTTY or simply access it through command line. The IP address can be obtained by entering the server password on the site, or through the EC2 Dashboard, selecting the iPV4 address from the corresponding instanceID in your configuration file. For MacOS and Linux systems
ssh -i pathToYourKeyFileHere [email protected]
Make the ubuntu user admin if it isn't already with:
adduser ubuntu sudo
The next step will be to install JavaJDK onto your system. For newer versions you may enter:
sudo apt install openjdk-11-jdk-headless
If this doesn't work you can use
sudo apt list and search through these packages for an alternative java version.
Open up an FTP client such as FileZilla and connect to the same address as the same user with the same IP address. Drag all files from the instanceSetup folder from this repository, into the root directory of the current user (probably ubuntu, for the purposes of these commands I will be using ubuntu, but feel free to replace with your own user if appropriate).
Download the desired Minecraft server version from https://www.minecraft.net/en-us/download/server/, rename it server.jar and drag it into the root directory of the user using FileZilla.
Using the FTP client, create a new folder in the root directory of the current user called screens
In the SSH client, create a folder in the current directory with the command:
sudo mkdir screens
Then execute the following command:
sudo chmod 700 /home/ubuntu
Then execute the next command:
Then execute the command:
sudo crontab /home/ubuntu/crontab -u ubuntu
Feel free to close the server through the AWS console or execute the command:
sudo /sbin/shutdown -P +1
At this point you may restart the server from the Web Application using the password you configured. You should then be able to play!
The server startup command does not specify memory constraints by default, but is available to be specified in Configuration.py. In the event that you configure this from an empty string, the trailing space is required as in the example below. Traditional minecraft server flags apply for this configuration.
MEMORY_ALLOCATION='-Xmx1024M -Xms1024M '
The title and header for the site can be changed in /templates/index.html. Feel free to add any more content or styling to the site, though be careful not to change any form, input, button, or script elements in the template.
Maintaining the server is fairly straightforward and is done primarily through FileZilla. Updating the server file can be done by downloading the new server file, renaming it to server.jar and replacing the old file on the server. The world file can be backed up to your PC manually though there is no automated process at this time.