-1

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.

Vidya
  • 179
  • 1
  • 2
  • 12

1 Answers1

2

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
}
Marcin
  • 215,873
  • 14
  • 235
  • 294