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.
Module variable assignments must be explicitly set by the parent module when it instantiates the child module.
Variable Declaration in Child Module (modules/vm/main.tf):
variable "instance_type" {
type = string
default = "t2.micro" # This is an optional default value.
}
resource "aws_instance" "example" {
instance_type = var.instance_type
ami = "ami-abc123"
subnet_id = "subnet-1234abcd"
}
Variable Assignment in Parent Module (main.tf):
module "vm" {
source = "./modules/vm"
instance_type = "t3.micro" # Assigning a value to the variable.
}
In this example, instance_type is declared as a variable in the child module and is assigned a value by the parent module when it instantiates the vm module. The value "t3.micro" assigned by the parent module will override the default value "t2.micro" declared in the child module.
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.
I think we are confused between variable assignment and variable declaration. Variable need to be declared in child module but not necessarily be assigned. We can pass values from parent module
Modules do not inherit variables from the parent module
upvoted 2 times
...
Log in to ExamTopics
Sign in:
Community vote distribution
A (35%)
C (25%)
B (20%)
Other
Most Voted
A voting comment increases the vote count for the chosen answer by one.
Upvoting a comment with a selected answer will also increase the vote count towards that answer by one.
So if you see a comment that you already agree with, you can upvote it instead of posting a new comment.
stalk98
Highly Voted 2 years, 5 months agoBere
Most Recent 1 year, 1 month agomodarov
1 year, 3 months agocracit
1 year, 5 months agoDineshSG
1 year, 6 months agoNi33
1 year, 6 months agoEltooth
2 years, 4 months agoamrith501
2 years, 5 months ago