问题描述
有什么方法可以将 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 模板的输入变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!