Reputation: 2465
Given a .tfvars
containing:
environment = "dev"
web_servers = ["192.168.0.1","192.168.0.2"]
and a node
resource like:
resource "node" "web_nodes" {
count = "${length(var.web_servers)}"
name = "${var.environment}_web_${count.index}"
address = "${element(var.web_servers, count.index)}"
}
I want to express the following dynamically:
resource "pool" "homepage_pool" {
name = "homepage_${var.environment}"
nodes = [
"dev_web_0:443",
"dev_web_1:443"
]
}
with a plan output like:
nodes.2387117855: "" => "dev_web_1:443"
nodes.3005787823: "" => "dev_web_0:443"
I think that pool.nodes
should be generated using something like formatlist()
or join()
or a combination of the 2. Struggling to find decent examples.
The closest I can get results in a plan like:
nodes.467461356: "" => "\"/Common/preprod_web_0\","/Common/preprod_web_1\""
Upvotes: 3
Views: 5787
Reputation: 74064
The formatlist
function can be used in conjunction with the "splat" operator to get the result you need here.
The "splat" operator allows you to concisely create a list from a given attribute of a set of count
ed resource instances. Your example here it seems that you want to use the node name and a hard-coded port 443, in which case this can be expressed as follows:
resource "pool" "homepage_pool" {
name = "homepage_${var.environment}"
nodes = "${formatlist("%s:443", node.web_nodes.*.name)}"
}
The notation node.web_nodes.*.name
means to take the name
attribute of each of the instances created under the name node.web_nodes
and produce a list of strings from them. formatlist
then applies the given format to each element of the list in turn, appending the :443
port suffix.
Upvotes: 5