Search code examples
terraformkubernetes-helm

Conditional set in Terraform with Helm


I'm trying to figure out what is the best way to set helm values in terraform but for a specific condition. For example, I want the snippet below to execute only in a specific use case. otherwise, I want Terraform to ignore it. My usecase is that I want to let the end user the will run this Terraform to choose if he want to provision an internal Azure LB or a regular LB. If regular -- The public IP that also created in the code will be set in the values. If Internal -- Azure will generate automatically a private IP and set it as the "external IP" of the LB.

  set {
    name  = "controller.service.loadBalancerIP"
    value = mypuclicip
  }

Any idea how can I achieve it?


Solution

  • It feels like Terraform should have a more ergonomic way of doing this, but I believe the best solution you have available is to use a dynamic block:

    dynamic "set" {
      for_each = <condition> ? [mypublicip] : []
      content {
        name  = "controller.service.loadBalancerIP"
        value = set.value
      }
    }