Terraform Inheritance/Overrides

Does terraform offer any type of inheritance or overrides?

Essentially, we would like to have a baseline terraform configuration for our Cloudflare domains. However, there are times when some domains may need to have some exceptions or exclusions and may need some Cloudflare resources configured differently than the baseline terraform configuration.

Does terraform support any type of inheritance or overrides to be able to handle deviations from a baseline/standard configuration? Where one main.tf file could override another main.tf in a sub-directory.

Please see attached image for a simple example. Please let me know if I should elaborate further.

I think you could get some if this behaviour by using default values for your variables when you declare them in a shared variables.tf and then override that using terraform.tfvars in the subfolders which also contain symlinks to all the shared .tf files

Thanks for the idea. As I continued to ponder on this, I have decided to try out the following approach.

Create a terraform module with a var.tf file containing all default (or baseline) variable values. When calling the module, if needing to override one of the variables in the module’s var.tf file, pass the variable into where the module is called in the main.tf file (let me know if anyone reading this needs a more detailed explanation of this!)

If this solution fails me in the future, I will revisit symlinks. Thanks!

Hi @biker2o,

In addition to what @benterp suggested, you might find it interesting to read the Module Composion guide. Terraform’s design generally favors composition over inheritance, so if you can reframe your problem as one of composing smaller parts together rather than as inheriting and overriding then the patterns described in that guide might be helpful.

The crux of the idea is to break one big module up into smaller modules that are designed to work together (by passing the outputs values of one into the input variables of another) and then the callers of these modules can decide which of the modules to use in a particular situation, and possibly combine them in different ways, depending on how the modules are designed.

1 Like