I’m having an issue where when I add new items to a list being used as the input for random_shuffle
it doesn’t seem to recognize that the random_shuffle
will change, and blows up if you try to reference the index of a newly added item.
For example:
Original Terraform
locals {
things = [
"CW1",
"CW2",
"CW3"
]
}
resource "random_shuffle" "random_things" {
input = local.things
}
output "random_things" {
value = random_shuffle.random_things.result
}
output "random_index_test" {
value = index(random_shuffle.random_things.result, "CW3")
}
Original Plan
Terraform will perform the following actions:
# random_shuffle.random_things will be created
+ resource "random_shuffle" "random_things" {
+ id = (known after apply)
+ input = [
+ "CW1",
+ "CW2",
+ "CW3",
]
+ result = (known after apply)
}
Plan: 1 to add, 0 to change, 0 to destroy.
Changes to Outputs:
+ random_index_test = (known after apply)
+ random_things = (known after apply)
Original Apply:
random_shuffle.random_things: Creating...
random_shuffle.random_things: Creation complete after 0s [id=-]
Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
Outputs:
random_index_test = 1
random_things = [
"CW1",
"CW3",
"CW2",
]
But if I add another item to the original list of things being shuffled, and try to look up the index of that item in the shuffled list, it doesn’t recognize that the shuffled list should be changed and tries to lookup the item in the old shuffled list where the item doesn’t exist, and fails in the Plan job with an item not found error.
Example:
Updated Terraform: (added CW4 item)
locals {
things = [
"CW1",
"CW2",
"CW3",
"CW4"
]
}
resource "random_shuffle" "random_things" {
input = local.things
}
output "random_things" {
value = random_shuffle.random_things.result
}
output "random_index_test" {
value = index(random_shuffle.random_things.result, "CW4")
}
Updated Plan: (failure)
random_shuffle.random_things: Refreshing state... [id=-]
Error: Error in function call
on main.tf line 21, in output "random_index_test":
21: value = index(random_shuffle.random_things.result, "CW4")
|----------------
| random_shuffle.random_things.result is list of string with 3 elements
Call to function "index" failed: item not found.
Seems like with the original/first plan, it should recognize that these things will be (known after apply)
and not try to resolve them in the plan.
Any thoughts?
I’ve tried adding dependencies to the shuffle on the output, and messing with keepers on the shuffle, but no dice.