Amazon web services Terraform模块-从根中识别子模块变量

Amazon web services Terraform模块-从根中识别子模块变量,amazon-web-services,terraform,Amazon Web Services,Terraform,版本控制: 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.

版本控制:

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提供的VPC模块进行构建。孩子打电话给孩子的原因是,一旦我让vpc这一步开始工作,“核心”模块将负责在帐户中构建其他基础设施资产。其思想是让根模块为AWS帐户资产的每个重要部分调用一个子模块

我在这里遇到的问题是一个变量

core/
中,我在
core/variables.tf中定义了以下变量:

variable "vpc_facts" {
    type = map
}
module "vpc" {
    source = "terraform-aws-modules/vpc/aws"

    name = var.vpc_facts.name
    cidr = var.vpc_facts.cidr
}
core/main.tf
中,vpc模块的信息引用了该变量:

variable "vpc_facts" {
    type = map
}
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计划
-我已将
核心
作为模块包含在
terraform/main.tf中

module "core" {
    source = "./core/"
}
我的思考过程是
terraform/main.tf->收集terraform/terraform.tfvars->核心模块->使用terraform/terraform.tfvars作为vpc模块的输入变量

然而,这似乎没有发生。在从根模块文件夹运行
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和core/variables中都定义了它,但是当我实际尝试在root 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.
但是它在
核心/variables.tf中声明,并在
地形/terraform.tfvars中赋值

我错过了什么?这是否意味着我需要在子模块和根模块中重复定义变量?我认为如果一个根模块正在调用一个子模块,它在变量方面是一个平面结构,而这个子模块可以看到child/variables.tf

这是否意味着我需要在子模块和根模块中重复定义变量


是的,正是它的意思。所有模块都是自包含的,子模块不会从父模块继承变量。您必须在模块中明确定义变量,然后在创建模块时在父模块中设置它们。

@Patrick没问题。很高兴我能帮忙。