skip to Main Content

I am defining network interface inside the VMSS AZURE Terraform
Well what i want is disable or enable the load balancer backend address pool id if $app != "api"
In other words if app is api then only add that parameter and attach pool id.

Also, How to enable or disable entire resource lets say i want to diable or enable the network interface block in whole.

Thanks in advance for helping.

load_balancer_backend_address_pool_ids       = [var.backend_address_pool_id] #enable only when var.app is api.
network_interface {
    name    = "${var.app}-vmss-nic"
    primary = true
    ip_configuration {
      name                                         = "internal"
      primary                                      = true
      subnet_id                                    = var.pvt_subnet_1_id
      load_balancer_backend_address_pool_ids       = [var.backend_address_pool_id]
    }
  }

3

Answers


  1. you would need to test this but you could possible do something like

    load_balancer_backend_address_pool_ids = var.app == "api" ? [var.backend_address_pool_id] : null
    

    or incase the provider doesnt support null and just expects an empty list

    load_balancer_backend_address_pool_ids = var.app == "api" ? [var.backend_address_pool_id] : []
    
    Login or Signup to reply.
  2. This can be done by using the ternary operator [1] and a combination of the dynamic block [2] with for_each meta-argument [3] for the network interface. To achieve this you would do the following:

    dynamic "network_interface" {
       for_each = var.app == "api" ? [1] : [0]
       content {
        name    = "${var.app}-vmss-nic"
        primary = true
        ip_configuration {
          name                                         = "internal"
          primary                                      = true
          subnet_id                                    = var.pvt_subnet_1_id
          load_balancer_backend_address_pool_ids       = [var.backend_address_pool_id] 
        }
      }
    }
    

    [1] https://developer.hashicorp.com/terraform/language/expressions/conditionals

    [2] https://developer.hashicorp.com/terraform/language/expressions/dynamic-blocks

    [3] https://developer.hashicorp.com/terraform/language/meta-arguments/for_each

    Login or Signup to reply.
  3. When I have this kind of situation, I Always use this structure:

     dynamic "azure_devops_repo" {
      for_each = var.azure_devops_repo == null ? {} : var.azure_devops_repo
      iterator = devops
     content {
      account_name    = devops.value.account_name
      branch_name     = devops.value.branch_name
      project_name    = devops.value.project_name
      repository_name = devops.value.repository_name
      root_folder     = devops.value.root_folder
      tenant_id       = lookup(devops.value, "tenant_id", null)
     }
    }
    

    As you see this is like an optional block!

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search