peter johnan
peter johnan

Reputation: 21

Create multiple VM's in Azure with different configuration

I want to create two vm's with terraform in Azure. I have configured two "azurerm_network_interface" but when I try to apply the changes, I receive an error. Do you have any idea? Is there any issue if I try to create them on different regions?

The error is something like: vm2-nic was not found azurerm_network_interface

# Configure the Azure Provider
provider "azurerm" {
  subscription_id = var.subscription_id
  tenant_id       = var.tenant_id
  version = "=2.10.0"
  features {}
}

resource "azurerm_virtual_network" "main" {
  name                = "north-network"
  address_space       = ["10.0.0.0/16"]
  location            = "North Europe"
  resource_group_name = var.azurerm_resource_group_name
}

resource "azurerm_subnet" "internal" {
  name                 = "internal"
  resource_group_name  = var.azurerm_resource_group_name
  virtual_network_name = azurerm_virtual_network.main.name
  address_prefix       = "10.0.2.0/24"
}

resource "azurerm_public_ip" "example" {
  name                    = "test-pip"
  location                = "North Europe"
  resource_group_name     = var.azurerm_resource_group_name
  allocation_method       = "Static"
  idle_timeout_in_minutes = 30

  tags = {
    environment = "dev01"
  }
}

resource "azurerm_network_interface" "main" {
  for_each            = var.locations
  name                = "${each.key}-nic"
  location            = "${each.value}"
  resource_group_name = var.azurerm_resource_group_name

  ip_configuration {
    name                          = "testconfiguration1"
    subnet_id                     = azurerm_subnet.internal.id
    private_ip_address_allocation = "Dynamic"
    public_ip_address_id          = azurerm_public_ip.example.id
  }
}

resource "azurerm_virtual_machine" "main" {
  for_each              = var.locations
  name                  = "${each.key}t-vm"
  location              = "${each.value}"
  resource_group_name   = var.azurerm_resource_group_name
  network_interface_ids = [azurerm_network_interface.main[each.key].id]
  vm_size               = "Standard_D2s_v3"
...

Error:

Error: Error creating Network Interface "vm2-nic" (Resource Group "candidate-d7f5a2-rg"): network.InterfacesClient#CreateOrUpdate: Failure sending request: StatusCode=400 -- Original Error: Code="InvalidResourceReference" Message="Resource /subscriptions/xxxxxxx/resourceGroups/xxxx/providers/Microsoft.Network/virtualNetworks/north-network/subnets/internal referenced by resource /subscriptions/xxxxx/resourceGroups/xxxxx/providers/Microsoft.Network/networkInterfaces/vm2-nic was not found. Please make sure that the referenced resource exists, and that both resources are in the same region." Details=[]

  on environment.tf line 47, in resource "azurerm_network_interface" "main":
  47: resource "azurerm_network_interface" "main" {

Upvotes: 1

Views: 1334

Answers (2)

Nancy Xiong
Nancy Xiong

Reputation: 28224

Since you have for_each and in the resource "azurerm_network_interface", it will create two NICs in the location = "${each.value}" while the subnet or VNet has a fixed region "North Europe". You need to create the NICs or other Azure VM related resources like subnets in the same region, you could change the codes like this,

resource "azurerm_resource_group" "test" {
  name     = "myrg"
  location = "West US"
}

variable "locations" {
  type = map(string)
  default = {
    vm1 = "North Europe"
    vm2 = "West Europe"
  }
}


resource "azurerm_virtual_network" "main" {
  for_each            = var.locations
  name                = "${each.key}-network"
  address_space       = ["10.0.0.0/16"]
  location            = "${each.value}"
  resource_group_name = azurerm_resource_group.test.name
}

resource "azurerm_subnet" "internal" {
  for_each             = var.locations
  name                 = "${each.key}-subnet"
  resource_group_name  = azurerm_resource_group.test.name
  virtual_network_name = azurerm_virtual_network.main[each.key].name
  address_prefix       = "10.0.2.0/24"
}

resource "azurerm_public_ip" "example" {
  for_each                = var.locations
  name                    = "${each.key}-pip"
  location                = "${each.value}"
  resource_group_name     = azurerm_resource_group.test.name
  allocation_method       = "Static"
  idle_timeout_in_minutes = 30

}

resource "azurerm_network_interface" "main" {
  for_each            = var.locations
  name                = "${each.key}-nic"
  location            = "${each.value}"
  resource_group_name = azurerm_resource_group.test.name

  ip_configuration {
    name                          = "testconfiguration1"
    subnet_id                     = azurerm_subnet.internal[each.key].id
    private_ip_address_allocation = "Dynamic"
    public_ip_address_id          = azurerm_public_ip.example[each.key].id
  }
}

Upvotes: 0

daodennis
daodennis

Reputation: 33

According to the documentation each NIC attached to a VM must exist in the same location (Region) and subscription as the VM. https://learn.microsoft.com/en-us/azure/virtual-machines/windows/network-overview.

If you can re-create the NIC in the same location as the VM or create the VM in the same location as the NIC that will likely solve your problem.

Upvotes: 0

Related Questions