RabbitMQ - Bind a Queue and Exchange using Terraform

This assumes you have setup Terraform as described in RabbitMQ - Getting Started with Terraform. Let's say you have the following files on your Terraform server.

/usr/local/terraform/rabbitmq/main.tf
/usr/local/terraform/rabbitmq/foo/required_provider.tf
/usr/local/terraform/rabbitmq/foo/bindings.tf
/usr/local/terraform/rabbitmq/foo/exchanges.tf
/usr/local/terraform/rabbitmq/foo/policies.tf
/usr/local/terraform/rabbitmq/foo/queues.tf
/usr/local/terraform/rabbitmq/foo/shovels.tf
/usr/local/terraform/rabbitmq/foo/vhosts.tf

 

Add the following to your bindings.tf file. This will bind "foo.queue" to "foo.exchange" with routing key "foo.key"

resource "rabbitmq_binding" "binding1" {
  vhost = "foo"
  source = "foo.exchange"
  destination = "foo.queue"
  destination_type = "queue"
  routing_key = "foo.key"
}

 

The terraform init command can be used so that Terraform can communicate with RabbitMQ.

terraform init

 

The terraform plan command can be used to see what Terraform will try to do.

terraform plan

 

If the binding does not exist, Terraform will create the binding. If the binding already exist and the terraform.tfstate file contains the binding (it should), the binding will be updated. By default, the terraform.tfstate file should be found in your root module directory (/usr/local/terraform/rabbitmq in this example).

The terraform apply command from your root module directory (/usr/local/terraform/rabbitmq in this example) can be used to create or update the binding.

terraform apply

 

Something like this should be displayed, and you will be prompted to enter yes.

Terraform will perform the following actions:

  # rabbitmq_binding.binding1 will be created
  + resource "rabbitmq_binding" "binding1" {
      + destination      = "foo.queue"
      + destination_type = "queue"
      + id               = (known after apply)
      + properties_key   = (known after apply)
      + routing_key      = "foo.key"
      + source           = "foo.exchange"
      + vhost            = "foo"
    }

Plan: 1 to add, 0 to change, 0 to destroy.

 

After entering yes, the following should be displayed

rabbitmq_binding.binding1: Creating...
rabbitmq_binding.binding1: Creation complete after 0s [id=foo/foo.exchange/foo.queue/queue/foo.key]

Apply complete! Resources: 1 added, 0 changed, 0 destroyed.

 




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 b6de6 in the box below so that we can be sure you are a human.