For example, lets say I define a couple resources this way (in .tf
format):
resource resource_type X {
name = "X"
}
resource resource_type Y {
name = "Y"
}
...
Now lets say later on I want to create new resources that will each correspond to one of the previously created resources (X,Y, ...) Is there a way to create a list that holds the previously created resource and then loop over that list like:
variable "list_of_previously_created_resources" {
type = list(resource)
default = [resource_type.X, resource_type.Y, ...]
}
# Now create corresponding resources:
resource type_Dependent d {
for_each = var.list_of_previously_created_resource
some_attribute = each.value.name
depends_on = [each.value]
}
The syntax above of course does not work, but I tried my best to give the pseudo code for what I would like to do. Importantly, I don't necessarily want to loop over every resource of the type "resource_type"
just those that I manually define in the list variable.
I don't see anything in the docs that describes the best way to do this, so here I am.
You can't declare dynamic variables. But you can use local
instead:
locals {
list_of_previously_created_resources = [resource_type.X, resource_type.Y]
}
resource type_Dependent d {
for_each = {for idx, val in local.list_of_previously_created_resource: idx => val}
some_attribute = each.value.name
}