Allows Terraform to manage a Consul prepared query.
Managing prepared queries is done using Consul's REST API. This resource is useful to provide a consistent and declarative way of managing prepared queries in your Consul cluster using Terraform.
# Creates a prepared query myquery.query.consul that finds the nearest
# healthy myapp.service.consul instance that has the active tag and not
# the standby tag.
resource "consul_prepared_query" "myapp-query" {
name = "myquery"
datacenter = "us-central1"
token = "abcd"
stored_token = "wxyz"
only_passing = true
near = "_agent"
service = "myapp"
tags = ["active", "!standby"]
failover {
nearest_n = 3
datacenters = ["us-west1", "us-east-2", "asia-east1"]
}
dns {
ttl = "30s"
}
}
# Creates a Prepared Query Template that matches *-near-self.query.consul
# and finds the nearest service that matches the glob character (e.g.
# foo-near-self.query.consul will find the nearest healthy foo.service.consul).
resource "consul_prepared_query" "service-near-self" {
datacenter = "nyc1"
token = "abcd"
stored_token = "wxyz"
name = ""
only_passing = true
connect = true
near = "_agent"
template {
type = "name_prefix_match"
regexp = "^(.*)-near-self$"
}
service = "$${match(1)}"
failover {
nearest_n = 3
datacenters = ["dc2", "dc3", "dc4"]
}
dns {
ttl = "5m"
}
}
name
(String) The name of the prepared query. Used to identify the prepared query during requests. Can be specified as an empty string to configure the query as a catch-all.service
(String) The name of the service to queryconnect
(Boolean) When true
the prepared query will return connect proxy services for a queried service. Conditions such as tags
in the prepared query will be matched against the proxy service. Defaults to false.datacenter
(String) The datacenter to use. This overrides the agent's default datacenter and the datacenter in the provider setup.dns
(Block List, Max: 1) Settings for controlling the DNS response details. (see below for nested schema)failover
(Block List, Max: 1) Options for controlling behavior when no healthy nodes are available in the local DC. (see below for nested schema)ignore_check_ids
(List of String) Specifies a list of check IDs that should be ignored when filtering unhealthy instances. This is mostly useful in an emergency or as a temporary measure when a health check is found to be unreliable. Being able to ignore it in centrally-defined queries can be simpler than de-registering the check as an interim solution until the check can be fixed.near
(String) Allows specifying the name of a node to sort results near using Consul's distance sorting and network coordinates. The magic _agent
value can be used to always sort nearest the node servicing the request.node_meta
(Map of String) Specifies a list of user-defined key/value pairs that will be used for filtering the query results to nodes with the given metadata values present.only_passing
(Boolean) When true
, the prepared query will only return nodes with passing health checks in the result.service_meta
(Map of String) Specifies a list of user-defined key/value pairs that will be used for filtering the query results to services with the given metadata values present.session
(String) The name of the Consul session to tie this query's lifetime to. This is an advanced parameter that should not be used without a complete understanding of Consul sessions and the implications of their use (it is recommended to leave this blank in nearly all cases). If this parameter is omitted the query will not expire.stored_token
(String) The ACL token to store with the prepared query. This token will be used by default whenever the query is executed.tags
(Set of String) The list of required and/or disallowed tags. If a tag is in this list it must be present. If the tag is preceded with a "!" then it is disallowed.template
(Block List, Max: 1) Query templating options. This is used to make a single prepared query respond to many different requests (see below for nested schema)token
(String, Sensitive, Deprecated) The ACL token to use when saving the prepared query. This overrides the token that the agent provides by default.id
(String) The ID of this resource.dns
Optional:
ttl
(String) The TTL to send when returning DNS results.failover
Optional:
datacenters
(List of String) Remote datacenters to return results from.nearest_n
(Number) Return results from this many datacenters, sorted in ascending order of estimated RTT.targets
(Block List) Specifies a sequential list of remote datacenters and cluster peers to failover to if there are no healthy service instances in the local datacenter. This option cannot be used with nearest_n
or datacenters
. (see below for nested schema)failover.targets
Optional:
datacenter
(String) Specifies a WAN federated datacenter to forward the query to.peer
(String) Specifies a cluster peer to use for failover.template
Required:
regexp
(String) The regular expression to match with. When using name_prefix_match
, this regex is applied against the query name.type
(String) The type of template matching to perform. Currently only name_prefix_match
is supported.Optional:
remove_empty_tags
(Boolean) If set to true, will cause the tags list inside the service structure to be stripped of any empty strings.Import is supported using the following syntax:
terraform import consul_prepared_query.my_service 71ecfb82-717a-4258-b4b6-2fb75144d856