Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Terraform conditional source in MODULE

Tags:

terraform

I am trying to set a module's source (this IS NOT a resource) based on a conditional trigger but it looks like the module is getting fired before the logic is applied:

module "my_module" {
    source   = "${var.my_field == "" ? var.standard_repo : var.custom_repo}"
    stuff...
    more stuff...
}

I have created the standard_repo and custom_repo vars as well and defined with URLs for respective repos (using git:: -- this all works w/o conditional)

All this being said, anyone know of a way to implement this conditional aspect? (again, this is a module and not a resource)

I tried using duplicate modules and calling based off the var value but this, too, does not work (condition is never met, even when it is):

repo = ["${var.my_field == "na" ? module.my_module_old : module.my_module_new}"]
like image 219
sfell77 Avatar asked Jan 23 '26 18:01

sfell77


1 Answers

One way to achieve this is described in this post

Basically, a common pattern is to have several folders for different environments such as dev/tst/prd. These environments often reuse large parts of the codebase. Some may be abstracted as modules, but there is still often a large common file which is either copy-pasted or symlinked.

The post offers a way that doesn't conditionally disable based on variables but it probably solves your issue of enabling a module based on different enviornments. It makes use of the override feature of terraform and adds a infra_override.tf file. Here, it defines a different source for the module which points to an empty directory. Voila, a disabled module.

like image 190
pascalwhoop Avatar answered Jan 25 '26 16:01

pascalwhoop



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!