将 terraform 输出从一个文件传递到另一个文件 [英] Pass terraform output from one file to another

查看:26
本文介绍了将 terraform 输出从一个文件传递到另一个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下结构:

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

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

I have created a vpc in test1/vpc.tf

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

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

I am getting vpc id in output like:

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

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

How can I pass this id to test2/subnet.tf file? I am searching online and can't seem to find an answer for this.

推荐答案

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

Create a variable in subnet.tf:

variable "vpc_id" {
  type = string
}

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

Then in your main terraform file where you are utilizing both of these modules, you would take the output from the vpc module and pass it to the input of the subnet module:

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

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

这篇关于将 terraform 输出从一个文件传递到另一个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆