title: Terraform: Assign resource attribute to resource argument using If/Else pattern
body: <p>I am using the <a href="https://blog.gruntwork.io/terraform-tips-tricks-loops-if-statements-and-gotchas-f739bbae55f9" rel="nofollow noreferrer">IF/ELSE pattern</a> in Terraform to build a NIC with or without a public IP.  The problem comes when assigning the NIC.  I can't find a technique that lets me choose the resource that was used to create the NIC (with or without public IP).  Using ternary operations fail because one of the resources doesn't exist.  Putting the resources in a list doesn't interpolate.  How do I assign the correct resource output?</p>
  
  <pre><code>resource "azurerm_public_ip" "public_ip" {
      count                        = "${var.assign_public_ip}"
      name                         = "${format("${var.name}-pip%02d", count.index)}"
      location                     = "${var.location}"
      resource_group_name          = "${var.resource_group_name}"
      public_ip_address_allocation = "static"
  
      tags {
          environment = "${var.resource_group_name}"
      }
  }
  
  resource "azurerm_network_interface" "nic_with_public_ip" {
      count               = "${var.assign_public_ip}"
      name                = "${format("${var.name}-nic%02d", count.index)}"
      location            = "${var.location}"
      resource_group_name = "${var.resource_group_name}"
  
      ip_configuration {
          name                          = "ip_cfg"
          subnet_id                     = "${var.subnet_id}"
          private_ip_address_allocation = "dynamic"
          public_ip_address_id          = "${azurerm_public_ip.public_ip.id}"
      }
  }
  
  resource "azurerm_network_interface" "nic" {
      count               = "${1 - var.assign_public_ip}"
      name                = "${format("${var.name}-nic%02d", count.index)}"
      location            = "${var.location}"
      resource_group_name = "${var.resource_group_name}"
  
      ip_configuration {
          name                          = "ip_cfg"
          subnet_id                     = "${var.subnet_id}"
          private_ip_address_allocation = "dynamic"
      }
  }
  
  resource "azurerm_virtual_machine" "centos" {
      count                 = "${var.count}"
      name                  = "${format("${var.name}%02d", count.index)}"
      location              = "${var.location}"
      resource_group_name   = "${var.resource_group_name}"
      network_interface_ids = ["${var.assign_public_ip == 1 ? azurerm_network_interface.nic_with_public_ip.id : azurerm_network_interface.nic.id }"]
      vm_size               = "${var.size}"
      delete_os_disk_on_termination = true
      delete_data_disks_on_termination = true
  
      storage_image_reference {
          publisher = "OpenLogic"
          offer     = "CentOS"
          sku       = "7.3"
          version   = "latest"
      }
  
      storage_os_disk {
          name              = "${format("${var.name}-osdisk%02d", count.index)}"
          caching           = "ReadWrite"
          create_option     = "FromImage"
      }
  
      os_profile {
          computer_name  = "${format("${var.name}%02d", count.index)}"
          admin_username = "${var.admin_user}"
      }
  
      os_profile_linux_config {
          disable_password_authentication = true
          ssh_keys = {
              path = "/home/${var.admin_user}/.ssh/authorized_keys"
              key_data = "${var.ssh_key}"
      }
    }
  
    tags {
        environment = "${var.name}"
    }
  }
  </code></pre>
  
  <p>This fails with the following error:
      Error running plan: 1 error(s) occurred:</p>
  
  <pre><code>* module.jumphost.azurerm_virtual_machine.centos: 1 error(s) occurred:
  
  * module.jumphost.azurerm_virtual_machine.centos: Resource 'azurerm_network_interface.nic' not found for variable 'azurerm_network_interface.nic.id'
  </code></pre>