Reputation: 13
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
Upvotes: 1
Views: 4240
Reputation: 67
It took me a while to come up with what turned out to be a dead simple pattern to allow several independant modules to share configuration.
Let's say you have split some configuration into independent conceptual modules:
- k8scluster
--- certamanager
--- cluster
--- clusterroles
And that you want to share variables/locals between these modules.
You can create a module that contains all this configuration, and import it as a submodule to all other modules:
- k8scluster
--- _shared
------ main.tf
--- certamanager
--- cluster
--- clusterroles
Then the shared module can export the config:
locals {
config = {
k8sname = "mycluster"
vnetgroup = "aksfrancevnet"
vnet = "aks-vnet2"
subnetname = "aks-subnet-winpl"
imageregistryname = "myregistry"
imageregistrygroup = "containers"
}
}
output "config" {
value = local.config
}
The on one of the modules just use:
module "_shared" {
source = "../_shared"
}
And you can start referencing these shared configuration:
data "azurerm_virtual_network" "vnet" {
name = module._shared.config.vnet
resource_group_name = module._shared.config.vnetgroup
}
Note here that we are not really "Reusing variables declared and defined in parent module in terraform", we are creating a module that will be imported as a submodule into all our modules.
Upvotes: 0
Reputation: 63
So terraform works based on the concept of calling module and the called module. If the called module has a set a variables for which inputs have to be defined externally, then calling module (or the root module) must provide the called module with the input values (provided it has defines the same variables as the called module).
For e.g, this is the calling module which passes in variable inputs by defining the same variables as the called module. Additionally, it also passes in the values through a .tfvars file.
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 4.16"
}
}
required_version = ">= 1.2.0"
}
module "ec2" {
source = "../modules/ec2"
region = var.region
env_prefix = var.env_prefix
machine_image = var.machine_image
machine_tier = var.machine_tier
}
Upvotes: 0
Reputation: 238867
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.
Upvotes: 4