Amazon Linux 2 Setup¶
For what is intended to be the final production version, an Amazon EC2 instance is running Amazon Linux 2. Amazon Linux 2 includes such conveniences as pip and python pre-installed.
Setup Amazon Linux 2¶
Amazon Linux 2 is Amazon’s latest version of Amazon Linux. According to OS metadata, Amazon Linux 2 may be a fork of RHEL7 or the equivalent Fedora version, but built to run on the EC2 of AWS. It is a good choice for having an operating system that is fully up to date and supported by the “host.” I particularly like that it is already setup with most of what is needed. If preferred, use Ubuntu, OpenSUSE, Red Hat, et al., but your mileage will vary using this guide.
- Opted for a
t3.microinstance since that’s what’s ‘in’ right now. Also used Security Groups to limit access to My IP and used a key pair.
- It is a good idea to enable billing alerts.
- The root volume is deleted on termination of the instance, so I enabled termination protection. Alternatively, the root volume can be changed to persist.
Configure an EBS to store the database. Went with 20 GB since that should be more than enough. Also went with magnetic storage because it costs less than SSD storage and the I/O speed is not needed.
Next, secure the EC2 instance. It is an old guide, so the only thing that needs to be done is adding a new user (with limited access, by default) to run the bot. At this point, a root volume snapshot can be made to save progress, if persistence was not enabled previously.
- Install and enable yum-cron to keep the EC2 instance updated automatically.
Setup pip and python¶
Install some dependencies as the default user, not the new one.
sudo yum install gcc python3-devel.x86_64 openssl-devel libffi-devel pip3 install --upgrade pip
As the new user, set up the needed environment and select it from within the package directory:
python3 -m venv clashcallerbot-reddit/env source clashcallerbot-reddit/env/bin/activate # selects venv
From within the virtual environment, run:
pip install -U wheel pip install --upgrade pip pip install praw==6.0.0 # Still on praw6.0.0, for now pip install mysql-connector
Set up a MySQL database within an EBS volume as the default user. The guide is for MySQL and Ubuntu, but setup
for MariaDB and Amazon Linux 2 is very similar (replace
mariadb, and use
sudo systemctl [start|stop] mariadb to start or stop MariaDB). Mainly follow the steps for creating and
attaching an EBS volume. Fear not, the remaining steps will be summarized in this guide.
First, once the EBS volume has been created and attached, the default user needs to run the following from
within the EC2 instance to create an XFS filesystem at
# Create XFS filesystem sudo yum install xfsprogs mariadb-server mariadb-devel grep -q xfs /proc/filesystems || sudo modprobe xfs sudo mkfs.xfs /dev/sdf # change to wherever volume is mounted # Mount XFS filesystem echo "/dev/sdf /vol xfs noatime 0 0" | sudo tee -a /etc/fstab sudo mkdir -m 000 /vol sudo mount /vol
Now that MariaDB is installed, it must be configured.
sudo systemctl start mariadb sudo systemctl status mariadb # Confirm it is running sudo mysql_secure_installation # Say 'y' to everything! sudo mysql -uroot -p"password"
From within the MariaDB prompt,
MariaDB [(none)]>, the database can be set up.
CREATE DATABASE db_name; USE db_name; CREATE TABLE message_table (id INT UNSIGNED NOT NULL AUTO_INCREMENT, permalink VARCHAR(100), message VARCHAR(100), new_date DATETIME, username VARCHAR(20), PRIMARY KEY(id)); ALTER TABLE message_table AUTO_INCREMENT=1; GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, INDEX, ALTER, CREATE TEMPORARY TABLES, LOCK TABLES ON db_name.* TO 'botname'@localhost IDENTIFIED BY 'password'; QUIT;
Alternatively, run the
database.py script with the settings specified in Quickstart.
Make sure that MariaDB is stopped with
sudo systemctl stop mariadb && sudo systemctl status mariadb, then move
MariaDB into the EBS volume.
sudo mkdir /vol/etc /vol/lib /vol/log sudo mv /etc/my.cnf.d /vol/etc/ sudo mv /var/lib/mysql /vol/lib/ sudo mv /var/log/mariadb /vol/log/ sudo ln -s /vol/etc/my.cnf.d /etc/my.cnf.d sudo ln -s /vol/log/mariadb /var/log/mariadb sudo mkdir /var/lib/mysql echo "/vol/lib/mysql /var/lib/mysql none bind" | sudo tee -a /etc/fstab sudo mount /var/lib/mysql sudo systemctl start mariadb && sudo systemctl status mariadb sudo systemctl enable mariadb # set to start at boot
Now that python, pip, and MariaDB have been set up, the new user can download and setup the bot:
source clashcallerbot-reddit/env/bin/activate # set virtual environment, if needed cd clashcallerbot-reddit # go to desired source directory wget https://github.com/JoseALermaIII/clashcallerbot-reddit/raw/master/update.sh chmod +x ./update.sh ./update.sh
Next, add the bot’s reddit metadata to praw-example.ini and rename to praw.ini, then add the database’s root and desired bot user credentials to database-example.ini and rename to database.ini.
Then, all requirements can be installed via pip:
pip install -r requirements.txt
Once all relevant files have been downloaded and configured, the bot can be started:
chmod +x ./clashcallerbot.sh ./clashcallerbot.sh
- The bot has to login to reddit at least once to refresh the oauth token. Amazon Linux 2 does not have a web
browser installed by default, so run
sudo yum install lynxas the default user before running the script.