Calling the modules - cant see the varaibles?

Hi all. I need a help with using the modules. I have the following structure:

main.tf
providers.tf
terraform.tfvars
---- modules
---------cosmosdb
--------------main.tf
--------------variables.tf
--------------providers.tf
---------sqlserver
--------------main.tf
--------------variables.tf
--------------providers.tf

Problem is when i call the terraform plan on main.tf in root that is calling the modules, it returns the error that he doesnt see any variables… What might be a problem? Thank you.

This is the main.tf for cosmosdb

resource "azurerm_resource_group" "cosmosdb_rg" {
  name     = var.cosmosdb_resource_group_name
  location = var.cosmosdb_resource_group_location
}

resource "azurerm_cosmosdb_account" "cosmosdb" {
  name                = var.cosmosdb_account_name
  location            = azurerm_resource_group.cosmosdb_rg.location
  resource_group_name = azurerm_resource_group.cosmosdb_rg.name
  offer_type          = var.cosmosdb_offer_type
  kind                = var.cosmosdb_kind

  consistency_policy {
    consistency_level       = var.cosmosdb_consistency_level
    max_interval_in_seconds = var.cosmosdb_consistency_max_interval
    max_staleness_prefix    = var.cosmosdb_consistency_max_staleness
  }

  geo_location {
    location          = var.cosmosdb_geo_location[0].location
    failover_priority = var.cosmosdb_geo_location[0].failover_priority
  }

  tags = var.tags
}

  resource "azurerm_cosmosdb_sql_database" "cosmosdb_database" {
  name                = var.cosmosdb_database_name
  resource_group_name = azurerm_resource_group.cosmosdb_rg.name
  account_name        = azurerm_cosmosdb_account.cosmosdb.name
  throughput = var.cosmosdb_database_throughput
}

Variables.tf

variable "cosmosdb_resource_group_name" {
  description = "Name of the resource group in which to create the Cosmos DB account."
  type        = string
}

variable "cosmosdb_resource_group_location" {
  description = "Location for the resource group."
  type        = string
}

variable "cosmosdb_account_name" {
  description = "Name of the Cosmos DB account."
  type        = string
}

variable "cosmosdb_offer_type" {
  description = "Offer type for the Cosmos DB account."
  type        = string
}

variable "cosmosdb_kind" {
  description = "Kind for the Cosmos DB account."
  type        = string
}

variable "cosmosdb_consistency_level" {
  description = "Consistency level for the Cosmos DB account."
  type        = string
}

variable "cosmosdb_consistency_max_interval" {
  description = "Max interval in seconds for consistency."
  type        = number
}

variable "cosmosdb_consistency_max_staleness" {
  description = "Max staleness prefix for consistency."
  type        = number
}

variable "cosmosdb_geo_location" {
  description = "Geo location for the Cosmos DB account."
  type        = list(object({
    location          = string
    failover_priority = number
  }))
}

variable "cosmosdb_database_name" {
  description = "Name of the Cosmos DB database."
  type        = string
}

variable "cosmosdb_database_throughput" {
  description = "Throughput for the Cosmos DB database."
  type        = number
}

variable "tags" {
  description = "Tags to be assigned to the Cosmos DB account."
  type        = map(string)
}

Main.tf that is calling the modules:

module "cosmosdb" {
  source   =  "./modules/azure-cosmosdb"

  cosmosdb_resource_group_name        = var.cosmosdb_resource_group_name
  cosmosdb_resource_group_location    = var.cosmosdb_resource_group_location
  cosmosdb_account_name               = var.cosmosdb_account_name
  cosmosdb_kind                       = var.cosmosdb_kind
  cosmosdb_database_name              = var.cosmosdb_database_name
  cosmosdb_database_throughput        = var.cosmosdb_database_throughput
  cosmosdb_offer_type                 = var.cosmosdb_offer_type
  cosmosdb_consistency_level          = var.cosmosdb_consistency_level
  cosmosdb_consistency_max_interval   = var.cosmosdb_consistency_max_interval
  cosmosdb_consistency_max_staleness  = var.cosmosdb_consistency_max_staleness
  cosmosdb_geo_location               = var.cosmosdb_geo_location
  tags                                = var.tags
}

You should show your exact error message, not just give a vague description of it.

Additionally, please see Welcome to the forum - please reformat your message

My bad, i thought i copied above

Error: Reference to undeclared input variable
β”‚
β”‚   on main.tf line 4, in module "cosmosdb":
β”‚    4:   cosmosdb_resource_group_name        = var.cosmosdb_resource_group_name
β”‚
β”‚ An input variable with the name "cosmosdb_resource_group_name" has not been declared. This variable can be declared with a variable
β”‚ "cosmosdb_resource_group_name" {} block.
β•΅
β•·
β”‚ Error: Reference to undeclared input variable
β”‚
β”‚   on main.tf line 5, in module "cosmosdb":
β”‚    5:   cosmosdb_resource_group_location    = var.cosmosdb_resource_group_location
β”‚
β”‚ An input variable with the name "cosmosdb_resource_group_location" has not been declared. This variable can be declared with a variable      
β”‚ "cosmosdb_resource_group_location" {} block.
β•΅
β•·
β”‚ Error: Reference to undeclared input variable
β”‚
β”‚   on main.tf line 6, in module "cosmosdb":
β”‚    6:   cosmosdb_account_name               = var.cosmosdb_account_name
β”‚
β”‚ An input variable with the name "cosmosdb_account_name" has not been declared. This variable can be declared with a variable
β”‚ "cosmosdb_account_name" {} block.
β•΅
β•·
β”‚ Error: Reference to undeclared input variable
β”‚
β”‚   on main.tf line 7, in module "cosmosdb":
β”‚    7:   cosmosdb_kind                       = var.cosmosdb_kind
β”‚
β”‚ An input variable with the name "cosmosdb_kind" has not been declared. This variable can be declared with a variable "cosmosdb_kind" {}      
β”‚ block.
β•΅
β•·
β”‚ Error: Reference to undeclared input variable
β”‚
β”‚   on main.tf line 8, in module "cosmosdb":
β”‚    8:   cosmosdb_database_name              = var.cosmosdb_database_name
β”‚
β”‚ An input variable with the name "cosmosdb_database_name" has not been declared. This variable can be declared with a variable
β”‚ "cosmosdb_database_name" {} block.
β•΅
β•·
β”‚ Error: Reference to undeclared input variable
β”‚
β”‚   on main.tf line 9, in module "cosmosdb":
β”‚    9:   cosmosdb_database_throughput        = var.cosmosdb_database_throughput
β”‚
β”‚ An input variable with the name "cosmosdb_database_throughput" has not been declared. This variable can be declared with a variable
β”‚ "cosmosdb_database_throughput" {} block.
β•΅
β•·
β”‚ Error: Reference to undeclared input variable
β”‚
β”‚   on main.tf line 10, in module "cosmosdb":
β”‚   10:   cosmosdb_offer_type                 = var.cosmosdb_offer_type
β”‚
β”‚ An input variable with the name "cosmosdb_offer_type" has not been declared. This variable can be declared with a variable
β”‚ "cosmosdb_offer_type" {} block.
β•΅
β•·
β”‚ Error: Reference to undeclared input variable
β”‚
β”‚   on main.tf line 11, in module "cosmosdb":
β”‚   11:   cosmosdb_consistency_level          = var.cosmosdb_consistency_level
β”‚
β”‚ An input variable with the name "cosmosdb_consistency_level" has not been declared. This variable can be declared with a variable
β”‚ "cosmosdb_consistency_level" {} block.
β•΅
β•·
β”‚ Error: Reference to undeclared input variable
β”‚
β”‚   on main.tf line 12, in module "cosmosdb":
β”‚   12:   cosmosdb_consistency_max_interval   = var.cosmosdb_consistency_max_interval
β”‚
β”‚ An input variable with the name "cosmosdb_consistency_max_interval" has not been declared. This variable can be declared with a variable     
β”‚ "cosmosdb_consistency_max_interval" {} block.
β•΅
β•·
β”‚ Error: Reference to undeclared input variable
β”‚
β”‚   on main.tf line 13, in module "cosmosdb":
β”‚   13:   cosmosdb_consistency_max_staleness  = var.cosmosdb_consistency_max_staleness
β”‚
β”‚ An input variable with the name "cosmosdb_consistency_max_staleness" has not been declared. This variable can be declared with a variable    
β”‚ "cosmosdb_consistency_max_staleness" {} block.
β•΅
β•·
β”‚ Error: Reference to undeclared input variable
β”‚
β”‚   on main.tf line 14, in module "cosmosdb":
β”‚   14:   cosmosdb_geo_location               = var.cosmosdb_geo_location
β”‚
β”‚ An input variable with the name "cosmosdb_geo_location" has not been declared. This variable can be declared with a variable
β”‚ "cosmosdb_geo_location" {} block.
β•΅
β•·
β”‚ Error: Reference to undeclared input variable
β”‚
β”‚   on main.tf line 15, in module "cosmosdb":
β”‚   15:   tags                                = var.tags
β”‚
β”‚ An input variable with the name "tags" has not been declared. This variable can be declared with a variable "tags" {} block.
β•΅

Input variables defined in the root module are ones that get values from outside the Terraform configuration - e.g. from your terraform.tfvars file.

Input variables defined in child modules get their input from assignments inside module blocks.

Since you want to do both of these things, for each of these variables, you need a variable block in the root module and another identical variable block in modules/cosmosdb/.

1 Like

I think i get it. Thank you.
If i would change module call in this way, that would work?