Host your Discord Bot on EC2 instance(AWS) πŸ€–

Host your Discord Bot on EC2 instance(AWS) πŸ€–

Β·

4 min read

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" Alt Text
  • 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”. Alt Text

##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