Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Terraform Conditional Variables

I have a template in my terraform config to which I write the values of a variable like this:

data "template_file" "config" {
  template = "${file("${path.module}/templates/${var.json_config}")}"

  vars {
    is_enabled = "${var.is_enabled}"
  }
}

Now is_enabled is a boolean string which is either set to true or false. Now based on if this is true or false I want to set another variable. In pseudocode it would look like this:

if is_enabled == true path = /one/path/ else path = /another/path

I had a look at the conditional values but it seems to be for bringing up resources. how would I use this to set a variable in a template file ?

like image 369
letsc Avatar asked Feb 05 '26 12:02

letsc


1 Answers

Templates uses the same interpolation syntax as all other strings in Terraform. Documentation is available

So in your case it will look like this:

path = ${is_enabled ? "/one/path/" : "/another/path"}
like image 73
Hauleth Avatar answered Feb 08 '26 03:02

Hauleth



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!