如何使用 terraform 输出作为另一个 terraform 模板的输入变量 [英] How to use terraform output as input variable of another terraform template

查看:42
本文介绍了如何使用 terraform 输出作为另一个 terraform 模板的输入变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么方法可以将 Terraform 模板输出用于另一个 Terraform 模板的输入?

Is there any way I can use a Terraform template output to another Terraform template's input?

例如:我有一个 Terraform 模板,它创建一个 ELB,我还有另一个 Terraform 模板,它将创建一个需要 ELB 信息作为输入变量的自动缩放组.

Ex: I have a Terraform template which creates an ELB and I have another Terraform template which is going to create an auto scale group which need the ELB information as an input variable.

我知道我可以使用 shell 脚本来 grep 并输入 ELB 信息,但我正在寻找一些 Terraform 方法来执行此操作.

I know I can use shell script to grep and feed in the ELB information, but I'm looking for some Terraform way to doing this.

推荐答案

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

Have you tried using remote state to populate your second template?

这样声明:

resource "terraform_remote_state" "your_state" {
  backend = "s3"
  config {
    bucket = "${var.your_bucket}"
    region = "${var.your_region}"
    key = "${var.your_state_file}"
  }
}

然后你应该可以像这样直接提取你的资源:

And then you should be able to pull out your resource directly like this:

your_elb = "${terraform_remote_state.your_state.output.your_output_resource}"

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

If this doesn't work for you, have you tried implementing your ELB in a module and then just using the output?

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

https://github.com/terraform-community-modules/tf_aws_elb is a good example of how to structure the module.

这篇关于如何使用 terraform 输出作为另一个 terraform 模板的输入变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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