azure - Terraform: Assign resource attribute to resource argument using If/Else pattern -


i using if/else pattern in terraform build nic or without public ip. problem comes when assigning nic. can't find technique lets me choose resource used create nic (with or without public ip). using ternary operations fail because 1 of resources doesn't exist. putting resources in list doesn't interpolate. how assign correct resource output?

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

this fails following error: error running plan: 1 error(s) occurred:

* module.jumphost.azurerm_virtual_machine.centos: 1 error(s) occurred:  * module.jumphost.azurerm_virtual_machine.centos: resource 'azurerm_network_interface.nic' not found variable 'azurerm_network_interface.nic.id' 

a "splat expression" can used list of values of attribute of instances created resource block count:

azurerm_network_interface.nic_with_public_ip.*.id 

this returns empty list when count = 0. in situation @ times there total of 1 across counts, it's possible exploit concat select whichever 1 present:

network_interface_ids = "${concat(azurerm_network_interface.nic_with_public_ip.*.id, azurerm_network_interface.nic.*.id)}" 

since network_interface_ids list, can assign directly result of concat here. due how count being assigned on these resources, know list have 1 elements, achieves desired result of selecting 1 active.


Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -