Home > Net >  Terraform Vnet Peering with vlans in different ressource groups
Terraform Vnet Peering with vlans in different ressource groups

Time:09-30

Im quite new to Terraform so maybe i make a very basic mistake but after multiple hours maybe here someone can help me out.

So i tried to peer to vlans together. I viewed multiple tutorials about it and the only difference between my configuration i can see is that i want to make a peering between 2 vlans that are in 2 different resource groups. I also noticed that if i put the peering in one of the 2 vlan ressource groups i have fever errors.

error

#Creating Resource Groups
resource "azurerm_resource_group" "network" {
  name     = "network"
  location = "West Europe"
}

resource "azurerm_resource_group" "front" {
  name     = "front"
  location = "West Europe"
}

resource "azurerm_resource_group" "middle" {
  name     = "middle"
  location = "West Europe"
}

resource "azurerm_resource_group" "back" {
  name     = "back"
  location = "West Europe"
}

resource "azurerm_resource_group" "peerings" {
  name     = "peerings"
  location = "West Europe"
}


#Creating Virtual Networks
resource "azurerm_virtual_network" "network" {
  name                = "network"
  location            = azurerm_resource_group.network.location
  resource_group_name = azurerm_resource_group.network.name
  address_space       = ["10.1.0.0/16"]
  
  subnet {
    name           = "default"
    address_prefix = "10.1.0.0/24"
  }

  subnet {
    name           = "gatewaysubnet"
    address_prefix = "10.1.1.0/24"
  }

  subnet {
    name           = "azurefirewallsubnet"
    address_prefix = "10.1.3.0/24"
  }

  subnet {
    name           = "azurebastionsubnet"
    address_prefix = "10.1.2.0/24"
  }
}

resource "azurerm_virtual_network" "front" {
  name                = "network"
  location            = azurerm_resource_group.front.location
  resource_group_name = azurerm_resource_group.front.name
  address_space       = ["10.2.0.0/16"]
}

resource "azurerm_virtual_network" "middle" {
  name                = "network"
  location            = azurerm_resource_group.middle.location
  resource_group_name = azurerm_resource_group.middle.name
  address_space       = ["10.3.0.0/16"]
}

resource "azurerm_virtual_network" "back" {
  name                = "network"
  location            = azurerm_resource_group.back.location
  resource_group_name = azurerm_resource_group.back.name
  address_space       = ["10.4.0.0/16"]
}

#Create peerings
#network <--> front
resource "azurerm_virtual_network_peering" "networktofront" {
  name                      = "networktofront"
  resource_group_name       = azurerm_resource_group.peerings.name
  virtual_network_name      = azurerm_virtual_network.network.name
  remote_virtual_network_id = azurerm_virtual_network.front.id
}

resource "azurerm_virtual_network_peering" "fronttonetwork" {
  name                      = "fronttonetwork"
  resource_group_name       = azurerm_resource_group.peerings.name
  virtual_network_name      = azurerm_virtual_network.front.name
  remote_virtual_network_id = azurerm_virtual_network.network.id
}

#network <--> middle
resource "azurerm_virtual_network_peering" "networktomiddle" {
  name                      = "networktomiddle"
  resource_group_name       = azurerm_resource_group.peerings.name
  virtual_network_name      = azurerm_virtual_network.network.name
  remote_virtual_network_id = azurerm_virtual_network.middle.id
}

resource "azurerm_virtual_network_peering" "middletonetwork" {
  name                      = "middletonetwork"
  resource_group_name       = azurerm_resource_group.peerings.name
  virtual_network_name      = azurerm_virtual_network.middle.name
  remote_virtual_network_id = azurerm_virtual_network.network.id
}

#network <--> back
resource "azurerm_virtual_network_peering" "networktoback" {
  name                      = "networktoback"
  resource_group_name       = azurerm_resource_group.peerings.name
  virtual_network_name      = azurerm_virtual_network.network.name
  remote_virtual_network_id = azurerm_virtual_network.back.id
}

resource "azurerm_virtual_network_peering" "backtonetwork" {
  name                      = "backtonetwork"
  resource_group_name       = azurerm_resource_group.peerings.name
  virtual_network_name      = azurerm_virtual_network.back.name
  remote_virtual_network_id = azurerm_virtual_network.network.id
}

CodePudding user response:

Virtual Network Peerings are a subset of the Virtual Network Resource (Microsoft.Network/virtualNetworks/network/virtualNetworkPeerings) and it is therefore not possible to carve these out into different resource groups.

Besides that, your code is accurate and should work as soon as you create the peerings in the corresponding virtual network resource groups:

#Creating Resource Groups
resource "azurerm_resource_group" "network" {
  name     = "network"
  location = "West Europe"
}

resource "azurerm_resource_group" "front" {
  name     = "front"
  location = "West Europe"
}

resource "azurerm_resource_group" "middle" {
  name     = "middle"
  location = "West Europe"
}

resource "azurerm_resource_group" "back" {
  name     = "back"
  location = "West Europe"
}


#Creating Virtual Networks
resource "azurerm_virtual_network" "network" {
  name                = "network"
  location            = azurerm_resource_group.network.location
  resource_group_name = azurerm_resource_group.network.name
  address_space       = ["10.1.0.0/16"]
  
  subnet {
    name           = "default"
    address_prefix = "10.1.0.0/24"
  }

  subnet {
    name           = "gatewaysubnet"
    address_prefix = "10.1.1.0/24"
  }

  subnet {
    name           = "azurefirewallsubnet"
    address_prefix = "10.1.3.0/24"
  }

  subnet {
    name           = "azurebastionsubnet"
    address_prefix = "10.1.2.0/24"
  }
}

resource "azurerm_virtual_network" "front" {
  name                = "network"
  location            = azurerm_resource_group.front.location
  resource_group_name = azurerm_resource_group.front.name
  address_space       = ["10.2.0.0/16"]
}

resource "azurerm_virtual_network" "middle" {
  name                = "network"
  location            = azurerm_resource_group.middle.location
  resource_group_name = azurerm_resource_group.middle.name
  address_space       = ["10.3.0.0/16"]
}

resource "azurerm_virtual_network" "back" {
  name                = "network"
  location            = azurerm_resource_group.back.location
  resource_group_name = azurerm_resource_group.back.name
  address_space       = ["10.4.0.0/16"]
}

#Create peerings
#network <--> front
resource "azurerm_virtual_network_peering" "networktofront" {
  name                      = "networktofront"
  resource_group_name       = azurerm_resource_group.network.name
  virtual_network_name      = azurerm_virtual_network.network.name
  remote_virtual_network_id = azurerm_virtual_network.front.id
}

resource "azurerm_virtual_network_peering" "fronttonetwork" {
  name                      = "fronttonetwork"
  resource_group_name       = azurerm_resource_group.front.name
  virtual_network_name      = azurerm_virtual_network.front.name
  remote_virtual_network_id = azurerm_virtual_network.network.id
}

#network <--> middle
resource "azurerm_virtual_network_peering" "networktomiddle" {
  name                      = "networktomiddle"
  resource_group_name       = azurerm_resource_group.network.name
  virtual_network_name      = azurerm_virtual_network.network.name
  remote_virtual_network_id = azurerm_virtual_network.middle.id
}

resource "azurerm_virtual_network_peering" "middletonetwork" {
  name                      = "middletonetwork"
  resource_group_name       = azurerm_resource_group.middle.name
  virtual_network_name      = azurerm_virtual_network.middle.name
  remote_virtual_network_id = azurerm_virtual_network.network.id
}

#network <--> back
resource "azurerm_virtual_network_peering" "networktoback" {
  name                      = "networktoback"
  resource_group_name       = azurerm_resource_group.network.name
  virtual_network_name      = azurerm_virtual_network.network.name
  remote_virtual_network_id = azurerm_virtual_network.back.id
}

resource "azurerm_virtual_network_peering" "backtonetwork" {
  name                      = "backtonetwork"
  resource_group_name       = azurerm_resource_group.back.name
  virtual_network_name      = azurerm_virtual_network.back.name
  remote_virtual_network_id = azurerm_virtual_network.network.id
}
  • Related