Search code examples
azureterraformterraform-provider-azureazure-virtual-network

Terraform Error While trying to create 2 VMs with NICs Attached


I am trying to create two NICs to be attached to two VMs. The NICs are already created, but now i am trying to create the VMs and use the NICs, but i am getting the below Terraform error

resource "azurerm_linux_virtual_machine" "web_linuxvm" {
  count = var.vm-count

  name = "linuxvm-${count.index}"
  resource_group_name = var.location
  location = var.resource_group_name
  size = "Standard_DS1_v2"
  admin_username = "azureuser"
  network_interface_ids = format("nic-%s",var.nics[count.index])
  admin_ssh_key {
    username = "azureuser"
    public_key = file("${path.module}/ssh-keys/vm-ssh.pub")
  }
  os_disk {
    caching = "ReadWrite"
    storage_account_type = "Standard_LRS"
  }
  source_image_reference {
    publisher = "RedHat"
    offer = "RHEL"
    sku = "83-gen2"
    version = "latest"
  }
    
  custom_data = base64encode(local.vm_custom_data)  
  /*
  can do custom_data = filebase64("${path.module}/from-script.sh")
  would be better 
  */
}

resource "azurerm_network_interface" "network-interface" {
  count = var.vm-count

  name                = "nic-${count.index}"
  location            = var.location
  resource_group_name = var.resource_group_name

  ip_configuration {
    name                          = "vm-${count.index}"
    subnet_id                     = azurerm_subnet.subnet.id
    private_ip_address_allocation = "Dynamic"
  }
}



module "compute" {
  source = "../module/compute"
  vm-count = var.vm-count
  nics = ["nic0","nic1"]
  resource_group_name = azurerm_resource_group.rg.name
  location            = var.region
}

Planning failed. Terraform encountered an error while generating this plan.

╷ │

Error: Incorrect attribute value type
│ 
│   on ../module/compute/main.tf line 20, in resource "azurerm_linux_virtual_machine" "web_linuxvm":
│   20:   network_interface_ids = format("nic-%s",var.nics[count.index])
│     ├────────────────
│     │ count.index is 1
│     │ var.nics is tuple with 2 elements
│ 
│ Inappropriate value for attribute "network_interface_ids": list of string
│ required.
╵
╷
│ Error: Incorrect attribute value type
│ 
│   on ../module/compute/main.tf line 20, in resource "azurerm_linux_virtual_machine" "web_linuxvm":
│   20:   network_interface_ids = format("nic-%s",var.nics[count.index])
│     ├────────────────
│     │ count.index is 0
│     │ var.nics is tuple with 2 elements
│ 
│ Inappropriate value for attribute "network_interface_ids": list of string
│ required.
╵
Operation failed: failed running terraform plan (exit 1)

Solution

  • The error message states that the network_interface_ids argument requires a list, but you have provided it only a single string.

    I am not familiar with this resource type in particular but I would guess that if you want to assign only one network interface then you could assign a single-element list:

      network_interface_ids = [format("nic-%s",var.nics[count.index])]
    

    The brackets [ ] around the value are "tuple constructor" syntax, so this result would actually be a single-element tuple but Terraform can automatically convert that into a list and so the expression above should meet the provider schema's type constraint.