首页 > 解决方案 > 将 terraform 输出从一个文件传递到另一个文件

问题描述

我有以下结构:

modules
 |_ test1
 |    |_vpc.tf
 |_test2
      |_subnet.tf

我在 test1/vpc.tf 中创建了一个 vpc

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

我在输出中得到 vpc id,例如:

output "vpc_id" {
  value = aws_vpc.main.id
}

如何将此 id 传递给 test2/subnet.tf 文件?我正在网上搜索,似乎无法找到答案。

标签: terraformterraform0.12+

解决方案


在 subnet.tf 中创建一个变量:

variable "vpc_id" {
  type = string
}

然后在您使用这两个模块的主 terraform 文件中,您将从 vpc 模块获取输出并将其传递给子网模块的输入:

module "vpc" {
  source = "modules/test1"
}

module "subnet" {
  source = "modules/test2"
  vpc_id = module.vpc.vpc_id
}

推荐阅读