Assigning VM Name to azurerm_network_interface Issue

Hi,

I’m fairly new to Terraform and IaC. Ive been playing with creating a reusable template to deploy a IaaS resources with variables for when we onboard a new customer.

I’m currently getting the error:

Error: Cycle: azurerm_network_interface.example, azurerm_windows_virtual_machine.example

I’ve managed to reproduce and isolate the error when trying to set the azurerm_network_interface name as below.

resource "azurerm_network_interface" "example" {

 

  name                = "nic-${azurerm_windows_virtual_machine.example.name}"
  location            = azurerm_resource_group.example.location
  resource_group_name = azurerm_resource_group.example.name
  ip_configuration {

    name                          = "internal"

    subnet_id                     = azurerm_subnet.example.id

    private_ip_address_allocation = "static"

    private_ip_address            = "10.${var.Customer_VNET_Octect}.3.10"

  }

}

If I remove the variable for the VM Name and just enter a string. It works. But I would like to follow Azures default naming convention when new NICs are created and create the NIC based off the VM Name.

Any ideas?

Many Thanks,

Brad

Hi @bradsherwin! Good question, one which I can answer fortunately!

What you are trying to do is possible, but the way you are doing it is creating a cyclic dependency.

You need a NIC before you can create the VM, and you want to use the VM name for the NIC.

It is possible to use the same variable you are using for the VM for the NIC as well, but using a dependency from a dependent resource is impossible:

resource "azurerm_network_interface" "example" {
  name                = "nic-${var.vm_name}"
  location            = azurerm_resource_group.example.location
  resource_group_name = azurerm_resource_group.example.name
  ip_configuration {

    name                          = "internal"
    subnet_id                     = azurerm_subnet.example.id
    private_ip_address_allocation = "static"
    private_ip_address            = "10.${var.Customer_VNET_Octect}.3.10"

  }
}

resource "azurerm_windows_virtual_machine" "example" {
  name = var.vm_name
  ...
}

Hi aristosvo,

Thanks for the reply. I should’ve pasted the VM resource also as I do not use a var to define the whole VM name.

See below.

Many Thanks,

Brad

resource "azurerm_network_interface" "example" {
  #
  #name                = "nic-${azurerm_windows_virtual_machine.example.name}"

#temp fix
  name                = "web-nic"

  location            = azurerm_resource_group.example.location

  resource_group_name = azurerm_resource_group.example.name

  ip_configuration {

    name                          = "internal"
    subnet_id                     = azurerm_subnet.example.id
    private_ip_address_allocation = "static"
    private_ip_address            = "10.${var.Customer_VNET_Octect}.2.10"

  }

}


resource "azurerm_windows_virtual_machine" "example" {

  name                = "${var.Customer_Name_Prefix}pweb01"

  resource_group_name = azurerm_resource_group.example.name

  location            = azurerm_resource_group.example.location
  size                = "Standard_B2s"
  admin_username      = "AdminUser"
  admin_password      = "*****"
  network_interface_ids = [
    azurerm_network_interface.evoweb01_nic.id,
  ]

I’d solve it then in this way:

  • First add a local variable with the VM name
  • Use this variable for the NIC and VM
locals {
  vm_name = "${var.Customer_Name_Prefix}pweb01"
}

resource "azurerm_network_interface" "example" {
  name                = "nic-${local.vm_name}"
  location            = azurerm_resource_group.example.location
  resource_group_name = azurerm_resource_group.example.name
  ip_configuration {

    name                          = "internal"
    subnet_id                     = azurerm_subnet.example.id
    private_ip_address_allocation = "static"
    private_ip_address            = "10.${var.Customer_VNET_Octect}.3.10"

  }
}

resource "azurerm_windows_virtual_machine" "example" {
  name                = local.vm_name
  resource_group_name = azurerm_resource_group.example.name
  location            = azurerm_resource_group.example.location
  size                = "Standard_B2s"
  admin_username      = "AdminUser"
  admin_password      = "*****"
  network_interface_ids = [
    azurerm_network_interface.example.id,
  ]
}
1 Like

That worked and I learnt what Local Variables are too.

Thank you very much.