terraform-azurerm-jmusicbot: A Terraform Module to Deploy JMusicBot into Azure

terraform-azurerm-jmusicbot: A Terraform Module to Deploy JMusicBot into Azure

Have you ever been in a Discord server with a music bot that keeps going offline? It can be frustrating for the server members and those hosting the bot on their personal machines. That’s where TF-JDiscordBot comes in. This Terraform module installs, configures, and runs JMusicBot (https://jmusicbot.com/) on an Ubuntu 18 instance hosted in the Azure cloud. This way, you can have a reliable music bot for your Discord server without worrying about your personal machine being online 24/7.

Update: I changed the name to be compliant with the Terraform public module naming standards so I could publish it there.

https://registry.terraform.io/modules/RCFromCLE/jmusicbot/azure/latest?

Getting Started

There are two ways to use TF-JDiscord. The first option is to clone the repository and use the module locally:

git clone https://github.com/RCFromCLE/tf-jdiscord.git
cd tf-jdiscord
terraform init
terraform plan
terraform apply

The second option is to reference the module in your Terraform configuration:

module "jdiscord" {
  source = "git::https://github.com/RCFromCLE/tf-jdiscord.git"
  
  sub = "your-subscription-id"
}

Before you can use the module, you’ll need to set up the backend configuration in your Terraform configuration. This is where the state file will be stored, and it requires an Azure Storage Account and container. Once you have those resources set up, you can add the following block to your configuration:

terraform {
  backend "azurerm" {
    resource_group_name  = "tfstaterg01"
    storage_account_name = "tfstate01790900905"
    container_name       = "jdb-tf-state"
    key                  = "terraform.tfstate"
  }
}

The only required variable for the module is the Azure subscription ID, which you can set in your Terraform variables file (e.g. terraform.tfvars) like this:

sub = "your-subscription-id"

Note: You will need to generate an SSH keypair to be used to for access to the Linux VM.

Main.tf Breakdown

Now let’s take a closer look at the main.tf file. First, we have the provider blocks for azurerm, tls, random, null, and local. These are the providers that the module uses to create resources and generate random strings and numbers.

Next, we have the input variables that allow you to customize the resources that will be created, should you want to. You can set the name and location for the resource group, virtual network, subnet, public IP, and network security group. You can also set the name and size of the virtual machine, as well as the publisher, offer, and SKU of the image that will be used.

After that, we have the output variables that show the public and private IP addresses of the virtual machine, as well as the ID of the network interface.

Finally, we get to the resources that are actually created. The module starts by creating a resource group, virtual network, and subnet in Azure. Then it creates a public IP and network security group, and associates the NSG with the network interface.

The virtual machine is where things get interesting. The module first creates a disk resource and attaches it to the VM as the OS disk. This is where the JMusicBot application will be installed. Then, it creates the virtual machine resource and assigns the previously created network interface and OS disk to it. This virtual machine will run on the Ubuntu 18.04 LTS operating system, which is specified in the vm_image_sku variable.

The module also uses a remote exec provisioner to configure the virtual machine and install the necessary dependencies for JMusicBot. This block of code uses a connection block to specify how to connect to the virtual machine via SSH, and then runs a series of bash commands to update the package manager and install the required packages.

Note: this module assumes you have generated an SSH keypair and placed the private and public keys at ~/.ssh/.

Source: https://github.com/RCFromCLE/tf-jdiscord/main.tf

One important thing to note is that the module includes The local_file data source. Which allows you to access data from a file on the local file system. In this case, it is used to read the contents of a file called config.txt and assign it to the data.local_file.config_txt data object. This can be useful for storing sensitive information, such as API keys, in a separate file that is not committed to version control. Later in the configuration, this data object can be accessed and used as needed.

Overall, this module makes it easy to spin up an Azure virtual machine running JMusicBot, saving you the hassle of manually configuring the virtual machine and installing the necessary dependencies. Plus, with the use of Azure spot instances and the Standard_B1s VM size, it is a cost-effective solution for running a music bot for your Discord server.

Screenshots of the solution in Azure:


Thanks for reading this far. If you have any feedback about the module feel free to comment or reach out to me.

-Rudy

Related Links:

https://registry.terraform.io/modules/RCFromCLE/jmusicbot/azure/latest?tab=inputs
https://jmusicbot.com/
https://github.com/RCFromCLE/tf-jdiscord
https://github.com/jagrosh/MusicBot
https://learn.microsoft.com/en-us/windows-server/administration/openssh/openssh_keymanagement
https://developer.hashicorp.com/terraform/language

Leave a Reply