sfell77
sfell77

Reputation: 986

Terraform conditional source in MODULE

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}"]

Upvotes: 3

Views: 11830

Answers (2)

pascalwhoop
pascalwhoop

Reputation: 3101

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.

Upvotes: 2

Bigdom
Bigdom

Reputation: 1

Variables are not allowed to be used in the module source parameter. There also does not seem to be a plan for this to change. https://github.com/hashicorp/terraform/issues/1439 . Creating a wrapper script , or using something like mustache http://mustache.github.io/ seems to be the best way to solve the problem.

Upvotes: 0

Related Questions