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
}