Manish
Manish

Reputation: 2327

Vnet peering not getting deleted in azure

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
}

Upvotes: 0

Views: 1180

Answers (1)

RahulKumarShaw
RahulKumarShaw

Reputation: 4602

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

Upvotes: 1

Related Questions