Search code examples
terraformterraform-provider

Terraform loop newbie issue


I'm trying to deploy a few VMs via Terraform to Azure, and I have an issue with the loop.

My code:
Part of variables.tf

variable "vm_instance_detail" {
type = map
default = {
    vm1 = {
        name = "vm-01"
        size = "Standard_B1ls"
    }
    vm2 = {
        name = "vm-02"
        size = "Standard_B1ls"
    }
} }

main.tf

 resource "azurerm_network_interface" "nic" {
  for_each = var.vm_instance_detail

  name                  = each.value.name
  location              = var.location
  resource_group_name   = var.name_rg

  ip_configuration {
      name                          = var.ip_configuration.name
      subnet_id                     = data.azurerm_subnet.subnet.id
      private_ip_address_allocation = var.ip_configuration.private_ip_address_allocation
  }
  depends_on = [azurerm_resource_group.rg]
}

resource "azurerm_linux_virtual_machine" "VM" {
  for_each = var.vm_instance_detail
  name                  = each.value.name
  resource_group_name   = var.name_rg
  location              = var.location
  size                  = each.value.size
  admin_username        = var.admin_username
  admin_password        = var.admin_password
  disable_password_authentication = var.disable_password_authentication

  network_interface_ids = [
    azurerm_network_interface.nic[each.key].id,
  ]

When I tries to do terraform plan got the error:

Error: Cycle: azurerm_linux_virtual_machine.vm["vm2"], azurerm_linux_virtual_machine.vm["vm1"]

Maybe who has any advice on my case? Thanks.


Solution

  • Thank You Marko E for your suggestion and MTLTR_ you resolved your issues by applying the suggestion. I have also tested in my enviroment and found the issue with depends_on = [azurerm_resource_group.rg] . This is unneeded parameter you were mentioning. Sharing the full code it could help other community member who might be encounter the same issue in future.

    provider "azurerm"{
      features{
    
      }
    }
    
    data "azurerm_resource_group" "example" {
      name     = "v-XXXXXXXee"
      # location = "West Europe"
    }
    
    resource "azurerm_virtual_network" "example" {
      name                = "example-network"
      address_space       = ["10.0.0.0/16"]
      location            = data.azurerm_resource_group.example.location
      resource_group_name = data.azurerm_resource_group.example.name
    }
    
    resource "azurerm_subnet" "example" {
      name                 = "internal"
      resource_group_name  = data.azurerm_resource_group.example.name
      virtual_network_name = azurerm_virtual_network.example.name
      address_prefixes     = ["10.0.2.0/24"]
    }
    
    resource "azurerm_network_interface" "nic" {
      for_each = var.vm_instance_detail
    
      name                  = each.value.name
      location              = data.azurerm_resource_group.example.location
      resource_group_name   = data.azurerm_resource_group.example.name
    
      ip_configuration {
          name                          = "internal"
          subnet_id                     = azurerm_subnet.example.id
          private_ip_address_allocation = "Dynamic"
      }
      
    }
    
    resource "azurerm_linux_virtual_machine" "VM" {
      for_each = var.vm_instance_detail
      name                  = each.value.name
      resource_group_name   = data.azurerm_resource_group.example.name
      location              = data.azurerm_resource_group.example.location
      size                  = each.value.size
      admin_username        = var.admin_username
      admin_password        = var.admin_password
      disable_password_authentication = var.disable_password_authentication
    
      network_interface_ids = [
        azurerm_network_interface.nic[each.key].id,
      ]
    
      os_disk {
        caching              = "ReadWrite"
        storage_account_type = "Standard_LRS"
      }
    
      source_image_reference {
        publisher = "Canonical"
        offer     = "UbuntuServer"
        sku       = "16.04-LTS"
        version   = "latest"
      }
    
    }
    

    enter image description here

    OutpUt --

    enter image description here