【问题标题】:Create multiple VM's in Azure with different configuration在 Azure 中使用不同的配置创建多个 VM
【发布时间】:2020-09-06 15:12:07
【问题描述】:

我想在 Azure 中使用 terraform 创建两个 vm。我已经配置了两个“azurerm_network_interface”,但是当我尝试应用更改时,我收到一个错误。你有什么主意吗?如果我尝试在不同的区域创建它们会有什么问题吗?

错误类似于: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 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" {

【问题讨论】:

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


    【解决方案1】:

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

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

    【讨论】:

    • 谢谢你,道丹尼斯。 NIC 有不同的位置,但每个 nic 与对应的 VM 具有相同的位置。不正常吗?
    【解决方案2】:

    由于您有for_eachresource "azurerm_network_interface",它将在location = "${each.value}" 中创建两个网卡,而子网或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
      }
    }
    

    【讨论】:

    • network_interface_ids = azurerm_network_interface.main[each-key].id,现在我在这里收到另一个错误:错误:在资源“azurerm_virtual_machine”“main”中的 environment.tf 第 70 行的引用无效:70: network_interface_ids = azurerm_network_interface.main[each-key].id 对资源类型的引用必须后跟至少一个属性访问,指定资源名称。
    • 这是另一个新问题。请接受这一点并发布一个新主题
    猜你喜欢
    • 1970-01-01
    • 2019-08-14
    • 1970-01-01
    • 1970-01-01
    • 2020-07-04
    • 2022-01-26
    • 2021-11-14
    • 1970-01-01
    • 2017-03-25
    相关资源
    最近更新 更多