# Building & Hosting a Discord Bot on AWS

**Discord** is a pretty amazing Platform. We all have been envisioning a collaborative platform among your family & friends, at least vigorously during the Pandemic. Discord has been evolving across the industry faster than any other Chat Platform. Initially, Discord was targeted purely for Games, and now making its way across millions of people across the globe! Discord can be used for free, and thankfully not hidden behind a paywall. However, Discord can be ramped up with Server Emojis, Custom Emotes, and more with the paid subscription called **Discord Nitro**. Let’s not forget something, they support streaming up to **4K (60 FPS).**

In 2020, Discord introduced two major and useful features which are Slash Commands & Interaction Endpoints. However, there are not many use-cases for both of these features, just yet.

Okay, enough of storytelling. Let’s not go too deep into the history, let’s get right into the **“cool stuff” —** We have Uncle Google for the history :)

## Why AWS?

I’m not going to put in a lot of details on this, as the results or cost would be self-explanatory. **So, why AWS?** Because they are reliable, cheap, and cost-effective. However, the steps listed below could work on any Virtual Private Server, Dedicated Server, or any Linux-based Servers.

## Okay, let’s get started

This is for hosting a standard Python Discord Bot on AWS, I will be walking you through some cheesy boilerplate to get things started. However, I’m assuming that you already have adequate knowledge on developing a Discord Bot, including attaining the relevant Tokens and more!

## **Step 1**

Below is a simple gist on getting started with your own Discord Bot

%[https://gist.github.com/8a03eb73c4277c1669e110a7ecadd580] 

## **Step 2**

Let’s create our Python Environment, with the installation of the dependency.  
To get started, simply run the commands below to giddy things up. (Also, assuming you have adequate knowledge on setting up Virtual Env, etc).

\# Assuming you have created the file in Step 1 on a separate folder.  
\# Note: Activating the venv can be different on Windows. Or with any terminal extensions like **fish, etc**.

```bash
python3 -m venv venv
pip install discord
pip freeze > requirements.txt
source venv/bin/activate
```

## **Step 3**

Now, assuming that you have gone through all the necessary steps to attain your own Discord Bot Token. If you are unsure, you may visit this [link](https://discord.com/developers/applications).

## Step 4

\*\*That is all on the setting up part. Let’s pack our Code Editor and store it in a safe place. In this step, we’ll be using **Amazon EC2** to set up our bot & keep it always online.

## **Step 5**

Let’s now go to your [AWS Console](http://console.aws.amazon.com).

Search for EC2 in AWS Console

## **Step 6**

Now, let’s choose the EC2 instance of our choice. In this tutorial, I will be choosing **“t2.micro”.** If you’d like to estimate the cost & compare across all the types of instances AWS has to offer, simply visit this [link](https://calculator.aws/#/createCalculator/EC2).

**Instance Configuration (Most default):**

```plaintext
OS: Ubuntu 20.04 LTS (x86)
vCPU: 1
Memory: 1 GB
Storage: 8 GB
```

## **Step 7**

Now, once we’ve configured our instance. Let’s look into your security group. Please ensure port 22 is allowed from **“Anywhere”** as we’ll be using SSH to connect to the EC2. Please refer to the screenshot below.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1767059322332/8e33f881-4b47-4dd7-a786-0c71393d4dbb.png align="left")

## **Step 8**

Alright. Once that is selected appropriately, let’s jump into the next step. Let’s now choose our Key Pair (If present) or create a separate Key Pair file.   
**NOTE:** This Key Pair serves as the credential in order for you to log into your EC2. Please do not lose this file. The downloaded Key Pair file will come with the extension of **“.pem”**.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1767059324059/a551208b-4b96-4725-ba31-b1f4bbd49c00.png align="left")

## **Step 9**

Now, we have got our EC2 fired up. Hold on! Before we move forward, let’s verify if our EC2 is running.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1767059325983/5b4a3292-9d90-41e6-87f0-c3cd0d14a896.png align="left")

## **Step 10** 

**Woila!** The EC2 server is now live. Let’s try connecting to it.   
**NOTE:** You will have to use the .pem file to authenticate to the Server.

<mark># Open up your Terminal / Favorite SSH Client</mark>  
<mark># If you are facing issues as "Bad Permission" while trying to authenticate, please change the file permission as below;</mark>

> **chmod 600 discord-ec2.pem**

Then, run this command to connect to the EC2 Server.

> **ssh -i "discord-ec2.pem" ubuntu@Your Public iPv4 DNS**

### **Example:**

> ssh -i "discord-ec2.pem" ubuntu@ec2-18-140-70-118.ap-southeast-1.compute.amazonaws.com

## **Step 11** 

Upon successful authentication, you’ll be directly in the Shell of the EC2.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1767059327280/321e597e-864e-4d59-8a31-1f9480a1e73e.png align="left")

## **Step 12**

Now, let’s prepare our EC2 for some Rock & Roll.

Firstly, let’s update the dependencies. Simply run the command below in your EC2 Instance:

> **sudo apt-get update && sudo apt-get upgrade -y**

## **Step 13**

Hold on right there!

> ***How do we transfer the file from our Local Machine to the EC2?***  
> Well, you **“could”** find many other ways of transferring the file to the EC2. In this Tutorial, I will be using `rsync` to transfer the file to the EC2.

**<mark>Other methods you could consider:</mark>**  
\- GitHub (Pull code from EC2)  
\- SFTP  
\- More

Let’s now giddy up `rsync` on our Local Machine. You may install `rsync` using **Homebrew (Mac)** or any other Package Manager for your corresponding Platform.

<mark>Example (Mac OS):</mark>

```bash
brew install rsync
```

## **Step 14**

Once `rsync` is installed on your local machine, let’s get the files moving.

Simply run the command below

<mark># Please replace the values accordingly when executing.</mark>

```bash
rsync -azvv --progress -e "ssh -i discord-ec2.pem" \
/path/to/your/code/folder \
ubuntu@ec2-18-140-70-118.ap-southeast-1.compute.amazonaws.com:~/
```

---

Now, that your files are on the Server. Quickly, verify the files with the following commands

```bash
cd folder
ls -all
```

---

Once all files are intact, let’s set up the Python Environment.

Firstly, create the Python Virtual Environment.

```bash
cd path/to/your/code/in/the/server
sudo apt install python3.8-venv -y
python3 -m venv venv
```

Secondly, let’s activate the newly created Virtual Environment & Install the dependencies

```bash
source venv/bin/activate
pip install -r requirements.txt
```

Lastly, assuming you have replaced the Discord Auth Token with a Token attained from Discord’s Developer Portal. Let’s run it

\# Simply run the command below

```bash
python3 run.py
```

---

## Conclusion

All the steps listed are curated in such a way where it can be understood and followed along. Rest assured, your Discord Bot should now be online!

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1767059328521/6e6300ac-718a-4f0c-949c-e55963a99d60.png align="left")

Stay Safe. Happy Coding & Happy Deploying!
