0

I am trying to create/delete vnet peering connection in azure using terraform. To provide some context, there are two vnets- A and B, under two different subscription but within same AD and I have access to both. Vnet A is static and vnetB is created on-demand.

I am able to create the peering by initiating from vnet B. But when I delete the peering from Vnet B and delete the entire resource group of vNet B and recreate the resource group and peering, it says-

Error: Cannot create or update peering vnetB. Virtual networks -vnetB and vnetA cannot be peered because address space of the first virtual network overlaps with address space of vnet already peered with the second virtual network. Overlapping address prefixes: 10.2.65.0/25.

I can see peering is not deleted from vnet A. Is there a way to delete this peering ?

Terraform file:

# It is assumed that A already has a resource group and vnet created
# Access the static A account 
provider "azurerm" {
  alias           = "A"
  subscription_id = "XXXX-XXXX-XXXX"
  features {}
  skip_provider_registration = true
}

data "azurerm_resources" "vnet" {
    resource_group_name = "A-ResourceGroup"
    type = "Microsoft.Network/virtualNetworks"
    provider = azurerm.Aprov
}
resource "azurerm_virtual_network_peering" "A-B" {
  provider = azurerm.Aprov
  name                      = "A-B"
  resource_group_name       = data.azurerm_resources.vnet.resource_group_name
  virtual_network_name      = data.azurerm_resources.vnet.resources[0].name
  remote_virtual_network_id = azurerm_virtual_network.B-vnet.id
}

# Deployment in B Account
provider "azurerm" {
  skip_provider_registration = true
  features {}
}

resource "azurerm_resource_group" "B" {
  name     = "B-peer-1"
  location = "West US"
}

resource "azurerm_virtual_network" "B-vnet" {
  name                = "B-network1"
  resource_group_name = azurerm_resource_group.B.name
  address_space       = ["10.0.1.0/24"]
  location            = "West US"
}

# Add the VNET peering to A account
resource "azurerm_virtual_network_peering" "B-A" {
  name                      = "B-A"
  resource_group_name       = azurerm_resource_group.B.name
  virtual_network_name      = azurerm_virtual_network.B-vnet.name
  remote_virtual_network_id = data.azurerm_resources.vnet.resources[0].id
}

Manish
  • 1,999
  • 2
  • 15
  • 26
  • Are both Vnets in the same region? Trying to understand if this is Global Vnet peering or not. – Ken W - Zero Networks Feb 22 '22 at 13:21
  • Also if you could provide the terraform script it would help to repo. I would like to try this in my environment. – Ken W - Zero Networks Feb 22 '22 at 13:27
  • 1
    Sorry for the late reply. The Vnets are in different regions. The issue was exactly as @RahulKumarShaw-MT mentioned i.e deletion of peering from one side leaves it dangling at the other end. Need to remove peering from both Vnets. Will add the terraform file as well. The problem has been solved. Thanks Ken and Rahul – Manish Mar 02 '22 at 05:42

1 Answers1

1

For reproducing your issue create a VNETA and VNET B in the same region and peer them as well

VNETA to VNETB Peering enter image description here

VNETB to VNETA Peering

enter image description here

Now i deleted the VNETB, But still it will be peer with VNETA untill unless we don't delete or remove peering.

But, you are creating the same VNETB or with different name but with the same address space it will say you can't peer because you already in peer and will throw the error like you are getting.

enter image description here

You can delete the Existing Peering from portal itself.

enter image description here

You can also use terraform command to destroy the existing peering terraform destroy -target nameofpeering

RahulKumarShaw
  • 4,192
  • 2
  • 5
  • 11