0
votes

For a project I use remote modules (git modules) these are called and executed in a terraformMain.tf file.

For example, I use an Azure Resource Group Module, this module is looped in the terraformMain.tf by "count = length (var.resourcegroups)". The problem I have now is that I want to use one of the two created Resource groups in the next module (creating VNET) but I keep encountering the following error:

Error: Unsupported attribute

on outputs.tf line 2, in output "RG": 2: value = [module.resourceGroups.resource_group_name]

This value does not have any attributes.

Unsupported attribute

on terraformMain.tf line 33, in module "vnet": 33: resourcegroup_name = module.resourceGroups.resource_group_name[0]

This value does not have any attributes.

The Azure Resource Group module code looks like this :

main.tf

resource "azurerm_resource_group" "RG" {
  name  = var.resource_group_name
  location = var.location
}

variables.tf

variable "location" {
    type = string
}

variable "resource_group_name" {
    type = string
}

outputs.tf

output "resource_group_names" {
   value       = concat(azurerm_resource_group.RG.*.name, [""])[0]
}

The code of the terraformMain.tf looks like this:

terraformMain.tf

terraform {
  required_version = ">= 0.13"

  required_providers {
    azurerm = {
      source = "hashicorp/azurerm"
      version = "2.45.1"
    }
  }

  backend "azurerm" {
    resource_group_name   = "__terraformresourcegroup__"
    storage_account_name  = "__terraformstorageaccount__"
    container_name        = "__terraformcontainer__"
    key                   = "__terraformkey__"
  }

}

provider "azurerm" {
  features {}
}

module "resourceGroups" {
  count     = length(var.resourcegroups)
  source    = "git::https://*****@dev.azure.com/****/TerraformAzureModules/_git/ResourceGroup"
  location  = var.location
  resource_group_name = var.resourcegroups[count.index]
}

module "vnet" {
  source = "git::https://*****@dev.azure.com/****/TerraformAzureModules/_git/VirtualNetwork"
  resourcegroup_name = module.resourceGroups.resource_group_name[0]
  location = var.location  
  vnet_name = var.vnet_name

  count     = length(var.subnet_names)
  vnet_cidr = var.vnet_cidr[count.index]  
  subnet_cidr = var.subnet_cidr[count.index]
  subnet_name = var.subnet_names[count.index]
}

variables.tf

variable "location" {
    default = "westeurope" 
}

variable "resourcegroups" {
    default = ["rg1", "rg2"]
}

#Azure Vnet / Subnet
variable "vnet_name" {
  default = "vnet_1"
}
variable "subnet_names" {
  default = ["subnet1", "subnet2"]
}
variable "vnet_cidr" {
  default = ["10.116.15.0/24"]
}
variable "subnet_cidr" {
  default     = ["10.116.15.0/26", "10.116.15.128/27"]
}

outputs.tf

output "RG" {
  value       = [module.resourceGroups.resource_group_name]
}

any help is appreciated!

3
Could you please share code of your module resourceGroups?Andriy Bilous
The code is already in the post, under " The Azure Resource Group module code looks like this"Theironrock95

3 Answers

2
votes

Your resourceGroups module has count = length(var.resourcegroups) set, and so module.resourceGroups is a list of objects and therefore you will need to request a specific element from the list before accessing an attribute:

module.resourceGroups[0].resource_group_name

Or, if your goal was to return a list of all of the resource group names, you can use the [*] operator to concisely access the resource_group_name argument from each of the elements and return the result as a list:

resource.resourceGroups[*].resource_group_name
1
votes

The variables in the module need to have a type or a default.

For example, this would be a valid file:

variable "location" {
    type = string
}

variable "resource_group_name" {
    type = string
}
0
votes

The solution we have applied is to move the count from the terraformMain.tf to the resource module main.tf. this allowed us to pass the resoucgroups to the terraformMain through the output.tf of the module.

ResourceGroup module:

main.tf

resource "azurerm_resource_group" "RG" {
  count     = length(var.resource_group_name)
  name  = var.resource_group_name[count.index]
  location = var.location
}

outputs.tf

output "resource_group_names" {
   value       = azurerm_resource_group.RG.*.name
}

terraformMain.tf code:

    terraform {
      required_version = ">= 0.13"
    
      required_providers {
        azurerm = {
          source = "hashicorp/azurerm"
          version = "2.45.1"
        }
      }
    
      backend "azurerm" {
        resource_group_name   = "__terraformresourcegroup__"
        storage_account_name  = "__terraformstorageaccount__"
        container_name        = "__terraformcontainer__"
        key                   = "__terraformkey__"
      }
    
    }
    
    provider "azurerm" {
      features {}
    }
    
    module "resourceGroups" {
      source    = "git::https://*****@dev.azure.com/*****/TerraformAzureModules/_git/ResourceGroup"
      location  = var.location
      resource_group_name = var.resourcegroups
    }

module "vnet" {
  source = "git::https://******@dev.azure.com/*****/TerraformAzureModules/_git/VirtualNetwork"
  resourcegroup_name = module.resourceGroups.resource_group_names[0]
  location = var.location  
  vnet_name = var.vnet_name
  vnet_cidr = var.vnet_cidr 
  subnet_cidr = var.subnet_cidr
  subnet_name = var.subnet_names
}

I want to thank you for your contribution