RabbitMQ - Getting Started with Terraform

This assumes you have installed Terraform, as described at https://learn.hashicorp.com/tutorials/terraform/install-cli?in=terraform/aws-get-started.

You should probably setup Terraform with root and child modules, where the root module defines your RabbitMQ servers (providers), and the child modules are used to create/update/destroy objects, such as virtual hosts, queues, exchanges, and so on.

Let's say you will be storing your Terraform file for RabbitMQ files in the /usr/local/terraform/rabbitmq directory on your Terraform server. Files in this directory will be known as the root module. You will almost always want to define the RabbitMQ provider in the main.tf file.

If you have a single RabbitMQ instance, then the endpoint can simply be the hostname and HTTP port of the single RabbitMQ instance, such as http://rabbit.example.com:15672. However, if you have a cluster of two or more RabbitMQ nodes, it is probably the case that you have a load balancer forwarding requests onto the cluster of RabbitMQ nodes. In this scenario, the endpoint can be the load balancer URL, something like http://balancer.example.com. In this scenario, you wouldn't want to target one of the nodes in the cluster, as this would cause objects to get created on a single node in the cluster, which defeats the purpose of having a cluster of RabbitMQ nodes.

  • username = The RabbitMQ user (must exist in RabbitMQ and have been granted write permission to the virtual host)
  • password = The RabbitMQ users password

AVOID TROUBLE

The name of the provider must be an exact match of the name of the required_provider, "rabbitmq" in this example.

terraform {
  required_providers {
    rabbitmq = {
      source  = "cyrilgdn/rabbitmq"
    }
  }
}

provider "rabbitmq" {
  endpoint = "http://rabbit.example.com:15672"
  username = "john.doe"
  password = "itsasecret"
}

module "foo" {
  source = "./foo"
}

module "bar" {
  source = "./bar"
}

 

Notice main.tf has two child modules, foo and bar. This means that .tf files in the /usr/local/terraform/foo and /usr/local/terraform/bar directories will be able to use the RabbitMQ provider in /usr/local/terraform/main.tf.

 

For example, the vhost.tf file in the foo and bar directories could contain the rabbitmq_vhost resource to create/update/destory a virtual host.

/usr/local/terraform/foo/required_provider.tf
/usr/local/terraform/bar/vhost.tf
/usr/local/terraform/foo/required_provider.tf
/usr/local/terraform/bar/vhost.tf

 

Here is what you would have in the required_provider.tf files.

terraform {
  required_providers {
    rabbitmq = {
      source  = "cyrilgdn/rabbitmq"
    }
  }
}

 

And here is what you could have in the foo vhost.tf file.

resource "rabbitmq_vhost" "vhost1" {
  name = "foo"
}

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee

Add a Comment




We will never share your name or email with anyone. Enter your email if you would like to be notified when we respond to your comment.





Please enter f9674 in the box below so that we can be sure you are a human.




Comments

Web design by yours truely - me, myself, and I   |   jeremy.canfield@freekb.net   |