Output from a module that has conditional count = 0

Hi.
how to have outputs with modules not used (count=0)? I get this error message:

Error: Unsupported attribute

on front.tf line 913, in output “frontend_fqdn”:
913: value = module.front.frontend_fqdn

This value does not have any attributes.

All examples I see do not use outputs.

Thanks.

Hi @sdouche,

When you set count for a resource or a module, the value representing that resource or module in expressions becomes a list of objects rather than just a single object, and so that is why Terraform is telling you that module.front doesn’t have any attributes: only object values have attributes.

Because your module is conditional, you must decide what you want this output to return in the case where the module wasn’t instantiated. Once possibility would be to have it be null in that case:

output "frontend_fqdn" {
  value = length(module.front) > 0 ? module.front.frontend_fqdn : null
}

If this is an output of a shared module that is called from another module then the calling module will then need to itself handle the case where this value is null, using similar conditional expressions. If there’s a sensible default value for this output then you could return that instead of null and thus free the caller from dealing with that, but in this case I don’t think that would be true since if the “frontend” isn’t enabled at all then there presumably isn’t any FQDN to return for it.

2 Likes