Reputation: 441
Someone can help to clarify how local and global variables working in Terraform ? I'm facing now this issue :
PS E:\GitRepo\Terraform\prod> terraform plan ╷ │ Error: Missing required argument │ │ on main.tf line 46, in module "pub-rt": │ 46: module "pub-rt" { │ │ The argument "vpc_cidr_block" is required, but no definition was found. ╵ ╷ │ Error: Missing required argument │ │ on main.tf line 46, in module "pub-rt": │ 46: module "pub-rt" { │ │ The argument "nat_id" is required, but no definition was found.
My code structure is :
-- Dev
-- main.tf
-- modules
-- rt
-- pub-rt.tf
-- pri-rt.tf
-- vars.tf
This is my main.tf
# Create Public Route Table
module "pub-rt" {
source = "../modules/rt"
pub_rt_tag = { Name = "prod-pub-rt" }
vpc_id = module.vpc.vpcId
ir_cidr = var.ir_cidr # routing inside the VPC
gateway_id = module.igw.igwId # routing to the internet through igw
}
# Create Private Route Table
module "pri-rt" {
source = "../modules/rt"
pub_rt_tag = { Name = "prod-pri-rt" }
vpc_id = module.vpc.vpcId
vpc_cidr_block = var.vpc_cidr # routing inside the VPC
nat_id = module.nat.natId # routing to the internet NAT
}
My ../rt/vars.tf contains :
variable "vpc_cidr_block" { } //this variable point to "pri-rt.tf"
variable "vpc_id" { } //this variable common and point to "pub-rt.tf" and "pri-rt.tf"
variable "gateway_id" { } //this variable point to "pub-rt.tf"
variable "nat_id" { } //this variable point to "pri-rt.tf"
variable "ir_cidr" { } //this variable point to "pub-rt.tf"
Upvotes: 3
Views: 14956
Reputation: 238081
Variables have module scope, so there is no global variables that propagate over all sub-modules. Your vars.tf
should be in ./Dev
. You need also corresponding vars.tf
with variables specific to your module.
Upvotes: 2