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 :
- randomly selects starting value (1 or 2 or 3)
- selects the next index (if 2 were to be selected as starting value, 3 would be selected next)
- if there are 4 or more objects on total the loop should start over and over as long as there are remaining objects
OR
- randomly selects starting value (1 or 2 or 3)
- selects randomly any of the remaining value
- if there are 4 or more objects on total, randoms values would be selected so the global distribution would be equal
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 ?
sequential
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) + 1to get the value between [1-3] like you wanta terraform plan on that:
sequential with random start
We can do a random start with the help of the
random_integerresource and using the start and limit parameters in the range functionrandom
Randomization is also possible using the
random_shuffleresource:https://registry.terraform.io/providers/hashicorp/random/latest/docs/resources/shuffle
and a terraform apply on that:
more complex
We can also use other values instead of
[1-3]see sample below using colors