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!