Col*_*rk1 2 amazon-web-services terraform devops infrastructure-as-code
我正在处理基础设施配置,所以我将模块称为嵌套。
有我的文件系统树。
??? main.tf
??? modules
??? client.tf
??? in
??? main.tf
Run Code Online (Sandbox Code Playgroud)
我的文件显示如下。
#main.tf
module "my_vpc" {
source = "./modules"
}
# modules/client.tf
provider "aws" {
region = "us-east-2"
}
module "inner" {
source = "./in"
}
# in/main.tf
provider "aws" {
region = "us-east-2"
}
resource "aws_vpc" "main" {
cidr_block = "10.0.0.0/16"
}
output "vpc_id" {
value = "${aws_vpc.main.id}"
}
Run Code Online (Sandbox Code Playgroud)
所以就我而言,我想从 in/main.tf 中的资源创建模块中获取输出。但是当我运行 terraform apply 命令时,没有输出。
我该如何解决这个问题?
您使用了两个模块,但只有一个输出语句。
./main.tf创建模块my_vpc从./modules/client.tf
在client.tf您创建模块inner从./modules/in/main.tf
该模块inner有一个vpc_id定义在./modules/in/main.tf
你需要在./modules/client.tf级别上做一个输出语句的输出。您想要输出的任何模块都必须具有该变量的输出语句,即使输出链接了内部模块的输出。
# ./modules/client.tf
provider "aws" {
region = "us-east-2"
}
module "inner" {
source = "./in"
}
output "vpc_id" {
value = "${modules.inner.vpc_id}"
}
Run Code Online (Sandbox Code Playgroud)
现在定义的模块在./modules/client.tf顶层输出您想要的值。你可以./main.tf像这样与它交互:
#main.tf
module "my_vpc" {
source = "./modules"
}
locals {
vpc_id = "${modules.my_vpc.vpc_id}"
}
# output the vpc id if you need to
output "vpc_id" {
value = "${modules.my_vpc.vpc_id}"
}
Run Code Online (Sandbox Code Playgroud)
附带说明一下,随着您扩大 terraform 和模块的使用,保持一致将有所帮助。如果您打算在另一个模块中包含一个模块,我建议您使用如下一致的文件夹结构。
??? main.tf
??? modules
??? vpc
??? modules
? ??? in
? ??? main.tf
??? client.tf
??? another_module
??? main.tf
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8607 次 |
| 最近记录: |