Terraform count calling module output(return) error

I need to return output of name and id only

root main.tf

# Azure Provider configuration
provider "azurerm" {
  features {}
}

module "rg" {
  source              = "../../modules/rg"
  count               = length(var.resource_groups)
  resource_group_name = var.resource_groups[count.index].name
  tags                = {}
  location            = westus2
  lookup              = false
}

module/rg/main.tf

resource "azurerm_resource_group" "rg" {
    count    = var.lookup == true ? 0 : 1
    name = var.resource_group_name
    location = var.location
    tags = var.tags
}

output "rg_id" {
  description = "The id of the newly created rg"
  value       = azurerm_resource_group.rg[count_index].id # azurerm_resource_group.rg, this statement will work
}

output "rg_name" {
  description = "The name of the newly created rg"
  value       = azurerm_resource_group.rg[count_index].name #  azurerm_resource_group.rg, this statement will work
}

I am getting below error:

A reference to a resource type must be followed by at least one attribute access, specifying the resource name.

1 answer

  • answered 2021-07-31 05:38 Marcin

    I think the following should address your issue:

    output "rg_id" {
      description = "The id of the newly created rg"
      value       = var.lookup == true ? azurerm_resource_group.rg[0].id : null
    }
    
    output "rg_name" {
      description = "The name of the newly created rg"
      value       = var.lookup == true ? azurerm_resource_group.rg[0].name : null
    }
    

How many English words
do you know?
Test your English vocabulary size, and measure
how many words do you know
Online Test
Powered by Examplum