如何使用terraform输出作为另一个terraform模板的输入变量

Joy*_*Joy 8 terraform

我是terraform的新手.

有什么办法可以将terraform模板输出用于另一个terraform模板输入吗?例如:我有一个创建elb的terraform模板,我有另一组terraform模板,它将创建需要elb信息作为输入变量的自动缩放组.我知道我可以使用shell脚本grep并输入elb信息.但我正在寻找一些实现这一目标的方式.

谢谢

mch*_*ier 6

您是否尝试过使用远程状态填充第二个模板?

像这样声明:

resource "terraform_remote_state" "your_state" {
  backend = "s3"
  config {
    bucket = "${var.your_bucket}"
    region = "${var.your_region}"
    key = "${var.your_state_file}"
  }
}
Run Code Online (Sandbox Code Playgroud)

然后,您应该能够像这样直接提取资源:

your_elb = "${terraform_remote_state.your_state.output.your_output_resource}"
Run Code Online (Sandbox Code Playgroud)

如果这对您不起作用,您是否尝试过在模块中实现ELB,然后仅使用输出?

https://github.com/terraform-community-modules/tf_aws_elb是如何构造模块的一个很好的例子。

  • 应该 your_elb = "${terraform_remote_state.your_state.outputs.your_output_resource}" - 输出而不是输出 (2认同)