【发布时间】:2021-07-13 23:45:18
【问题描述】:
版本控制:
Terraform v0.15.0
on linux_amd64
+ provider registry.terraform.io/hashicorp/aws v3.37.0
+ provider registry.terraform.io/hashicorp/random v3.1.0
我有以下文件夹结构:
terraform/
├── main.tf
└── terraform.tfvars
├── core
│ ├── main.tf
│ └── variables.tf
这里关注的文件是:
terraform/main.tf
terraform/terraform.tfvars
core/main.tf
core/variables.tf
高级概述是我正在尝试使用 TF 在 AWS 中创建 VPC,使用 terraform-aws-vpc 模块,因此我的根模块正在调用核心模块,该模块调用 terraform-provided vpc 模块建造。它是一个孩子叫一个孩子的原因是,一旦我让这个 vpc 步骤工作,“核心”模块将负责在帐户中构建其他基础设施资产。这个想法是让根模块为 AWS 账户资产的每个重要部分调用一个子模块。
我在这里遇到的问题是变量之一。
在core/ 中,我在core/variables.tf 中定义了以下变量:
variable "vpc_facts" {
type = map
}
引用该变量以获取core/main.tf 中 vpc 模块的信息:
module "vpc" {
source = "terraform-aws-modules/vpc/aws"
name = var.vpc_facts.name
cidr = var.vpc_facts.cidr
}
我在terraform/terraform.tfvars中定义了这些值,我希望将它们传递到模块中:
vpc_facts = {
name = "demo-vpc"
cidr = "192.168.0.0/16"
}
然后我从terraform/main.tf调用核心模块
module "core" {
source = "./core/"
}
我正在尝试从terraform/ 的根文件夹中调用terraform plan - 我已将core 作为模块包含在terraform/main.tf 中
我的思路是terraform/main.tf -> gathers terraform/terraform.tfvars -> core module -> uses terraform/terraform.tfvars as input variables for vpc module
但是,这似乎并没有发生。从根模块文件夹运行terraform plan 时,我不断收到此错误:
Releasing state lock. This may take a few moments...
╷
│ Warning: Value for undeclared variable
│
│ The root module does not declare a variable named "vpc_facts" but a value was found in file "terraform.tfvars". If you meant to use
│ this value, add a "variable" block to the configuration.
│
│ To silence these warnings, use TF_VAR_... environment variables to provide certain "global" settings to all configurations in your
│ organization. To reduce the verbosity of these warnings, use the -compact-warnings option.
╵
╷
│ Error: Missing required argument
│
│ on main.tf line 57, in module "core":
│ 57: module "core" {
│
│ The argument "vpc_facts" is required, but no definition was found.
我猜这应该已经定义了,因为我在 tfvars 和核心/变量中都定义了它,但是当我实际尝试在根 main.tf 中定义它时:
module "core" {
source = "./core/"
vpc_facts = var.vpc_facts
}
我得到一个不同的错误:
╷
│ Error: Reference to undeclared input variable
│
│ on main.tf line 60, in module "core":
│ 60: vpc_facts = var.vpc_facts
│
│ An input variable with the name "vpc_facts" has not been declared. This variable can be declared with a variable "vpc_facts" {}
│ block.
但它是在core/variables.tf 中声明并在terraform/terraform.tfvars 中赋值
我在这里缺少什么?这是否意味着我需要在子模块和根模块中重复定义变量?我认为如果根模块正在调用子模块,那么就变量而言,它是一个平面结构,并且该子模块可以看到 child/variables.tf
【问题讨论】:
标签: amazon-web-services terraform