Vijay Gupta
Vijay Gupta

Reputation: 47

Unable to peer Vnet in Azure using Terraform

i'hv list of Vnet in azure and through count trying to peer from index[0] to rest all of vNet in list. Not sure how to put logic for rest vNet that count must start from index[1] instead [0]. While mentioning index.count , it is trying to peer with same vNet at last and throwing error.

Here is my code.

Variable.tf
===========
variable "rg" {
 type= list(string)
 description = " Name of Resource Group"
 default = ["hub", "ansible", "spoke1", "spoke2", "spoke3", "spoke4", "spoke5"]
  }
#------------------------------:Vnet's:--------------------------------------------

variable "vnet_name" {
    description = "Vnet Details "
    type = list(string)
    default =  ["hub_vnet", "ansible_vnet", "spoke1_vnet", "spoke2_vnet", "spoke3_vnet", "spoke4_vnet", "spoke5_vnet"]
}

Main.tf
========
resource "azurerm_virtual_network_peering" "az_to_rest" {
  name                      = element(var.vnet_name, count.index)
  resource_group_name       = azurerm_resource_group.az_rg[0].name
  virtual_network_name      = azurerm_virtual_network.az_vnet[0].name
 remote_virtual_network_id = azurerm_virtual_network.az_vnet[count.index].id
  count                     = length(var.vnet_name)
}

Upvotes: 0

Views: 354

Answers (1)

Ansuman Bal
Ansuman Bal

Reputation: 11401

I tested it for 3 vnets in 3 resource groups using the below code:

variable "rg" {
 type= list(string)
 description = " Name of Resource Group"
 default = ["testgroup","hubtest","ansibletest"]
  }
#------------------------------:Vnet's:--------------------------------------------

variable "vnet_name" {
    description = "Vnet Details "
    type = list(string)
    default =  ["ansuman_vnet","hub_vnet","ansible_vnet"]
}

provider "azurerm" {
  features {}
}

data "azurerm_resource_group" "test" {
  count = length(var.rg)
  name = element(var.rg,count.index)
}

data "azurerm_virtual_network" "vnet" {
  count               = length(var.rg)
  name                = element(var.vnet_name, count.index)
  resource_group_name = element(data.azurerm_resource_group.test.*.name, count.index)
}


resource "azurerm_virtual_network_peering" "az_to_rest" {
  name                      = element(var.vnet_name, count.index)
  resource_group_name       = "${data.azurerm_resource_group.test.0.name}"
  virtual_network_name      = "${data.azurerm_virtual_network.vnet.0.name}"
 remote_virtual_network_id = data.azurerm_virtual_network.vnet[count.index].id
  count                     = length(var.vnet_name)
}

output:

enter image description here

enter image description here

As you can see from the above image, it errors out as its trying to peer with itself as well.

So , as a solution I have hard coded the virtual network name and resource group that I want to peer with other vnets and removed it from the list like below:

variable "rg" {
 type= list(string)
 description = " Name of Resource Group"
 default = ["hubtest","ansibletest"]
  }
#------------------------------:Vnet's:--------------------------------------------

variable "vnet_name" {
    description = "Vnet Details "
    type = list(string)
    default =  ["hub_vnet","ansible_vnet"]
}

provider "azurerm" {
  features {}
}

data "azurerm_resource_group" "test" {
  count = length(var.rg)
  name = element(var.rg,count.index)
}

data "azurerm_virtual_network" "vnet" {
  count               = length(var.rg)
  name                = element(var.vnet_name, count.index)
  resource_group_name = element(data.azurerm_resource_group.test.*.name, count.index)
}


resource "azurerm_virtual_network_peering" "az_to_rest" {
  name                      = element(var.vnet_name, count.index)
  resource_group_name       = "testgroup" # resource group of vnet1
  virtual_network_name      = "ansuman_vnet"#vnet1
 remote_virtual_network_id = data.azurerm_virtual_network.vnet[count.index].id
  count                     = length(var.vnet_name)
}

Output:

enter image description here

enter image description here

Upvotes: 1

Related Questions