在Azure中使用不同的配置创建多个VM

时间:2020-05-20 09:45:15

标签: azure azure-devops terraform terraform-provider-azure

我想在Azure中创建具有Terraform的两个虚拟机。我已经配置了两个“ azurerm_network_interface”,但是当我尝试应用更改时,出现错误。你有什么主意吗?如果我尝试在不同的区域创建它们,会有任何问题吗?

错误类似于:找不到vm2-nic 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 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" {

2 个答案:

答案 0 :(得分:0)

根据文档,与虚拟机相连的每个NIC必须与虚拟机位于相同的位置(区域)和预订。 https://docs.microsoft.com/en-us/azure/virtual-machines/windows/network-overview

如果可以在与VM相同的位置重新创建NIC或在与NIC相同的位置创建VM,这很可能会解决您的问题。

答案 1 :(得分:0)

由于您拥有for_each并且在resource "azurerm_network_interface"中,因此它将在location = "${each.value}"中创建两个NIC,而子网或VNet具有固定的区域“北欧”。您需要在同一区域中创建NIC或其他与Azure VM相关的资源(如子网),可以更改类似的代码,

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
  }
}