How to call attributes from child module to another child module?

Is there a way that I can call an attribute from child module -application gateway to child module -subnet?

from app-gateway/main.tf


need to get a value for subnet_id which is in another child module.

The more tightly coupled way is to make an output in the other module, and reference that output (module.foo.outputs.subnet_id or whatever you name the output). If the two are in different states, you’ll also need to add a remote state data resource.

The other way (which would be more loosely coupled to the other module, and probably not what you want, especially if the modules are both instantiated in the same state) would be to use a data resource for the subnet, and use that to get the ID.

Using a data source to access a managed resource which is defined within the same configuration does not make them loosely coupled, but rather it can introduce errors or unexpected behavior. If the dependencies are setup correctly, then the data source may not be readable during a plan when you expect it to be, though this is usually not a problem if the rest of the configuration is correct. The coupling is still there becasue you need to connect the dependencies for planning. If the dependency is not explicitly defined, then you will get an incorrect value in a plan where there is a change, or an error during an initial apply with no prior state.

The correct solution is to export the value from the module using an output so that terraform can plan for unknown values during changes.

1 Like