RabbitMQ - Create Update or Delete an 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 exchanges.tf file. This will create a direct exchange named foo.exchange and a fanout exchange named bar.exchange

resource "rabbitmq_exchange" "exchange1" {
  name = "foo.exchange"
  vhost = "foo"

  settings {
    type = "direct"
    durable = true
    auto_delete = false
  }
}

resource "rabbitmq_exchange" "exchange2" {
  name = "bar.exchange"
  vhost = "foo"

  settings {
    type = "fanout"
    durable = true
    auto_delete = false
  }
}

 

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

 

By default, the terraform.tfstate file should be found in your root module directory (/usr/local/terraform/rabbitmq in this example).

  • If the exchange does not exist and the terraform.tfstate file does not contain the exchange, Terraform will create the exchange.
  • If the exchange exists and the terraform.tfstate file contains the exchange and a difference is found between the exchange.tf file and the terraform.tfstate file, Terraform will update the exchange.
  • If the exchange exists and the terraform.tfstate file contains the exchange and the exchange is removed from the exchange.tf file, Terraform will destroy (delete) the exchange.

 

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

terraform apply

 

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

Terraform will perform the following actions:

  # rabbitmq_exchange.exchange1 will be created
  + resource "rabbitmq_exchange" "exchange1" {
      + id    = (known after apply)
      + name  = "foo.exchange"
      + vhost = "foo"

      + settings {
          + auto_delete = false
          + durable     = true
          + type        = "direct"
        }
    }

  # rabbitmq_exchange.exchange2 will be created
  + resource "rabbitmq_exchange" "exchange2" {
      + id    = (known after apply)
      + name  = "bar.exchange"
      + vhost = "foo"

      + settings {
          + auto_delete = false
          + durable     = true
          + type        = "fanout"
        }
    }

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

 

After entering yes, the following should be displayed.

rabbitmq_exchange.exchange1: Creating...
rabbitmq_exchange.exchange2: Creating...
rabbitmq_exchange.exchange2: Creation complete after 0s [id=bar.exchange@foo]
rabbitmq_exchange.exchange1: Creation complete after 0s [id=foo.exchange@foo]

Apply complete! Resources: 2 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 85b50 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   |