brooke-guajardo / blut_aws_tf_minecraft

Fargate Minecraft server with discord bot using lambda and apigw for slash comand management
0 stars 1 forks source link

Welcome to my Repo

This is just a way for my friends and I can enjoy some MC that's hopefully a bit more cost effective than hosting services. As well as give more control! As the about says, this is an ECS Fargate Minecraft server that uses a Discord bot that runs on a Lambda with an API Gateway Trigger for slash command management.

Happy to answer any questions, I'd like to make this more generic for others to use. For now it does have configs that are more tuned to what my friends and I are playing.

Setup Instructions

1. Create AWS ECR

# env vars set NOT RECOMMMENDED (:
export AWS_ACCESS_KEY_ID=your_aws_access_key
export AWS_SECRET_ACCESS_KEY=your_aws_secret_access_key

git clone https://github.com/brooke-guajardo/blut_aws_tf_minecraft.git
cd blut_aws_tf_minecraft/ecr
terraform init
terraform apply

2. Build Docker Image and Push to ECR

Since we are leveraging the auto download handled by Curse Forge's API, we have to handle mods that owners have not enabled auto downloads. This requires you to locally download the mods, so that when building the docker file, the mods' jar files are copied over. More details here.

I found what files I needed to manually download by locally running the docker image, more detailed steps here.

Note:

Below I have v1.0.0 this value needs to match what you put in the ecs/ecs.tf file. And if you ever rebuild the image, you need to bump the version since the ECR is set to immutable.

# env vars set NOT RECOMMMENDED (:
export AWS_ACCESS_KEY_ID=your_aws_access_key
export AWS_SECRET_ACCESS_KEY=your_aws_secret_access_key
export AWS_ACCOUNT_ID=your_aws_account_id
export AWS_REGION=your_aws_region
export TF_VAR_public_key=your_public_key

cd build
# before the docker build download and put into the build folder your jar files!
docker build . -t jardo_minecraft:v1.0.0
docker image ls # to confirm it was made properly, get the IMAGE_ID
aws ecr get-login-password --region "${AWS_REGION}" | docker login --username AWS --password-stdin "${AWS_ACCOUNT_ID}".dkr.ecr."${AWS_REGION}".amazonaws.com
docker tag IMAGE_ID "${AWS_ACCOUNT_ID}".dkr.ecr."${AWS_REGION}".amazonaws.com/minecraft:v1.0.0
docker push "${AWS_ACCOUNT_ID}".dkr.ecr."${AWS_REGION}".amazonaws.com/minecraft:v1.0.0

3. Deploy ECS, EFS, VPC and Security Groups

# env vars set NOT RECOMMMENDED (:
export AWS_ACCESS_KEY_ID=your_aws_access_key
export AWS_SECRET_ACCESS_KEY=your_aws_secret_access_key
export TF_VAR_cf_api_key=your_curse_forge_api_key
export TF_VAR_rcon_pass=your_rcon_password

cd ecs
terraform init
terraform apply

4. Deploy Lambda, API Gateway, and supporting Infrastructure (Discord Bot)

This section assumes you have created your discord bot already, that you have a discord server that you own or can have your bot run in (you need the server's ID i.e. guild ID for registering your commands, though you can also make the commands global, further reading here), and you have RCON enabled in the ECS section of the terraform code.

Also for the lambda function, it needs all libraries packaged with it as per AWS documentation. Below was my method of doing so

# env vars set NOT RECOMMMENDED (:
export AWS_ACCESS_KEY_ID=your_aws_access_key
export AWS_SECRET_ACCESS_KEY=your_aws_secret_access_key
export TF_VAR_rcon_pass=your_rcon_password
export TF_VAR_public_key=your_public_key
export TF_VAR_discord_app_id=your_discord_bot_app_id
export DISCORD_SERVER_ID=your_discord_server_id
export DISCORD_BOT_TOKEN=your_discord_bot_token

cd discord
cd commands
# the output should tell you if they succeed
# also they'll show up in the server you are testing in if it works
python3 register_commands.py
cd .. # or back into the discord directory
# create python virtual environment
python3 -m venv venv
source venv/bin/activate
pip3 install pipreqs
# this will make the requirements.txt file
pipreqs .
pip3 install -r requirements.txt
cd venv/lib/python3.9/site-packages/
zip -r ../../../../deployment_package.zip .
# change directory back to discord/
cd -
# add the lamba function to the zip
zip deployment_package.zip lambda_function.py
terraform init
terraform apply

The output from this terraform will give you the APIGW url that you need to put into the settings of your discord bot.

Client instructions

Discord Bot Usage

Full Command List

To get the service IP address please use /get_ip don't worry if it fails, the lambda is finicky, just wait a moment and run the command again. If get_ip returns "list index out of range" it means the server is not up, no worries! Turn on the minecraft server with /turn_on_mc wait a minute or two (at most like 5 mins?) and you should be able to get the IP When done, you should be able to run /turn_off_mc from discord this command will now run RCON /save-all against the server before scaling down the instances to 0.

References

Pre-generated worlds (TODO)

  1. Reference branch init_pregen you will need a folder in the build directory and some changes to the docker file.
  2. Then 2 env vars that you only want to run once against your ECS or else you'll lose progress that's been written to your EFS.
  3. Once you have those changes
  4. Build new image and upload to ECR
  5. Deploy to ECS the new env var and with the new docker image tag