Conditional include for files in a terraform workpace?

I’m familiar with using for_each and count to conditionally create single resources and dynamic blocks. I’m wondering if there’s a way to apply a conditional to all of the resources in a specific .tf file?

The use case is that I’m writing a module for an azure linux VM. Most of the time, we’d just connect the VM to an existing subnet, but if we don’t want to for some reason (say, we’re doing e2e module testing, or just throwing something up there for a quick hackathon or PoC) I want to module to create the vnets/subnets/NIC/Bastion necessary.

I know I can just copy/paste the conditional for each of the resources, but that feels inelegant.

The only other option I can think of is writing a “vm networking” module specifically for these use cases, but that also feels like the wrong answer for some reason.

TIA

I have seen the need for this and seen this question asked, but I don’t think I’ve ever seen a solution.

There are variables for the workspace name, but given that Terraform already includes any .tf files and doesn’t have any “include” syntax for .tf files that I know of, I don’t think this would be possible, except by suppressing at the resource level.

Yeah, that’s kind of what I was afraid of. Thanks!

That’s correct, configuration can’t be optionally included. The unit of composition in Terraform is the module, and even as a module configuration is evaluated as a whole so there is no way to optionally include configuration files.

The usual way to do this is to move the group of resources which may not always be needed into a local module, and then conditionally create an instance of that module.