'Generation of a set to be used in for_each
Is there a way to generate a set
from an Integer? It would convenient if we have a number_of_instances
parameter that is converted to set to be used in for_each
Meta-Argument. In result, we don't need to write:
for_each = toset(["one", "two", "three",])
In stead, we just set a parameter:
number_of_instances = 3
For now I came up with this:
variable "number_of_instances" {
type = number
default = 0
}
locals {
i = [
"a",
"b",
"c",
"d",
"e",
"f",
"g",
"h",
"i",
"j",
]
}
resource "null_resource" "main" {
for_each = toset(slice(local.i, 0, var.number_of_instances))
connection {
name = each.key
}
}
Solution 1:[1]
May you could the following:
variable "number_of_instances" {
default = 3
}
locals {
numbers = ["one", "two", "three", "four", "five", "six"]
number_set = slice(local.numbers, 0, var.number_of_instances)
}
and then use:
for_each = toset(local.number_set)
Off course numbers
must be pre-populated first.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Marcin |