Fork of https://github.com/leobeosab/AwsEc2DiscordBot with a few mods to make it easy for my kids and friends to start/stop a minecraft server, including:
- fixed some issues arising from newer discord module
- replaced hard coding with environment variables
- added similar bot script for ECS bots (more flexible, faster to start, cheaper if you use Fargate spot)
- bot limited to guild-tagged EC2 instances or ECS services
- added
infocommand statechecks to see if minecraft running and reports appropriately- templates for a
systemdlinux service added
- Python 3 (v3.8 or higher) and pip3
- AWS CLI :
pip3 install awscli - AWS BOTO library :
pip3 install boto3 - Discord Bot library :
pip3 install discord
- for ECS (Docker/Fargate) bots, see http://github.com/drpump/minebot-cdk
- for EC2 bots:
- create an EC2 instance and configure minecraft to run as a daemon. I used msm (https://msmhq.com)
- add a
guildtag to your ec2 instance with your Discord guild ID as the value
- Install and setup the required tools above on your bot server
- Setup AWS CLI with
aws configure - Go to Discord's developer site and setup a bot here
- Clone this repo into a desired folder
- Set the discord token environment variable with the name 'AWSDISCORDTOKEN'
- Start bot for EC2 or ECS:
- For EC2, run python3 bot.py
- for ECS, run python3 ecsbot.py
Assuming you've already got it running locally using the instructions above ...
- Create cloud VM using an Ubuntu image and login
- Install python3
sudo apt install python3 - Install AWS cli
sudo pip3 install awscli. You might need to use--target /usr/lib/python3.Xto ensure it is installed system-wide. - Install required libs
sudo pip3 install boto3 discord. - Create a daemon user with a home dir
sudo useradd --service -m ec2bot - Su to that user
sudo su - ec2bot - Setup AWS using
aws configure(you will need suitable AWS credentials, preferably locked down to limit capabilities), or copy an already configured.awsdirectory for this user (chmod'ed so ec2bot owns the dir and files) - Copy/download
bot.pyorecsbot.pyinto the home directory - Exit from your
susession (^D) - Copy/download
ec2bot.serviceorecsbot.service - Edit and set the
AWSDISCORDTOKENenvironment variable - Copy service file to systemd, e.g.
sudo cp ec2bot.service /etc/systemd/system/ - Load and enable
sudo systemctl daemon-reload
sudo systemctl enable ec2bot # or ecsbot
sudo systemctl start ec2bot # or ecsbot
- Use
tail /var/log/syslogto check output and make sure it is running