/k8s-discord

Build a Discord Slash command bot and deploy it to Kubernetes β˜ΈπŸ€–πŸ’œ

Primary LanguageHCLGNU General Public License v3.0GPL-3.0

k8s-discord

k8s discord terraform

Create a K8s cluster running a Discord bot using pure Terraform

deployment review tfsec


What You Will Create ⭐

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!

Prerequisites 🚩

You will need a few things to use this project:

  1. Fork me! 🍴

  2. An Azure account (this project uses AKS)

  3. tfenv (for managing Terraform versions)

  4. kubectl (for applying K8s manifests)

  5. Azure CLI

  6. 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)
  7. An Azure Service Principal for deploying your Terraform changes - Create a Service Principal

  8. 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

  9. 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

  10. A Discord bot with Slash command permissions set and a token + client id - Discord Documentation

Usage πŸ’»

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 - Returns pong
  • /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! ✨

Developing Locally πŸ› οΈ

If you want to test out the bot locally, rather than deploy to Azure AKS right away you can do so with docker-compose

Prerequisites

  • 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 to src/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!

Project Folder Information πŸ“‚

  • script/ - Contains various scripts for deployments and maintenance
  • src/ - 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, etc
  • terraform/k8s/* - Kubernetes deployment manifests and Terraform files for Kong, Grafana/Prometheus, and the NGINX example http server

GitHub Actions ⚑

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

Purpose πŸ’‘

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.

Contributing πŸ‘©β€πŸ’»

All contributions are welcome! If you have any questions or suggestions, please open an issue or fork this repo and create a pull request!