Today I will be sharing on how to host your Discord Bot on AWS (EC2 Instance). Assuming that you already have a discord bot created (if not, please checkout this amazing article :Creating your first Discord Bot — Part 1
Let's dive into it.
##Creating a server instance.
##Connect to your instance
Navigate terminal to the directory where you have your “pem” file. First you must make sure your key is not “publicly” viewable. This has to do with file permissions.
chmod 400 yourkeyfile.pem
Login using the key with the following command:
$ ssh -i "yourkeyfile.pem" firstname.lastname@example.org
⚠️Replace “188.8.131.52” with your “public IP” as shown in your EC2 console.
Your terminal should then show that you have connected.😊
##Installing node and running your Bot.
sudo apt-get update sudo apt-get install nodejs sudo apt-get install npm
Upload your node application to the server. You can do this with SFTP software like Cyberduck or WinSCP. The key is selecting “Use Public Key Authentication” rather than enter a password. OR you can clone your repo from Git.
Install the node package dependencies (specified in your package.json file):
Once your files are uploaded you’ll want to check and make sure your bot works, i.e.
The bot will only run while you have terminal open. In order for it to run after you’ve logged out of your EC2 instance and closed your computer, you can either use PM2 or "forever".
sudo npm install -g pm2
Now, we will use the pm2 start command to run our bot, bot.js, in the background
pm2 start bot.js [PM2] Starting /home/ubuntu/discord-greeter-bot/bot.js in fork_mode(1 instance) [PM2] Done. ┌────┬────────────────────┬──────────┬──────┬───────────┬──────────┬──────────┐ │ id │ name │ mode │ ↺ │ status │ cpu │ memory > │ ├────┼────────────────────┼──────────┼──────┼───────────┼──────────┼──────────┤ │ 0 │ bot │ fork │ 0 │ online │ 0% │ 40.4mb │ │ 1 │ bot │ fork │ 0 │ online │ 0% │ 24.5mb │ └────┴────────────────────┴──────────┴──────┴───────────┴──────────┴──────────┘
⚠️Applications that are running under PM2 will be restarted automatically if the application crashes or is killed, but an additional step needs to be taken to get the application to launch on system startup (boot or reboot). Luckily, PM2 provides an easy way to do this, the startup subcommand.
The startup subcommand generates and configures a startup script to launch PM2 and its managed processes on server boots:
pm2 startup systemd
The last line of the resulting output will include a command that you must run with superuser privileges:
Output [PM2] Init System found: systemd [PM2] You have to run this command as root. Execute the following command: sudo env PATH=$PATH:/usr/bin /usr/lib/node_modules/pm2/bin/pm2 startup systemd -> -u ubuntu --hp /home/ubuntu
Run the command that was generated (similar to the highlighted output above, but with your username instead of ubuntu) to set PM2 up to start on boot (use the command from your own output):
sudo env PATH=$PATH:/usr/bin /usr/lib/node_modules/pm2/bin/pm2 startup systemd -> u ubuntu --hp /home/ubuntu