consul_key_prefix

Allows Terraform to manage a "namespace" of Consul keys that share a common name prefix.

Like consul_keys, this resource can write values into the Consul key/value store, but unlike consul_keys this resource can detect and remove extra keys that have been added some other way, thus ensuring that rogue data added outside of Terraform will be removed on the next run.

This resource is thus useful in the case where Terraform is exclusively managing a set of related keys.

To avoid accidentally clobbering matching data that existed in Consul before a consul_key_prefix resource was created, creation of a key prefix instance will fail if any matching keys are already present in the key/value store. If any conflicting data is present, you must first delete it manually.

Example Usage

resource "consul_key_prefix" "myapp_config" {
  datacenter = "nyc1"
  token      = "abcd"

  # Prefix to add to prepend to all of the subkey names below.
  path_prefix = "myapp/config/"

  subkeys = {
    "elb_cname"         = "${aws_elb.app.dns_name}"
    "s3_bucket_name"    = "${aws_s3_bucket.app.bucket}"
    "database/hostname" = "${aws_db_instance.app.address}"
    "database/port"     = "${aws_db_instance.app.port}"
    "database/username" = "${aws_db_instance.app.username}"
    "database/password" = "${aws_db_instance.app.password}"
    "database/name"     = "${aws_db_instance.app.name}"
  }
}

Argument Reference

The following arguments are supported:

  • datacenter - (Optional) The datacenter to use. This overrides the datacenter in the provider setup and the agent's default datacenter.

  • token - (Optional) The ACL token to use. This overrides the token that the agent provides by default.

  • path_prefix - (Required) Specifies the common prefix shared by all keys that will be managed by this resource instance. In most cases this will end with a slash, to manage a "folder" of keys.

  • subkeys - (Required) A mapping from subkey name (which will be appended to the given path_prefix) to the value that should be stored at that key. Use slashes, as shown in the above example, to create "sub-folders" under the given path prefix.

Attributes Reference

The following attributes are exported:

  • datacenter - The datacenter the keys are being read/written to.

© 2018 HashiCorp
Licensed under the MPL 2.0 License.
https://www.terraform.io/docs/providers/consul/r/key_prefix.html