-1

Why is this not working for me? I have seen this code working everywhere but I keep getting the following error. So frustrating...

Error: Error running plan: 1 error(s) occurred:

* azurerm_virtual_machine.main: 2 error(s) occurred:

* azurerm_virtual_machine.main[0]: Resource 'azurerm_network_interface.main' does not have attribute 'id' for variable 'azurerm_network_interface.main.*.id'
* azurerm_virtual_machine.main[1]: Resource 'azurerm_network_interface.main' does not have attribute 'id' for variable 'azurerm_network_interface.main.*.id'







resource "azurerm_network_interface" "main" {
  count               = "${length(var.hostname)}"
  name                = "${var.hostname[count.index]}-nic"
  location            = "${azurerm_resource_group.main.*.location}"
  resource_group_name = "${azurerm_resource_group.main.*.name}"

  ip_configuration {
    name                          = "${var.hostname[count.index]}-ipaddress"
    subnet_id                     = "${var.subnet[count.index]}"
    private_ip_address_allocation = "Dynamic"
  }
}

resource "azurerm_virtual_machine" "main" {
  name                  = "${var.aplication}-${var.region[count.index]}-${var.hostname[count.index]}-vm"
  location              = "${azurerm_resource_group.main.*.location}"
  resource_group_name   = "${azurerm_resource_group.main.*.name}"
  network_interface_ids = ["${element(azurerm_network_interface.main.*.id, count.index)}"]
  vm_size               = "Standard_DS1_v2"

  # Uncomment this line to delete the OS disk automatically when deleting the VM
  delete_os_disk_on_termination = true

  # Uncomment this line to delete the data disks automatically when deleting the VM
  # delete_data_disks_on_termination = true

  storage_image_reference {
    publisher = "Canonical"
    offer     = "UbuntuServer"
    sku       = "16.04-LTS"
    version   = "latest"
  }

  storage_os_disk {

    name              = "osdisk"
    caching           = "ReadWrite"
    create_option     = "FromImage"
    managed_disk_type = "Standard_LRS"
  }

  os_profile {
    computer_name  = "${var.hostname[count.index]}"
    admin_username = "testadmin"
    admin_password = "Password1234!"
  }

  os_profile_linux_config {
    disable_password_authentication = false
  }

  tags {
    environment = "staging"
  }
  count = "${length(var.hostname)}"
  depends_on = ["azurerm_network_interface.main"]
}
0

It looks like your construct with setting Count is not producing the expected array of resources but a single resource instead.

You should be able to verify that with a debug log enabled run, by callling TF_LOG=DEBUG terraform plan.

  • Thanks @marenkay , setting the variable "TF_LOG=DEBUG" was very useful and it lead me to find where the problem was. As I'm using some variables as lists, for example: region = ["westeurope", "eastasia"]and attributes were expecting a string the loop was not working: Errors: [location must be a single value, not a list I had to use the same construct was in network_interface_ids = ["${element(azurerm_network_interface.main.*.id, count.index)}"]to be able to assign those variables. – Juanjo Oct 15 '18 at 12:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.