Search code examples
terraformterraform-provider-azure

Terraform : calling output.tf of child module to another child module


I am trying to use output of one child module to reference in another module for creating another resource. In this scenario, I am createing resource group in one child module and using output of this module to create storage account. Getting below error

Error Error

File Structure

 azurerm_resources
       main.tf
       provider.tf
       variable.tf

 Modules
    azurerm_resource_group
       main.tf
       variable.tf
       output.tf

    SA
       main.tf
       variable.tf

Root main.tf

module "rg" {
  source        = "../../module/modules/azurerm_resource_groups"
  env           = var.env
  tags          = var.tags
  infra_rg_name = var.infra_rg_name
  rg_location   = var.rg_location
}

module "SA" {
  source             = "../../module/modules/SA"
  storageaccountname = var.storageaccountname
  infra_rg_name      = var.infra_rg_name
  env                = var.env
}

Root Variable.tf

variable "infra_rg_name" {
  type = string
}

variable "rg_location" {
  type = string
  #default = "canadacentral"
}

variable "env" {}
variable "tags" {}
variable "storageaccountname" {}

Child Module 1 - azurerm_resource_groups

Main.tf

resource "azurerm_resource_group" "RG01Name" {
  name     = "${var.env}-${var.infra_rg_name}"
  location = var.rg_location
  tags     = var.tags
}

variable.tf

variable "infra_rg_name" {
  type = string
}

variable "rg_location" {
  type = string
  #default = "canadacentral"
}

variable "env" {}
variable "tags" {}

output.tf

output  azurerm_resource_group {value= azurerm_resource_group.RG01Name.name}

Child Module 2 - SA

main.tf

resource "azurerm_storage_account" "sa01" {
  name                     = "${var.env}-${var.storageaccountname}"
  resource_group_name      = module.azurerm_resource_group.RG01Name.name
  location                 = module.azurerm_resource_group.RG01Name.location
  account_tier             = "Standard"
  account_replication_type = "GRS"
  tags                     = var.tags
}

resource "azurerm_storage_container" "container01" {
  name                  = "container01"
  storage_account_name  = azurerm_storage_account.sa01.name
  container_access_type = "private"
}

variable.tf

variable "storageaccountname" {}
variable "env" {}
variable "infra_rg_name" {
  type = string
}

Error

PS C:\Users\xx\OneDrive\Desktop\ADO-Test\Test-Repo\terraform\module\azurerm_resources> terraform validate
╷
│ Error: Reference to undeclared module
│
│   on ..\..\module\modules\SA\main.tf line 8, in resource "azurerm_storage_account" "sa01":
│    8:   resource_group_name      = module.azurerm_resource_group.RG01Name.name
│
│ No module call named "azurerm_resource_group" is declared in module.SA.
╵
╷
│ Error: Reference to undeclared module
│
│   on ..\..\module\modules\SA\main.tf line 9, in resource "azurerm_storage_account" "sa01":
│    9:   location                 = module.azurerm_resource_group.RG01Name.location
│
│ No module call named "azurerm_resource_group" is declared in module.SA.

Error Error


Solution

  • This is not how you pass outputs of one child module to another. Your SA module doesn't have access to your azurerm_resource_groups module. In order to pass an output of the azurerm_resource_groups module to the SA module you have to first declare an input variable in the SA module like so:

    variable "resource_group" {
      type = object({
        name = string
        location = string
      })
    }
    

    Then update the azurerm_resource_groups module's outputs to include both name and location. Then in the root module reference the azurerm_resource_groups outputs in the newly declared SA variable. Finally, use that variable in the SA module.

    Accessing Child Module Outputs