【问题标题】:Terraform: Output a field from a moduleTerraform:从模块输出一个字段
【发布时间】:2018-04-12 13:40:34
【问题描述】:

代码

考虑一个 terraform 模块:

module "blah-asg" {
  source = "asg"

  asg_max_size       = 1
  asg_min_size       = "${var.min_blah}"
  ...
}

我的问题

我如何output 变量呢?

我尝试了什么

output "blah-es-asg" {
    value = "${asg.blah-asg.arn}"
}

失败了

获取插件时出错:模块根目录:发生 1 个错误: * 输出“blah-asg”:变量 asg.blah-asg.arn 中引用的未知资源“asg.blah”

我的问题

如何在 Terraform 中输出模块字段?

【问题讨论】:

  • 请考虑接受解决/解决您的问题的帖子(只是一个友好的提醒)。

标签: terraform


【解决方案1】:

所以首先需要在模块asg中设置输出:

$ cat asg/output.tf

output "blah-es-asg" {
    value = "${aws_autoscaling_group.blah-asg.arn}"
}

然后你用source = "asg"调用模块:

module "blah-asg" {
  source = "asg"

  asg_max_size       = 1
  asg_min_size       = "${var.min_blah}"
  ...
}

你现在可以用这种格式在当前代码中输出它:

output "blah-es-asg" {
    value = "${module.blah-asg.blah-es-asg}"
}

【讨论】:

  • 这很有帮助,应该是一个可以接受的答案。
  • 对您提供的解释和指导很有帮助。如果原始海报会使用比等等更好的东西,那就更清楚了。我花了一分钟来转换现实生活......但感谢其他人的帮助!!!
【解决方案2】:

模块本身不知道名称blah-asg - 这只是在调用它的脚本中 - 实际上它可以用不同的名称和参数多次调用。

输出应该只是引用模块内的东西,就像你在同一个模块的其他地方一样。例如,如果您想输出以下资源的 arn:

resource "aws_lb" "test" {
  # ...
}

你会使用:

output "blah-es-asg" {
    value = "${aws_lb.test.arn}"
}

请注意,输出是与模块代码的其余部分一起定义的,而不是在调用它的脚本中。

这个输出可以被调用模块的脚本使用为${module.blah-asg.blah-es-asg}

【讨论】:

    【解决方案3】:

    使用模块。[模块名称]。[输出变量名称]

    例如在 Azure 中创建一个名为 og 模块输出的资源组

    resource "azurerm_resource_group" "my-test-rg" {
         name =  module.blah-asg.blah-es-asg  
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-02-12
      • 2021-12-04
      • 2022-07-11
      • 2021-01-11
      • 1970-01-01
      • 1970-01-01
      • 2021-03-07
      • 1970-01-01
      相关资源
      最近更新 更多