有人可以帮助澄清 Terraform 中的局部变量和全局变量是如何工作的吗?我现在面临这个问题:
<块引用>PS E:\GitRepo\Terraform\prod> terraform 计划 ╷ │ 错误:缺少必需的参数 │ │ 在 main.tf 第 46 行,在模块“pub-rt”中: │ 46:模块“pub-rt”{ │ │ 参数“vpc_cidr_block”是必需的,但没有找到定义。 ╵ ╷ │ 错误:缺少必需的参数 │ │ 在 main.tf 第 46 行,在模块“pub-rt”中: │ 46:模块“pub-rt”{ │ │ 参数“nat_id”是必需的,但没有找到定义。
我的代码结构是:
-- Dev
-- main.tf
-- modules
-- rt
-- pub-rt.tf
-- pri-rt.tf
-- vars.tf
这是我的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
}
我的 ../rt/vars.tf 包含:
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"