Error: Incorrect attribute value type
main.tf line 47, in resource "azurerm_network_interface_backend_address_pool_association" "nic-lbpool":
47: backend_address_pool_id = [module.azure_lb.modout_poolid]
Inappropriate value for attribute "backend_address_pool_id": string required.
The underlying problem here is that when you have a resource that can have either zero or one instances then Terraform expects you to describe how to deal with the situation where there are zero instances. In the example you shared, you’ve made the module return either a zero-element or one-element list of ids, and it’s the parent module’s responsibility to deal with that.
I’d suggest that ideally the child module should be the one responsible for handling the case where there are zero items, so that parent modules don’t need to worry about it. That does require deciding what value to return for the modout_poolid output when there is no pool. In this case, where there is no pool in that case, I’d probably design it to return a null pool id in that case:
The calling module can then just refer directly to module.azure_lb.pool_id, without the need to index [0] itself.
What I’ve described here will, in the end, have the same behavior as what you showed, but I just wanted to note this as a slightly different approach that encapsulates this behavior inside the child module, thus making the module more convenient to use by its callers.
I appreciate your feedback on this. I have implemented the approach you suggested, i am getting expected results and most importantly the code is now much cleaner. I am reading that Terraform 0.13 has “count” support in module, so i think we can enable/disable module in a better way.