Search code examples
terraform

Reusing variables declared and defined in parent module in terraform


Suppose in parent module

module "gcs_buckets" {
  source  = "something...aws or azure"
  version = "~> 1.7"
  project_id  = var.id
  names = var.names_0 
  prefix = var.prefix_0
}
    
variable "id" { type = string }
    
variable "names_0" {type = list }  
    
variable "prefix_0" {type = string }

I used in child module below format ,its throwing error

module "gcs_buckets_0" {
  source  = "../modules/buckets_00"
  version = "~> 1.7"
  project_id  = var.id
  names = var.names 
  prefix = var.prefix
}

variable "id" {
    type = string 
}

variable "names" {
    type = list  
}  

variable "prefix" {
    type = string  
}

Its throwing error, suggest me how to use variables in child module


Solution

  • Modules do not inherit variables from the parent module. All modules are self-contained units. So you have to explicitly define variables in the child module, and then explicit set these variables in the parent module, when you instantiate the child module.