terraform icon indicating copy to clipboard operation
terraform copied to clipboard

Allow output of a module to be used as input to the same module when iterating the module

Open JonathonAnderson opened this issue 1 year ago • 1 comments

Terraform Version

Terraform v1.8.3

Use Cases

The use case for this feature request is to allow resources output from one iteration of a module to be used as input to other iterations of the module.

Normally, when you try to make the output of a module an input to the some module, you get this error...   Error: Cycle: module.a.var.input (expand), module.a (close) because module.a.var.input -> module.a -> module.a.var.input

This makes sense when there is only one iteration of the module, but with multiple iterations the following scenario is valid

module.a["one"].input -> module.a["two"]

This would really be determined by what resources are needed within the module

resource new_resource demo {
    property = module.a[var.module-to-reference].some-output.value
}

So the graph could be determined by looking at

module.a["one"].resource.demo -> module.a["two"].some-output.value or module.a["one"] -> module.a["two"]

This would be especially useful where one might have a core set of resources that is only referenced by other groups of resources. For example, in Azure I might have a core resource group with a primary vnet. I want to peer it to another vnet in another resource group. The peering is nested under the resource group module.

Attempted Solutions

Moving resources that depend on cross referencing to the same module as the iterated module

Proposal

Refine the dependency graph to take iterations of modules or resources into account

References

No response

JonathonAnderson avatar Jul 30 '24 13:07 JonathonAnderson

Hi @JonathonAnderson,

Thanks for filing the issue. This has been researched in the past; it's essentially the same issue as resources (#23735), and relatedly in recursive modules (#27248).

The problem is a bit more complicated than "refine the dependency graph" makes it sound -- the architecture of Terraform is based on instance expansion happening dynamically, after all dependencies have already been resolved. The impasse starts with something like module.a["one"] -> module.a["two"], but because "one" and "two" don't initially exist the dependency is actually module.a -> module.a.

jbardin avatar Jul 30 '24 14:07 jbardin