Create a K8s cluster running a Discord bot using pure Terraform
A distributed Discord bot using Slash commands in Kubernetes!
- A Kubernetes Cluster running on Azure Kubernetes Service (AKS)
- A "frontend" app that listens and forwards incoming Discord Slash commands (uses Discord.js)
- A "backend" app that reponds to HTTP requets from the frontend app and runs all your bots custom logic! (Using Python and Flask)
Note: This is a "sister" repo to my other project
kong-k8s-terraform
feel free to checkout both and learn more about K8s and Terraform!
You will need a few things to use this project:
-
Fork me! π΄
-
An Azure account (this project uses AKS)
-
tfenv (for managing Terraform versions)
-
kubectl (for applying K8s manifests)
-
A Terraform Cloud account to store your TF state remotely
- See the
terraform-cloud
docs in this repo for more info (required if you are using Terraform Cloud)
- See the
-
An Azure Service Principal for deploying your Terraform changes - Create a Service Principal
-
Your Azure Service Principal will need
owner
permissions to your Azure Subscription. This is due to K8s needing to bind your ACR registiry to your K8s cluster with pull permissions - Assign Roles to a Service Principal -
You will need to skim through the following files and edit the lines with "
(CHANGE ME)
" comments:Example: Updating values with your own unique K8s cluster name and pointing to your own Terraform cloud workspaces
-
A Discord bot with Slash command permissions set and a token + client id - Discord Documentation
Build a K8s cluster with a single command!
Go make a coffee while this runs because it can take up to 15 minutes
$ make build
π¨ Let's build a K8s cluster!
β
tfenv is installed
β
Azure CLI is installed
β
kubectl is installed
β
terraform/k8s-cluster/terraform.auto.tfvars.json exists
β
terraform/k8s-cluster/terraform.auto.tfvars.json contains non-default credentials
π Deploying 'terraform/k8s-cluster'...
β΅ Configuring kubectl environment
π¨ Time to build K8s resources and apply their manifests on the cluster!
β
All manifests applied successfully
β¨ Done! β¨
Now that your bot is up and running in Kubernetes, you will need to register your Slash commands. To make this easy, I added a helper script which does this for you. It relies on the correct src/frontend/.env
file being setup with your Discord CLIENT_ID
and DISCORD_TOKEN
. Below is an example of me registering my commands for my test Guild:
$ make register
Started refreshing application (/) commands.
Successfully registered application commands for development guild
Successfully reloaded application (/) commands.
[#] Registered all slash commands in the src/frontend/commands folder!
If all went as planned you should now have a very simple Discord bot running that can respond to (now registered) Slash commands!
You can use either of the commands to test:
/ping
- Returnspong
/health
- Returns the health of the "backend" app
When you are done using your K8s cluster, you may destroy it by executing the following command:
$ make destroy
π₯ Let's DESTROY your K8s cluster!
Continue with the complete destruction of your K8s cluster (y/n)? y
β
Approval for destroy accepted
β
tfenv is installed
β
terraform/k8s-cluster/terraform.auto.tfvars.json exists
β
terraform/k8s-cluster/terraform.auto.tfvars.json contains non-default credentials
π₯ Destroying 'terraform/k8s'...
π₯ Destroying 'terraform/k8s-cluster'...
β¨ Done! β¨
If you want to test out the bot locally, rather than deploy to Azure AKS right away you can do so with docker-compose
- You have created a Discord bot with Slash command permissions
- You have invited your Discord bot to a Guild (channel) with the Slash command permissions
- You have filled out the variables in the
src/frontend/.env.example
and renamed it tosrc/frontend/.env
- You have run the
make register
command which registers your Slash commands with Discord
Now you can run the bot locally:
$ make run
[#] Killing old docker processes
[#] Building docker containers
[+] Building 2.4s (17/17) FINISHED
[+] Building 1.9s (11/11) FINISHED
Creating frontend ... done
Creating backend ... done
[#] The Docker stack is now running!
Open up Discord and run /ping
to test the bot in the Discord channel you added it to!
script/
- Contains various scripts for deployments and maintenancesrc/
- Contains the source code for the bot (frontend and backend)terraform/k8s-cluster
- The main terraform files for building the infrastructure of the K8s cluster. This folder contains configurations for the amount of K8s nodes, their VM size, their storage, etcterraform/k8s/*
- Kubernetes deployment manifests and Terraform files for Kong, Grafana/Prometheus, and the NGINX example http server
Once you have successfully built your K8s cluster and tested its functionality, you can deploy it using CI/CD with GitHub actions!
To do so, check out the following documentation in this repo: github-actions
The purpose of this project/repo is to quickly build a minimal K8s cluster with Terraform to get a Discord bot with Slash commands going.
All contributions are welcome! If you have any questions or suggestions, please open an issue or fork this repo and create a pull request!