How to use terraform output as input variable of another terraform template

前端 未结 2 811
悲哀的现实
悲哀的现实 2021-02-07 10:25

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

Ex: I have a Terraform template which creates an ELB and I have another T

相关标签:
2条回答
  • 2021-02-07 10:32

    Looks like in newer versions of Terraform you'd access the output var like this

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

    All the rest is the same, just how you referenced it.

    0 讨论(0)
  • 2021-02-07 10:47

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

    Declare it like this:

    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}"
    

    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 is a good example of how to structure the module.

    0 讨论(0)
提交回复
热议问题