Terraform Vm Provision随附2个NIC

时间:2019-10-12 13:03:31

标签: terraform

我正在使用terraform设置新的Azure VM,并将2 nic附加到该VM。 我遇到错误了。

azurerm_virtual_machine.vm: compute.VirtualMachinesClient#CreateOrUpdate: Failure sending request: StatusCode=400 -- Original Error: Code="VirtualMachineMustHaveOneNetworkInterfaceAsPrimary" Message="Virtual machine AZLXSPTOPTFWTEST must have one network interface set as the primary." Details=[]

我已经引用https://www.terraform.io/docs/providers/azurerm/r/network_interface.html这个URL来创建NIC

这是我的Terraform代码。

resource "azurerm_resource_group" "main" {
  name     = "RG-EASTUS-FW-TEST"
  location = "eastus"
}

#create a virtual Network
resource "azurerm_virtual_network" "privatenetwork" {
    name = "VNET-EASTUS-FWTEST"
    address_space = ["10.100.0.0/16"]
    location = "${azurerm_resource_group.main.location}"
    resource_group_name = "${azurerm_resource_group.main.name}"

}
#create a subnet with externel virtual network

resource "azurerm_subnet" "external"{
    name = "SNET-FWTEST-OUT"
    virtual_network_name = "${azurerm_virtual_network.privatenetwork.name}"
    resource_group_name = "${azurerm_resource_group.main.name}"
    address_prefix = "10.100.10.0/24"

}

#Create a public IP address

resource "azurerm_public_ip" "public" {
    name = "PFTEST-PUBLIC"
    location = "${azurerm_resource_group.main.location}"
    resource_group_name = "${azurerm_resource_group.main.name}"
    allocation_method  = "Static"

}


# Create a Subnet within the Virtual Network
resource "azurerm_subnet" "internal" {
  name                 = "SNET-FWTEST-IN"
  virtual_network_name = "${azurerm_virtual_network.privatenetwork.name}"
  resource_group_name  = "${azurerm_resource_group.main.name}"
  address_prefix       = "10.100.11.0/24"

}

resource "azurerm_network_interface" "OUT" {
    name = "NIC-FWTEST-OUT"
    location = "${azurerm_resource_group.main.location}"
    resource_group_name = "${azurerm_resource_group.main.name}"
  #  network_security_group_id = "${azurerm_network_interface.main.id}"
    primary = "true"
    ip_configuration {

        name = "OUT"
        subnet_id = "${azurerm_subnet.external.id}"
        private_ip_address_allocation = "static"
        private_ip_address = "10.100.10.5"
        public_ip_address_id = "${azurerm_public_ip.public.id}"

    }


}


# Create a network interface for VMs and attach the PIP and the NSG
resource "azurerm_network_interface" "main" {
  name                      = "NIC-FWTEST-IN"
  location                  = "${azurerm_resource_group.main.location}"
  resource_group_name       = "${azurerm_resource_group.main.name}"
 # network_security_group_id = "${azurerm_network_security_group.main.id}"

  ip_configuration {
    name = "IN"
    subnet_id                     = "${azurerm_subnet.internal.id}"
    private_ip_address_allocation = "static"
    private_ip_address            = "10.100.11.5"
  }
}

# Create a new Virtual Machine based on the Golden Image
resource "azurerm_virtual_machine" "vm" {
  name                             = "AZLXSPTOPTFWTEST"
  location                         = "${azurerm_resource_group.main.location}"
  resource_group_name              = "${azurerm_resource_group.main.name}"
  network_interface_ids            = ["${azurerm_network_interface.OUT.id}","${azurerm_network_interface.main.id}"]
  vm_size                          = "Standard_DS12_v2"
  delete_os_disk_on_termination    = true
  delete_data_disks_on_termination = true

我期望提供2 nic的天蓝色虚拟机。

预先感谢

1 个答案:

答案 0 :(得分:0)

我得到了解决方案,因为我们需要指定主网络接口ID并将其添加到网络接口ID。在创建网络接口时,我们还需要添加ipconfiguration。 请参考下面的代码。

ip_configuration {

        name = "OUT"
        subnet_id = "${azurerm_subnet.external.id}"
        primary  = true
        private_ip_address_allocation = "static"
        private_ip_address = "10.100.10.5"
        public_ip_address_id = "${azurerm_public_ip.public.id}"

    }

resource "azurerm_virtual_machine" "vm" {
  name                             = "AZLXSPTOPTFWTEST"
  location                         = "${azurerm_resource_group.main.location}"
  resource_group_name              = "${azurerm_resource_group.main.name}"
  network_interface_ids            = ["${azurerm_network_interface.main.id}","${azurerm_network_interface.OUT.id}"]
  primary_network_interface_id     = "${azurerm_network_interface.OUT.id}"
  vm_size                          = "Standard_DS12_v2"
  delete_os_disk_on_termination    = true
  delete_data_disks_on_termination = true