Bootstrap FreeKB - RabbitMQ - Create Update or Delete a Policy using Terraform
RabbitMQ - Create Update or Delete a Policy using Terraform

Updated:   |  RabbitMQ articles

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 policies.tf file. This will create a policy named foo.policy

resource "rabbitmq_policy" "policy1" {
  name = "foo.policy"
  vhost = "foo"

  policy {
    pattern = "^foo.queue$"
    priority = 0
    apply_to = "queues"

    definition = {
      dead-letter-exchange = "foo.exchange"
      dead-letter-routing-key = "*"
      max-length-bytes = "10000000"
      overflow = "reject-publish-dlx"
    }
  }
}

 

You may need to reissue the terraform init command.

~]# terraform init
Initializing the backend...
Initializing modules...
Initializing provider plugins...
Terraform has been successfully initialized!

 

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 policy does not exist and the terraform.tfstate file does not contain the policy, Terraform will create the policy.
  • If the policy exists and the terraform.tfstate file contains the policy and a difference is found between the policy.tf file and the terraform.tfstate file, Terraform will update the policy.
  • If the policy exists and the terraform.tfstate file contains the policy and the policy is removed from the policy.tf file, Terraform will destroy (delete) the policy.

 

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

terraform apply

 

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

Terraform will perform the following actions:

  # rabbitmq_policy.policy1 will be created
  + resource "rabbitmq_policy" "policy1" {
      + id    = (known after apply)
      + name  = "foo.policy"
      + vhost = "foo"

      + policy {
          + apply_to   = "queues"
          + definition = {
              + "dead-letter-exchange"    = "foo.exchange"
              + "dead-letter-routing-key" = "*"
              + "max-length-bytes"        = "10000000"
              + "overflow"                = "reject-publish-dlx"
            }
          + pattern    = "^foo.queue$"
          + priority   = 0
        }
    }

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

 

After entering yes, the following should be displayed.

rabbitmq_policy.policy1: Creating...
rabbitmq_policy.policy1: Creation complete after 0s [id=foo.policy@foo]

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



Comments


Add a Comment


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