Search code examples
terraforminterrupt-handlingterraform-provider-azureterraform-template-fileterraform0.12+

how to call the resource group in virtual network the resource group parameter comes to CSV file


I am trying to access the resource group name in virtual network in same code, have define some parameters using CSV file but when I am calling that parameter in code its showing error,

please check below code

provider "azurerm" {
    features {}
}

locals {
 group_names = csvdecode(file("./test.csv"))
}

//Create a resource group and nsg

resource "azurerm_resource_group" "Customer" {
  count = length(local.group_names)
  name  = local.group_names[count.index].resource_group_name
  location = local.group_names[count.index].region
} 


//Create a Vnet
resource "azurerm_virtual_network" "Customer" {
  count               = length(local.group_names)
  name                = local.group_names[count.index].virtual_network_name
  location            = azurerm_resource_group.Customer.location 
  resource_group_name = azurerm_resource_group.Customer.name
  address_space       = [local.group_names[count.index].address_space] 
}

//create Subnet
resource "azurerm_subnet" "Customer" {
  count                = length(local.group_names)
  name                 = local.group_names[count.index].subnet_name
  resource_group_name  = azurerm_resource_group.Customer.name                                           
  virtual_network_name = azurerm_virtual_network.Customer.name                         
  address_prefix       = local.group_names[count.index].address_prefix_subnet   
}

following error is occured


Solution

  • As the error show, you could change the code like this

    resource "azurerm_virtual_network" "Customer" {
      count               = length(local.group_names)
      name                = local.group_names[count.index].virtual_network_name
      location            = azurerm_resource_group.Customer[count.index].location # add [count.index]
      resource_group_name = azurerm_resource_group.Customer[count.index].name  # add [count.index]
      address_space       = [local.group_names[count.index].address_space] 
    }