1
votes

I am working on provisioning the new azure VM using terraform and attached 2 nic to that VM. I am getting below error.

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=[]

I have referred https://www.terraform.io/docs/providers/azurerm/r/network_interface.html this URL for creating NIC

This is my Terraform code.

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

I am expecting result as provision azure VM with 2 nic.

Thanks In Advance

1

1 Answers

1
votes

I got the solution as we need to specify the primary network interface id and add this to the network interface id. As well as we need to add in ipconfiguration while creating network interface. Please refer below code.

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