I would like your technical advice on the way to achieve a specific variable attribution with Terraform. Regardless of the context, I have n objects to which I would like to attribute a value between [1-3] so that the values are equally distributed (whether it is in a sequential or random way does not really matter).
To be more explicit here are the two ways I thought of :
OR
I can easily achieve the first step with the random provider, but I have no clue on how to proceed for the other steps. I mean I know how to increment a value, but how to proceed when the value is 3 and I want to start the loop over and come back to value 1 ? Or how to randomly select values between remaining ones ?
You mentioned that the way doesn't really matter... I think in a sequential way is simpler, using range and a loop with a math trick (i % 3) + 1
to get the value between [1-3] like you want
variable "objects" {
default = 12
}
locals {
distrib = [ for i in range(var.objects) : (i % 3) + 1 ]
}
output "test" {
value = local.distrib
}
a terraform plan on that:
Changes to Outputs:
+ test = [
+ 1,
+ 2,
+ 3,
+ 1,
+ 2,
+ 3,
+ 1,
+ 2,
+ 3,
+ 1,
+ 2,
+ 3,
]
We can do a random start with the help of the random_integer
resource and using the start and limit parameters in the range function
variable "objects" {
default = 12
}
resource "random_integer" "extra" {
min = 0
max = 2
}
locals {
start = random_integer.extra.result
distrib = [for i in range(local.start, var.objects + local.start) : (i % 3) + 1]
}
output "test" {
value = local.distrib
}
Randomization is also possible using the random_shuffle
resource:
https://registry.terraform.io/providers/hashicorp/random/latest/docs/resources/shuffle
variable "objects" {
default = 12
}
locals {
distrib = [ for i in range(var.objects) : (i % 3) + 1 ]
}
resource "random_shuffle" "random" {
input = local.distrib
result_count = var.objects
}
output "test" {
value = random_shuffle.random.result
}
and a terraform apply on that:
Apply complete! Resources: 1 added, 0 changed, 1 destroyed.
Outputs:
test = tolist([
"3",
"1",
"3",
"2",
"2",
"3",
"1",
"1",
"2",
"2",
"1",
"3",
])
We can also use other values instead of [1-3]
see sample below using colors
variable "objects" {
default = 9
}
variable "items" {
default = ["red", "blue", "cyan"]
}
locals {
distrib = [for i in range(var.objects) : element(var.items, (i % length(var.items)))]
}
resource "random_shuffle" "random" {
input = local.distrib
result_count = var.objects
}
output "test" {
value = random_shuffle.random.result
}