Hey Everyone,
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.
- Go to the Amazon EC2 Page and sign up for an account.
- Navigate to the Amazon web services console.
- Click on βEC2β (top left of page).
- Select "Launch Instance".
- Select "Ubuntu Server 18.04 LTS (HVM), SSD Volume Type - ami-07ebfd5b3428b6f4d"
- Pick βGeneral Purposeβ (free tier micro)
- Select βReview and Launchβ
- Launch!
- Youβll then see a prompt about your βkey/pairβ. The key pair consists of a public key that AWS stores, and a private key file that you store. They come together in a file βsomething.pemβ which you will need in order to login to your account. Select βcreate new pairβ and enter a title for your key/pair.
- Select βDownload Key Pairβ. The file will download to your computer. Save this file! Youβll need it for future steps.
- Now click βLaunch Instancesβ.
- Now you should see a message that βYour instances are launchingβ (it may take a few minutes before they are available.)
- Select βView Instancesβ.
##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" ubuntu@54.38.245.127
β οΈReplace β54.38.245.127β 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.
Install node
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):
npm install
- Once your files are uploaded youβll want to check and make sure your bot works, i.e.
node bot.js
- 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".
##Install PM2.
- PM2 provides an easy way to manage and daemonize applications (run them in the background as a service).
- We will use npm, a package manager for Node modules that installs with Node.js, to install PM2 on our server. Use this command to install PM2
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