Using Count on module, but we do not want one resource inside to be duplicated


We use a module to deploy VM’s to GCP, and we use the count feature to deploy multiples VM. But in the module, we crate a Snapshot policy that only needs to be created once, and will crash if we try to create it twice.

so, if we use a count higher then 1, is there some tricks to prevent the creation of that policy resource more then once?

Remove that resource from the module, and define it separately.

Yes, that’s what we will do if there is no other alternative.

Or you could pass in a boolean variable (only set to true for one of the instances) to enable/disable that single resource.

I’m assuming @kinwolfqc wishes to reference the created resource (a snapshot policy for VMs) from other resources (the VM) in the module - in that case, I don’t think it will be possible to make conditional creation within one instance of the module, also permit referencing the created resource within other instances.

Ah, yes, I forgot that we do need to reference it from within the module(we assign the policy to disks). I guess we have no choice but to get it out of the module. Thanks!

The only other option would be to use repetition in the individual resources inside the module rather than the module as a whole.

If you specify that you want multiple instances of an entire module, that means multiple independent instances of everything in the module, without those objects being aware of each other. If you specify that you want only one Instance of a module, you can still declare multiple instances of objects inside the module.

All three of these designs (the third one being placing the singleton object outside of the module) are valid for different situations. It just depends on what abstraction you are intending to create.