Reputation: 3327
lets say I have some terraform vars: variable "cluster" {}
and variable "kfkcount" {}
and lets say cluster=test
and kfkcount=3
how can I use terraform to turn that into a list that looks like this?
["test1.c.com", "test2.c.com", "test3.c.com"]
Upvotes: 3
Views: 9395
Reputation: 21
Quite late to the party but if you end up here like me. This can be solved with the range
function.
formatlist("${var.cluster}%s.c.com", range(1, var.kfkcount + 1))
will output:
tolist(["test1.c.com", "test2.c.com", "test3.c.com",])
Upvotes: 2
Reputation: 293
This should do what you want:
variable "cluster" {}
variable "kfkcount" {}
data "template_file" "test" {
template = "$${cluster}$${index}.c.com"
count = "${var.kfkcount}"
vars = {
index = "${count.index + 1}"
cluster = "${var.cluster}"
}
}
output "list" {
value = "${data.template_file.test.*.rendered}"
}
It's a bit of a hack using the template_file
resource, however it will return the list as required:
$ terraform apply
var.cluster
Enter a value: test
var.kfkcount
Enter a value: 3
data.template_file.test[0]: Refreshing state...
data.template_file.test[1]: Refreshing state...
data.template_file.test[2]: Refreshing state...
Apply complete! Resources: 0 added, 0 changed, 0 destroyed.
Outputs:
list = [
test1.c.com,
test2.c.com,
test3.c.com
]
Upvotes: 10
Reputation: 419
EDIT
One thing that can help is to use count for looping, and using the count.index
as the value for the names.
So you could end up with something like this:
resource ".." "..." {
count = "${var.kfkcount}"
something = "${var.cluster}${count.index}.c.com"
}
There is even one example that shows a simple usage of count and, also, list expansion with *
.
Have you tried using list(items, ...)
described in the Interpolation Syntax page?
Something in the like of
"${list("${var.cluster}", "${var.kfkcount}")}"
Upvotes: 1