Module terraform v0.12.12在模块之间传递数据-这应该如何工作?

Module terraform v0.12.12在模块之间传递数据-这应该如何工作?,module,output,terraform,Module,Output,Terraform,我正在努力理解Terraform v0.12.12中数据进出模块的方式。我有一个我认为非常简单的例子,但不理解数据应该如何在模块之间传递。我能找到的大多数例子不是不完整就是过时了 我创建了一个包含两个模块的简单示例。创建vpc和子网的网络模块,以及创建EC2实例的计算模块。我只是尝试向计算模块提供EC2实例应该去的子网的id。但我不明白: 如何从创建子网的网络模块中获取子网id 子网到其他模块可以使用吗? 如何让计算模块使用子网id? 基本结构如下 。 ├── main.tf └── 模块 ├─

我正在努力理解Terraform v0.12.12中数据进出模块的方式。我有一个我认为非常简单的例子,但不理解数据应该如何在模块之间传递。我能找到的大多数例子不是不完整就是过时了

我创建了一个包含两个模块的简单示例。创建vpc和子网的网络模块,以及创建EC2实例的计算模块。我只是尝试向计算模块提供EC2实例应该去的子网的id。但我不明白:

如何从创建子网的网络模块中获取子网id 子网到其他模块可以使用吗? 如何让计算模块使用子网id? 基本结构如下

。 ├── main.tf └── 模块 ├── 计算 │ └── main.tf └── 网络 ├── main.tf └── output.tf

您需要将variables.tf文件添加到计算模块,以便它可以从网络模块接收子网id

检查variables.tf文件内容和compute模块的main.tf 查看如何访问输入变量

示例的结构应如下所示

.
├── main.tf
└── modules
    ├── compute
    │   ├── main.tf
    │   └── variables.tf
    └── network
        ├── main.tf
        └── output.tf
然后在每个文件中可以执行类似的操作

# main.tf

provider "aws" {
    region     = "eu-west-1"
}

# Call network module and receive output
module "m_network" {
    source      = "./modules/network"
}

module "m_compute" {
    source     = "./modules/compute"
    # pass the output of the network module
    # as input variables for the compute module
    subnet_id  = module.m_network.output_subnet_id
}
您需要将variables.tf文件添加到计算模块,以便它可以从网络模块接收子网id

检查variables.tf文件内容和compute模块的main.tf 查看如何访问输入变量

示例的结构应如下所示

.
├── main.tf
└── modules
    ├── compute
    │   ├── main.tf
    │   └── variables.tf
    └── network
        ├── main.tf
        └── output.tf
然后在每个文件中可以执行类似的操作

# main.tf

provider "aws" {
    region     = "eu-west-1"
}

# Call network module and receive output
module "m_network" {
    source      = "./modules/network"
}

module "m_compute" {
    source     = "./modules/compute"
    # pass the output of the network module
    # as input variables for the compute module
    subnet_id  = module.m_network.output_subnet_id
}
在./modules/compute模块中是否有可变子网id块?有关此类块内容的更多详细信息,请参阅。在./modules/compute模块中是否有可变子网id块?有关此类块内容的更多详细信息,请参见。
.
├── main.tf
└── modules
    ├── compute
    │   ├── main.tf
    │   └── variables.tf
    └── network
        ├── main.tf
        └── output.tf
# main.tf

provider "aws" {
    region     = "eu-west-1"
}

# Call network module and receive output
module "m_network" {
    source      = "./modules/network"
}

module "m_compute" {
    source     = "./modules/compute"
    # pass the output of the network module
    # as input variables for the compute module
    subnet_id  = module.m_network.output_subnet_id
}
# compute module | variables.tf

# declare a input variable for the compute module
variable "subnet_id" {
  description = "The subnet ID from the network module"

  # You can also enforce the type with
  # type = string OR number OR etc.
}

# compute module | main.tf

resource "aws_instance" "app" {
    ami           = "ami-13be557e"
    instance_type = "t2.micro"
    # you can use variables with var.{name}
    # access the subnet id variable
    subnet_id     = var.subnet_id
}
# network module | main.tf

resource "aws_vpc" "myvpc" {
  cidr_block = "10.0.0.0/16"
}

resource "aws_subnet" "myvpc_subnet" {
  vpc_id                  = "${aws_vpc.myvpc.id}"
  cidr_block              = "10.0.1.0/24"
}
# network module | output.tf

output "output_subnet_id" {
    description = "Subnet ID"
    value = "${aws_subnet.myvpc_subnet.id}"
}