【发布时间】:2021-05-20 10:47:24
【问题描述】:
有人可以帮助澄清局部变量和全局变量在 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"
【问题讨论】:
-
您没有在
pub-rt模块中传递vpc_cidr_block。 -
我不需要“pub-rt”中的“vpc_cidr_block”。我只在“pri-rt”中需要它
标签: terraform